Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 4 | * Copyright (c) 2012 Red Hat, Inc. |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 5 | * All Rights Reserved. |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 6 | */ |
| 7 | #include "xfs.h" |
| 8 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 9 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 10 | #include "xfs_format.h" |
| 11 | #include "xfs_log_format.h" |
| 12 | #include "xfs_trans_resv.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 13 | #include "xfs_bit.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 14 | #include "xfs_mount.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 15 | #include "xfs_da_format.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 16 | #include "xfs_defer.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 17 | #include "xfs_inode.h" |
| 18 | #include "xfs_btree.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 19 | #include "xfs_trans.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 20 | #include "xfs_extfree_item.h" |
| 21 | #include "xfs_alloc.h" |
| 22 | #include "xfs_bmap.h" |
| 23 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 24 | #include "xfs_bmap_btree.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 25 | #include "xfs_rtalloc.h" |
| 26 | #include "xfs_error.h" |
| 27 | #include "xfs_quota.h" |
| 28 | #include "xfs_trans_space.h" |
| 29 | #include "xfs_trace.h" |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 30 | #include "xfs_icache.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 31 | #include "xfs_log.h" |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 32 | #include "xfs_rmap_btree.h" |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 33 | #include "xfs_iomap.h" |
| 34 | #include "xfs_reflink.h" |
| 35 | #include "xfs_refcount.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 36 | |
| 37 | /* Kernel only BMAP related definitions and functions */ |
| 38 | |
| 39 | /* |
| 40 | * Convert the given file system block to a disk block. We have to treat it |
| 41 | * differently based on whether the file is a real time file or not, because the |
| 42 | * bmap code does. |
| 43 | */ |
| 44 | xfs_daddr_t |
| 45 | xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb) |
| 46 | { |
| 47 | return (XFS_IS_REALTIME_INODE(ip) ? \ |
| 48 | (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \ |
| 49 | XFS_FSB_TO_DADDR((ip)->i_mount, (fsb))); |
| 50 | } |
| 51 | |
| 52 | /* |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 53 | * Routine to zero an extent on disk allocated to the specific inode. |
| 54 | * |
| 55 | * The VFS functions take a linearised filesystem block offset, so we have to |
| 56 | * convert the sparse xfs fsb to the right format first. |
| 57 | * VFS types are real funky, too. |
| 58 | */ |
| 59 | int |
| 60 | xfs_zero_extent( |
| 61 | struct xfs_inode *ip, |
| 62 | xfs_fsblock_t start_fsb, |
| 63 | xfs_off_t count_fsb) |
| 64 | { |
| 65 | struct xfs_mount *mp = ip->i_mount; |
| 66 | xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb); |
| 67 | sector_t block = XFS_BB_TO_FSBT(mp, sector); |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 68 | |
Matthew Wilcox | 3dc2916 | 2016-03-15 11:20:41 -0600 | [diff] [blame] | 69 | return blkdev_issue_zeroout(xfs_find_bdev_for_inode(VFS_I(ip)), |
| 70 | block << (mp->m_super->s_blocksize_bits - 9), |
| 71 | count_fsb << (mp->m_super->s_blocksize_bits - 9), |
Christoph Hellwig | ee472d8 | 2017-04-05 19:21:08 +0200 | [diff] [blame] | 72 | GFP_NOFS, 0); |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 73 | } |
| 74 | |
Dave Chinner | bb9c2e5 | 2017-10-09 11:37:22 -0700 | [diff] [blame] | 75 | #ifdef CONFIG_XFS_RT |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 76 | int |
| 77 | xfs_bmap_rtalloc( |
| 78 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
| 79 | { |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 80 | int error; /* error return value */ |
| 81 | xfs_mount_t *mp; /* mount point structure */ |
| 82 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 83 | xfs_extlen_t mod = 0; /* product factor for allocators */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 84 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 85 | xfs_extlen_t align; /* minimum allocation alignment */ |
| 86 | xfs_rtblock_t rtb; |
| 87 | |
| 88 | mp = ap->ip->i_mount; |
| 89 | align = xfs_get_extsz_hint(ap->ip); |
| 90 | prod = align / mp->m_sb.sb_rextsize; |
| 91 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
| 92 | align, 1, ap->eof, 0, |
| 93 | ap->conv, &ap->offset, &ap->length); |
| 94 | if (error) |
| 95 | return error; |
| 96 | ASSERT(ap->length); |
| 97 | ASSERT(ap->length % mp->m_sb.sb_rextsize == 0); |
| 98 | |
| 99 | /* |
| 100 | * If the offset & length are not perfectly aligned |
| 101 | * then kill prod, it will just get us in trouble. |
| 102 | */ |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 103 | div_u64_rem(ap->offset, align, &mod); |
| 104 | if (mod || ap->length % align) |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 105 | prod = 1; |
| 106 | /* |
| 107 | * Set ralen to be the actual requested length in rtextents. |
| 108 | */ |
| 109 | ralen = ap->length / mp->m_sb.sb_rextsize; |
| 110 | /* |
| 111 | * If the old value was close enough to MAXEXTLEN that |
| 112 | * we rounded up to it, cut it back so it's valid again. |
| 113 | * Note that if it's a really large request (bigger than |
| 114 | * MAXEXTLEN), we don't hear about that number, and can't |
| 115 | * adjust the starting point to match it. |
| 116 | */ |
| 117 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 118 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
| 119 | |
| 120 | /* |
Dave Chinner | 4b680af | 2016-02-08 10:46:51 +1100 | [diff] [blame] | 121 | * Lock out modifications to both the RT bitmap and summary inodes |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 122 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 123 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 124 | xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 125 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
Dave Chinner | 4b680af | 2016-02-08 10:46:51 +1100 | [diff] [blame] | 126 | xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * If it's an allocation to an empty file at offset 0, |
| 130 | * pick an extent that will space things out in the rt area. |
| 131 | */ |
| 132 | if (ap->eof && ap->offset == 0) { |
| 133 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ |
| 134 | |
| 135 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 136 | if (error) |
| 137 | return error; |
| 138 | ap->blkno = rtx * mp->m_sb.sb_rextsize; |
| 139 | } else { |
| 140 | ap->blkno = 0; |
| 141 | } |
| 142 | |
| 143 | xfs_bmap_adjacent(ap); |
| 144 | |
| 145 | /* |
| 146 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 147 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 148 | do_div(ap->blkno, mp->m_sb.sb_rextsize); |
| 149 | rtb = ap->blkno; |
| 150 | ap->length = ralen; |
Christoph Hellwig | 089ec2f | 2017-02-17 08:21:06 -0800 | [diff] [blame] | 151 | error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length, |
| 152 | &ralen, ap->wasdel, prod, &rtb); |
| 153 | if (error) |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 154 | return error; |
Christoph Hellwig | 089ec2f | 2017-02-17 08:21:06 -0800 | [diff] [blame] | 155 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 156 | ap->blkno = rtb; |
| 157 | if (ap->blkno != NULLFSBLOCK) { |
| 158 | ap->blkno *= mp->m_sb.sb_rextsize; |
| 159 | ralen *= mp->m_sb.sb_rextsize; |
| 160 | ap->length = ralen; |
| 161 | ap->ip->i_d.di_nblocks += ralen; |
| 162 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 163 | if (ap->wasdel) |
| 164 | ap->ip->i_delayed_blks -= ralen; |
| 165 | /* |
| 166 | * Adjust the disk quota also. This was reserved |
| 167 | * earlier. |
| 168 | */ |
| 169 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 170 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 171 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 172 | |
| 173 | /* Zero the extent if we were asked to do so */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 174 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) { |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 175 | error = xfs_zero_extent(ap->ip, ap->blkno, ap->length); |
| 176 | if (error) |
| 177 | return error; |
| 178 | } |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 179 | } else { |
| 180 | ap->length = 0; |
| 181 | } |
| 182 | return 0; |
| 183 | } |
Dave Chinner | bb9c2e5 | 2017-10-09 11:37:22 -0700 | [diff] [blame] | 184 | #endif /* CONFIG_XFS_RT */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 185 | |
| 186 | /* |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 187 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 188 | * the allocation to a stripe unit boundary. All offsets are considered outside |
| 189 | * the end of file for an empty fork, so 1 is returned in *eof in that case. |
| 190 | */ |
| 191 | int |
| 192 | xfs_bmap_eof( |
| 193 | struct xfs_inode *ip, |
| 194 | xfs_fileoff_t endoff, |
| 195 | int whichfork, |
| 196 | int *eof) |
| 197 | { |
| 198 | struct xfs_bmbt_irec rec; |
| 199 | int error; |
| 200 | |
| 201 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof); |
| 202 | if (error || *eof) |
| 203 | return error; |
| 204 | |
| 205 | *eof = endoff >= rec.br_startoff + rec.br_blockcount; |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Extent tree block counting routines. |
| 211 | */ |
| 212 | |
| 213 | /* |
Darrick J. Wong | d29cb3e | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 214 | * Count leaf blocks given a range of extent records. Delayed allocation |
| 215 | * extents are not counted towards the totals. |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 216 | */ |
Christoph Hellwig | e17a5c6 | 2017-08-29 15:44:14 -0700 | [diff] [blame] | 217 | xfs_extnum_t |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 218 | xfs_bmap_count_leaves( |
Darrick J. Wong | d29cb3e | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 219 | struct xfs_ifork *ifp, |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 220 | xfs_filblks_t *count) |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 221 | { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 222 | struct xfs_iext_cursor icur; |
Christoph Hellwig | e17a5c6 | 2017-08-29 15:44:14 -0700 | [diff] [blame] | 223 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 224 | xfs_extnum_t numrecs = 0; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 225 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 226 | for_each_xfs_iext(ifp, &icur, &got) { |
Christoph Hellwig | e17a5c6 | 2017-08-29 15:44:14 -0700 | [diff] [blame] | 227 | if (!isnullstartblock(got.br_startblock)) { |
| 228 | *count += got.br_blockcount; |
| 229 | numrecs++; |
Darrick J. Wong | d29cb3e | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 230 | } |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 231 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 232 | |
Christoph Hellwig | e17a5c6 | 2017-08-29 15:44:14 -0700 | [diff] [blame] | 233 | return numrecs; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Count leaf blocks given a range of extent records originally |
| 238 | * in btree format. |
| 239 | */ |
| 240 | STATIC void |
| 241 | xfs_bmap_disk_count_leaves( |
| 242 | struct xfs_mount *mp, |
| 243 | struct xfs_btree_block *block, |
| 244 | int numrecs, |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 245 | xfs_filblks_t *count) |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 246 | { |
| 247 | int b; |
| 248 | xfs_bmbt_rec_t *frp; |
| 249 | |
| 250 | for (b = 1; b <= numrecs; b++) { |
| 251 | frp = XFS_BMBT_REC_ADDR(mp, block, b); |
| 252 | *count += xfs_bmbt_disk_get_blockcount(frp); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Recursively walks each level of a btree |
Zhi Yong Wu | 8be11e9 | 2013-08-12 03:14:52 +0000 | [diff] [blame] | 258 | * to count total fsblocks in use. |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 259 | */ |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 260 | STATIC int |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 261 | xfs_bmap_count_tree( |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 262 | struct xfs_mount *mp, |
| 263 | struct xfs_trans *tp, |
| 264 | struct xfs_ifork *ifp, |
| 265 | xfs_fsblock_t blockno, |
| 266 | int levelin, |
| 267 | xfs_extnum_t *nextents, |
| 268 | xfs_filblks_t *count) |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 269 | { |
| 270 | int error; |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 271 | struct xfs_buf *bp, *nbp; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 272 | int level = levelin; |
| 273 | __be64 *pp; |
| 274 | xfs_fsblock_t bno = blockno; |
| 275 | xfs_fsblock_t nextbno; |
| 276 | struct xfs_btree_block *block, *nextblock; |
| 277 | int numrecs; |
| 278 | |
| 279 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF, |
| 280 | &xfs_bmbt_buf_ops); |
| 281 | if (error) |
| 282 | return error; |
| 283 | *count += 1; |
| 284 | block = XFS_BUF_TO_BLOCK(bp); |
| 285 | |
| 286 | if (--level) { |
| 287 | /* Not at node above leaves, count this level of nodes */ |
| 288 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 289 | while (nextbno != NULLFSBLOCK) { |
| 290 | error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp, |
| 291 | XFS_BMAP_BTREE_REF, |
| 292 | &xfs_bmbt_buf_ops); |
| 293 | if (error) |
| 294 | return error; |
| 295 | *count += 1; |
| 296 | nextblock = XFS_BUF_TO_BLOCK(nbp); |
| 297 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); |
| 298 | xfs_trans_brelse(tp, nbp); |
| 299 | } |
| 300 | |
| 301 | /* Dive to the next level */ |
| 302 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 303 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 304 | error = xfs_bmap_count_tree(mp, tp, ifp, bno, level, nextents, |
| 305 | count); |
| 306 | if (error) { |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 307 | xfs_trans_brelse(tp, bp); |
| 308 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 309 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 310 | return -EFSCORRUPTED; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 311 | } |
| 312 | xfs_trans_brelse(tp, bp); |
| 313 | } else { |
| 314 | /* count all level 1 nodes and their leaves */ |
| 315 | for (;;) { |
| 316 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 317 | numrecs = be16_to_cpu(block->bb_numrecs); |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 318 | (*nextents) += numrecs; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 319 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
| 320 | xfs_trans_brelse(tp, bp); |
| 321 | if (nextbno == NULLFSBLOCK) |
| 322 | break; |
| 323 | bno = nextbno; |
| 324 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 325 | XFS_BMAP_BTREE_REF, |
| 326 | &xfs_bmbt_buf_ops); |
| 327 | if (error) |
| 328 | return error; |
| 329 | *count += 1; |
| 330 | block = XFS_BUF_TO_BLOCK(bp); |
| 331 | } |
| 332 | } |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | /* |
Darrick J. Wong | d29cb3e | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 337 | * Count fsblocks of the given fork. Delayed allocation extents are |
| 338 | * not counted towards the totals. |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 339 | */ |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 340 | int |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 341 | xfs_bmap_count_blocks( |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 342 | struct xfs_trans *tp, |
| 343 | struct xfs_inode *ip, |
| 344 | int whichfork, |
| 345 | xfs_extnum_t *nextents, |
| 346 | xfs_filblks_t *count) |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 347 | { |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 348 | struct xfs_mount *mp; /* file system mount structure */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 349 | __be64 *pp; /* pointer to block address */ |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 350 | struct xfs_btree_block *block; /* current btree block */ |
| 351 | struct xfs_ifork *ifp; /* fork structure */ |
| 352 | xfs_fsblock_t bno; /* block # of "block" */ |
| 353 | int level; /* btree level, for checking */ |
| 354 | int error; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 355 | |
| 356 | bno = NULLFSBLOCK; |
| 357 | mp = ip->i_mount; |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 358 | *nextents = 0; |
| 359 | *count = 0; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 360 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 361 | if (!ifp) |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 362 | return 0; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 363 | |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 364 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 365 | case XFS_DINODE_FMT_EXTENTS: |
Christoph Hellwig | e17a5c6 | 2017-08-29 15:44:14 -0700 | [diff] [blame] | 366 | *nextents = xfs_bmap_count_leaves(ifp, count); |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 367 | return 0; |
| 368 | case XFS_DINODE_FMT_BTREE: |
| 369 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 370 | error = xfs_iread_extents(tp, ip, whichfork); |
| 371 | if (error) |
| 372 | return error; |
| 373 | } |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 374 | |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 375 | /* |
| 376 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 377 | */ |
| 378 | block = ifp->if_broot; |
| 379 | level = be16_to_cpu(block->bb_level); |
| 380 | ASSERT(level > 0); |
| 381 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 382 | bno = be64_to_cpu(*pp); |
| 383 | ASSERT(bno != NULLFSBLOCK); |
| 384 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 385 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 386 | |
| 387 | error = xfs_bmap_count_tree(mp, tp, ifp, bno, level, |
| 388 | nextents, count); |
| 389 | if (error) { |
| 390 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", |
| 391 | XFS_ERRLEVEL_LOW, mp); |
| 392 | return -EFSCORRUPTED; |
| 393 | } |
| 394 | return 0; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 400 | static int |
| 401 | xfs_getbmap_report_one( |
| 402 | struct xfs_inode *ip, |
| 403 | struct getbmapx *bmv, |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 404 | struct kgetbmap *out, |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 405 | int64_t bmv_end, |
| 406 | struct xfs_bmbt_irec *got) |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 407 | { |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 408 | struct kgetbmap *p = out + bmv->bmv_entries; |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 409 | bool shared = false, trimmed = false; |
| 410 | int error; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 411 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 412 | error = xfs_reflink_trim_around_shared(ip, got, &shared, &trimmed); |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 413 | if (error) |
| 414 | return error; |
| 415 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 416 | if (isnullstartblock(got->br_startblock) || |
| 417 | got->br_startblock == DELAYSTARTBLOCK) { |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 418 | /* |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 419 | * Delalloc extents that start beyond EOF can occur due to |
| 420 | * speculative EOF allocation when the delalloc extent is larger |
| 421 | * than the largest freespace extent at conversion time. These |
| 422 | * extents cannot be converted by data writeback, so can exist |
| 423 | * here even if we are not supposed to be finding delalloc |
| 424 | * extents. |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 425 | */ |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 426 | if (got->br_startoff < XFS_B_TO_FSB(ip->i_mount, XFS_ISIZE(ip))) |
| 427 | ASSERT((bmv->bmv_iflags & BMV_IF_DELALLOC) != 0); |
| 428 | |
| 429 | p->bmv_oflags |= BMV_OF_DELALLOC; |
| 430 | p->bmv_block = -2; |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 431 | } else { |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 432 | p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock); |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 435 | if (got->br_state == XFS_EXT_UNWRITTEN && |
| 436 | (bmv->bmv_iflags & BMV_IF_PREALLOC)) |
| 437 | p->bmv_oflags |= BMV_OF_PREALLOC; |
| 438 | |
| 439 | if (shared) |
| 440 | p->bmv_oflags |= BMV_OF_SHARED; |
| 441 | |
| 442 | p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff); |
| 443 | p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount); |
| 444 | |
| 445 | bmv->bmv_offset = p->bmv_offset + p->bmv_length; |
| 446 | bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset); |
| 447 | bmv->bmv_entries++; |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 451 | static void |
| 452 | xfs_getbmap_report_hole( |
| 453 | struct xfs_inode *ip, |
| 454 | struct getbmapx *bmv, |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 455 | struct kgetbmap *out, |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 456 | int64_t bmv_end, |
| 457 | xfs_fileoff_t bno, |
| 458 | xfs_fileoff_t end) |
| 459 | { |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 460 | struct kgetbmap *p = out + bmv->bmv_entries; |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 461 | |
| 462 | if (bmv->bmv_iflags & BMV_IF_NO_HOLES) |
| 463 | return; |
| 464 | |
| 465 | p->bmv_block = -1; |
| 466 | p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno); |
| 467 | p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno); |
| 468 | |
| 469 | bmv->bmv_offset = p->bmv_offset + p->bmv_length; |
| 470 | bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset); |
| 471 | bmv->bmv_entries++; |
| 472 | } |
| 473 | |
| 474 | static inline bool |
| 475 | xfs_getbmap_full( |
| 476 | struct getbmapx *bmv) |
| 477 | { |
| 478 | return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1; |
| 479 | } |
| 480 | |
| 481 | static bool |
| 482 | xfs_getbmap_next_rec( |
| 483 | struct xfs_bmbt_irec *rec, |
| 484 | xfs_fileoff_t total_end) |
| 485 | { |
| 486 | xfs_fileoff_t end = rec->br_startoff + rec->br_blockcount; |
| 487 | |
| 488 | if (end == total_end) |
| 489 | return false; |
| 490 | |
| 491 | rec->br_startoff += rec->br_blockcount; |
| 492 | if (!isnullstartblock(rec->br_startblock) && |
| 493 | rec->br_startblock != DELAYSTARTBLOCK) |
| 494 | rec->br_startblock += rec->br_blockcount; |
| 495 | rec->br_blockcount = total_end - end; |
| 496 | return true; |
| 497 | } |
| 498 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 499 | /* |
| 500 | * Get inode's extents as described in bmv, and format for output. |
| 501 | * Calls formatter to fill the user's buffer until all extents |
| 502 | * are mapped, until the passed-in bmv->bmv_count slots have |
| 503 | * been filled, or until the formatter short-circuits the loop, |
| 504 | * if it is tracking filled-in extents on its own. |
| 505 | */ |
| 506 | int /* error code */ |
| 507 | xfs_getbmap( |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 508 | struct xfs_inode *ip, |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 509 | struct getbmapx *bmv, /* user bmap structure */ |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 510 | struct kgetbmap *out) |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 511 | { |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 512 | struct xfs_mount *mp = ip->i_mount; |
| 513 | int iflags = bmv->bmv_iflags; |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 514 | int whichfork, lock, error = 0; |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 515 | int64_t bmv_end, max_len; |
| 516 | xfs_fileoff_t bno, first_bno; |
| 517 | struct xfs_ifork *ifp; |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 518 | struct xfs_bmbt_irec got, rec; |
| 519 | xfs_filblks_t len; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 520 | struct xfs_iext_cursor icur; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 521 | |
Christoph Hellwig | 232b5194 | 2017-10-17 14:16:19 -0700 | [diff] [blame] | 522 | if (bmv->bmv_iflags & ~BMV_IF_VALID) |
| 523 | return -EINVAL; |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 524 | #ifndef DEBUG |
| 525 | /* Only allow CoW fork queries if we're debugging. */ |
| 526 | if (iflags & BMV_IF_COWFORK) |
| 527 | return -EINVAL; |
| 528 | #endif |
| 529 | if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK)) |
| 530 | return -EINVAL; |
| 531 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 532 | if (bmv->bmv_length < -1) |
| 533 | return -EINVAL; |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 534 | bmv->bmv_entries = 0; |
| 535 | if (bmv->bmv_length == 0) |
| 536 | return 0; |
| 537 | |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 538 | if (iflags & BMV_IF_ATTRFORK) |
| 539 | whichfork = XFS_ATTR_FORK; |
| 540 | else if (iflags & BMV_IF_COWFORK) |
| 541 | whichfork = XFS_COW_FORK; |
| 542 | else |
| 543 | whichfork = XFS_DATA_FORK; |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 544 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 545 | |
| 546 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 547 | switch (whichfork) { |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 548 | case XFS_ATTR_FORK: |
| 549 | if (!XFS_IFORK_Q(ip)) |
| 550 | goto out_unlock_iolock; |
| 551 | |
| 552 | max_len = 1LL << 32; |
| 553 | lock = xfs_ilock_attr_map_shared(ip); |
| 554 | break; |
| 555 | case XFS_COW_FORK: |
| 556 | /* No CoW fork? Just return */ |
| 557 | if (!ifp) |
| 558 | goto out_unlock_iolock; |
| 559 | |
| 560 | if (xfs_get_cowextsz_hint(ip)) |
| 561 | max_len = mp->m_super->s_maxbytes; |
| 562 | else |
| 563 | max_len = XFS_ISIZE(ip); |
| 564 | |
| 565 | lock = XFS_ILOCK_SHARED; |
| 566 | xfs_ilock(ip, lock); |
| 567 | break; |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 568 | case XFS_DATA_FORK: |
Christoph Hellwig | efa70be | 2013-12-18 02:14:39 -0800 | [diff] [blame] | 569 | if (!(iflags & BMV_IF_DELALLOC) && |
| 570 | (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 571 | error = filemap_write_and_wait(VFS_I(ip)->i_mapping); |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 572 | if (error) |
| 573 | goto out_unlock_iolock; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 574 | |
Christoph Hellwig | efa70be | 2013-12-18 02:14:39 -0800 | [diff] [blame] | 575 | /* |
| 576 | * Even after flushing the inode, there can still be |
| 577 | * delalloc blocks on the inode beyond EOF due to |
| 578 | * speculative preallocation. These are not removed |
| 579 | * until the release function is called or the inode |
| 580 | * is inactivated. Hence we cannot assert here that |
| 581 | * ip->i_delayed_blks == 0. |
| 582 | */ |
| 583 | } |
| 584 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 585 | if (xfs_get_extsz_hint(ip) || |
| 586 | (ip->i_d.di_flags & |
| 587 | (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))) |
| 588 | max_len = mp->m_super->s_maxbytes; |
| 589 | else |
| 590 | max_len = XFS_ISIZE(ip); |
| 591 | |
Christoph Hellwig | efa70be | 2013-12-18 02:14:39 -0800 | [diff] [blame] | 592 | lock = xfs_ilock_data_map_shared(ip); |
Darrick J. Wong | f86f403 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 593 | break; |
Christoph Hellwig | efa70be | 2013-12-18 02:14:39 -0800 | [diff] [blame] | 594 | } |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 595 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 596 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 597 | case XFS_DINODE_FMT_EXTENTS: |
| 598 | case XFS_DINODE_FMT_BTREE: |
| 599 | break; |
| 600 | case XFS_DINODE_FMT_LOCAL: |
| 601 | /* Local format inode forks report no extents. */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 602 | goto out_unlock_ilock; |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 603 | default: |
| 604 | error = -EINVAL; |
| 605 | goto out_unlock_ilock; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 606 | } |
| 607 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 608 | if (bmv->bmv_length == -1) { |
| 609 | max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len)); |
| 610 | bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset); |
| 611 | } |
| 612 | |
| 613 | bmv_end = bmv->bmv_offset + bmv->bmv_length; |
| 614 | |
| 615 | first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset); |
| 616 | len = XFS_BB_TO_FSB(mp, bmv->bmv_length); |
| 617 | |
| 618 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 619 | error = xfs_iread_extents(NULL, ip, whichfork); |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 620 | if (error) |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 621 | goto out_unlock_ilock; |
| 622 | } |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 623 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 624 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 625 | /* |
| 626 | * Report a whole-file hole if the delalloc flag is set to |
| 627 | * stay compatible with the old implementation. |
| 628 | */ |
| 629 | if (iflags & BMV_IF_DELALLOC) |
| 630 | xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno, |
| 631 | XFS_B_TO_FSB(mp, XFS_ISIZE(ip))); |
| 632 | goto out_unlock_ilock; |
| 633 | } |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 634 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 635 | while (!xfs_getbmap_full(bmv)) { |
| 636 | xfs_trim_extent(&got, first_bno, len); |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 637 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 638 | /* |
| 639 | * Report an entry for a hole if this extent doesn't directly |
| 640 | * follow the previous one. |
| 641 | */ |
| 642 | if (got.br_startoff > bno) { |
| 643 | xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno, |
| 644 | got.br_startoff); |
| 645 | if (xfs_getbmap_full(bmv)) |
| 646 | break; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 647 | } |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 648 | |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 649 | /* |
| 650 | * In order to report shared extents accurately, we report each |
| 651 | * distinct shared / unshared part of a single bmbt record with |
| 652 | * an individual getbmapx record. |
| 653 | */ |
| 654 | bno = got.br_startoff + got.br_blockcount; |
| 655 | rec = got; |
| 656 | do { |
| 657 | error = xfs_getbmap_report_one(ip, bmv, out, bmv_end, |
| 658 | &rec); |
| 659 | if (error || xfs_getbmap_full(bmv)) |
| 660 | goto out_unlock_ilock; |
| 661 | } while (xfs_getbmap_next_rec(&rec, bno)); |
| 662 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 663 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 664 | xfs_fileoff_t end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip)); |
| 665 | |
| 666 | out[bmv->bmv_entries - 1].bmv_oflags |= BMV_OF_LAST; |
| 667 | |
| 668 | if (whichfork != XFS_ATTR_FORK && bno < end && |
| 669 | !xfs_getbmap_full(bmv)) { |
| 670 | xfs_getbmap_report_hole(ip, bmv, out, bmv_end, |
| 671 | bno, end); |
| 672 | } |
| 673 | break; |
| 674 | } |
| 675 | |
| 676 | if (bno >= first_bno + len) |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | out_unlock_ilock: |
Christoph Hellwig | 01f4f32 | 2013-12-06 12:30:08 -0800 | [diff] [blame] | 681 | xfs_iunlock(ip, lock); |
Christoph Hellwig | abbf9e8 | 2017-10-17 14:16:18 -0700 | [diff] [blame] | 682 | out_unlock_iolock: |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 683 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 684 | return error; |
| 685 | } |
| 686 | |
| 687 | /* |
Christoph Hellwig | e2ac836 | 2018-06-21 23:24:38 -0700 | [diff] [blame] | 688 | * Dead simple method of punching delalyed allocation blocks from a range in |
| 689 | * the inode. This will always punch out both the start and end blocks, even |
| 690 | * if the ranges only partially overlap them, so it is up to the caller to |
| 691 | * ensure that partial blocks are not passed in. |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 692 | */ |
| 693 | int |
| 694 | xfs_bmap_punch_delalloc_range( |
| 695 | struct xfs_inode *ip, |
| 696 | xfs_fileoff_t start_fsb, |
| 697 | xfs_fileoff_t length) |
| 698 | { |
Christoph Hellwig | e2ac836 | 2018-06-21 23:24:38 -0700 | [diff] [blame] | 699 | struct xfs_ifork *ifp = &ip->i_df; |
| 700 | xfs_fileoff_t end_fsb = start_fsb + length; |
| 701 | struct xfs_bmbt_irec got, del; |
| 702 | struct xfs_iext_cursor icur; |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 703 | int error = 0; |
| 704 | |
Christoph Hellwig | d438017 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 705 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | e2ac836 | 2018-06-21 23:24:38 -0700 | [diff] [blame] | 706 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 707 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 708 | if (error) |
Christoph Hellwig | d438017 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 709 | goto out_unlock; |
Christoph Hellwig | e2ac836 | 2018-06-21 23:24:38 -0700 | [diff] [blame] | 710 | } |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 711 | |
Christoph Hellwig | e2ac836 | 2018-06-21 23:24:38 -0700 | [diff] [blame] | 712 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got)) |
Christoph Hellwig | d438017 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 713 | goto out_unlock; |
Christoph Hellwig | e2ac836 | 2018-06-21 23:24:38 -0700 | [diff] [blame] | 714 | |
| 715 | while (got.br_startoff + got.br_blockcount > start_fsb) { |
| 716 | del = got; |
| 717 | xfs_trim_extent(&del, start_fsb, length); |
| 718 | |
| 719 | /* |
| 720 | * A delete can push the cursor forward. Step back to the |
| 721 | * previous extent on non-delalloc or extents outside the |
| 722 | * target range. |
| 723 | */ |
| 724 | if (!del.br_blockcount || |
| 725 | !isnullstartblock(del.br_startblock)) { |
| 726 | if (!xfs_iext_prev_extent(ifp, &icur, &got)) |
| 727 | break; |
| 728 | continue; |
| 729 | } |
| 730 | |
| 731 | error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur, |
| 732 | &got, &del); |
| 733 | if (error || !xfs_iext_get_extent(ifp, &icur, &got)) |
| 734 | break; |
| 735 | } |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 736 | |
Christoph Hellwig | d438017 | 2018-07-11 22:25:57 -0700 | [diff] [blame] | 737 | out_unlock: |
| 738 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 739 | return error; |
| 740 | } |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 741 | |
| 742 | /* |
| 743 | * Test whether it is appropriate to check an inode for and free post EOF |
| 744 | * blocks. The 'force' parameter determines whether we should also consider |
| 745 | * regular files that are marked preallocated or append-only. |
| 746 | */ |
| 747 | bool |
| 748 | xfs_can_free_eofblocks(struct xfs_inode *ip, bool force) |
| 749 | { |
| 750 | /* prealloc/delalloc exists only on regular files */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 751 | if (!S_ISREG(VFS_I(ip)->i_mode)) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 752 | return false; |
| 753 | |
| 754 | /* |
| 755 | * Zero sized files with no cached pages and delalloc blocks will not |
| 756 | * have speculative prealloc/delalloc blocks to remove. |
| 757 | */ |
| 758 | if (VFS_I(ip)->i_size == 0 && |
Dave Chinner | 2667c6f | 2014-08-04 13:23:15 +1000 | [diff] [blame] | 759 | VFS_I(ip)->i_mapping->nrpages == 0 && |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 760 | ip->i_delayed_blks == 0) |
| 761 | return false; |
| 762 | |
| 763 | /* If we haven't read in the extent list, then don't do it now. */ |
| 764 | if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) |
| 765 | return false; |
| 766 | |
| 767 | /* |
| 768 | * Do not free real preallocated or append-only files unless the file |
| 769 | * has delalloc blocks and we are forced to remove them. |
| 770 | */ |
| 771 | if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) |
| 772 | if (!force || ip->i_delayed_blks == 0) |
| 773 | return false; |
| 774 | |
| 775 | return true; |
| 776 | } |
| 777 | |
| 778 | /* |
Brian Foster | 3b4683c | 2017-04-11 10:50:05 -0700 | [diff] [blame] | 779 | * This is called to free any blocks beyond eof. The caller must hold |
| 780 | * IOLOCK_EXCL unless we are in the inode reclaim path and have the only |
| 781 | * reference to the inode. |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 782 | */ |
| 783 | int |
| 784 | xfs_free_eofblocks( |
Brian Foster | a36b926 | 2017-01-27 23:22:55 -0800 | [diff] [blame] | 785 | struct xfs_inode *ip) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 786 | { |
Brian Foster | a36b926 | 2017-01-27 23:22:55 -0800 | [diff] [blame] | 787 | struct xfs_trans *tp; |
| 788 | int error; |
| 789 | xfs_fileoff_t end_fsb; |
| 790 | xfs_fileoff_t last_fsb; |
| 791 | xfs_filblks_t map_len; |
| 792 | int nimaps; |
| 793 | struct xfs_bmbt_irec imap; |
| 794 | struct xfs_mount *mp = ip->i_mount; |
| 795 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 796 | /* |
| 797 | * Figure out if there are any blocks beyond the end |
| 798 | * of the file. If not, then there is nothing to do. |
| 799 | */ |
| 800 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip)); |
| 801 | last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); |
| 802 | if (last_fsb <= end_fsb) |
| 803 | return 0; |
| 804 | map_len = last_fsb - end_fsb; |
| 805 | |
| 806 | nimaps = 1; |
| 807 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 808 | error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0); |
| 809 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 810 | |
Brian Foster | a36b926 | 2017-01-27 23:22:55 -0800 | [diff] [blame] | 811 | /* |
| 812 | * If there are blocks after the end of file, truncate the file to its |
| 813 | * current size to free them up. |
| 814 | */ |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 815 | if (!error && (nimaps != 0) && |
| 816 | (imap.br_startblock != HOLESTARTBLOCK || |
| 817 | ip->i_delayed_blks)) { |
| 818 | /* |
| 819 | * Attach the dquots to the inode up front. |
| 820 | */ |
Darrick J. Wong | c14cfcc | 2018-05-04 15:30:21 -0700 | [diff] [blame] | 821 | error = xfs_qm_dqattach(ip); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 822 | if (error) |
| 823 | return error; |
| 824 | |
Brian Foster | e4229d6b | 2017-01-27 23:22:57 -0800 | [diff] [blame] | 825 | /* wait on dio to ensure i_size has settled */ |
| 826 | inode_dio_wait(VFS_I(ip)); |
| 827 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 828 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, |
| 829 | &tp); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 830 | if (error) { |
| 831 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 832 | return error; |
| 833 | } |
| 834 | |
| 835 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 836 | xfs_trans_ijoin(tp, ip, 0); |
| 837 | |
| 838 | /* |
| 839 | * Do not update the on-disk file size. If we update the |
| 840 | * on-disk file size and then the system crashes before the |
| 841 | * contents of the file are flushed to disk then the files |
| 842 | * may be full of holes (ie NULL files bug). |
| 843 | */ |
Brian Foster | 4e52933 | 2018-05-10 09:35:42 -0700 | [diff] [blame] | 844 | error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK, |
| 845 | XFS_ISIZE(ip), XFS_BMAPI_NODISCARD); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 846 | if (error) { |
| 847 | /* |
| 848 | * If we get an error at this point we simply don't |
| 849 | * bother truncating the file. |
| 850 | */ |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 851 | xfs_trans_cancel(tp); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 852 | } else { |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 853 | error = xfs_trans_commit(tp); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 854 | if (!error) |
| 855 | xfs_inode_clear_eofblocks_tag(ip); |
| 856 | } |
| 857 | |
| 858 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 859 | } |
| 860 | return error; |
| 861 | } |
| 862 | |
Christoph Hellwig | 83aee9e | 2013-10-12 00:55:07 -0700 | [diff] [blame] | 863 | int |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 864 | xfs_alloc_file_space( |
Christoph Hellwig | 83aee9e | 2013-10-12 00:55:07 -0700 | [diff] [blame] | 865 | struct xfs_inode *ip, |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 866 | xfs_off_t offset, |
| 867 | xfs_off_t len, |
Christoph Hellwig | 5f8aca8 | 2013-10-12 00:55:06 -0700 | [diff] [blame] | 868 | int alloc_type) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 869 | { |
| 870 | xfs_mount_t *mp = ip->i_mount; |
| 871 | xfs_off_t count; |
| 872 | xfs_filblks_t allocated_fsb; |
| 873 | xfs_filblks_t allocatesize_fsb; |
| 874 | xfs_extlen_t extsz, temp; |
| 875 | xfs_fileoff_t startoffset_fsb; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 876 | int nimaps; |
| 877 | int quota_flag; |
| 878 | int rt; |
| 879 | xfs_trans_t *tp; |
| 880 | xfs_bmbt_irec_t imaps[1], *imapp; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 881 | uint qblocks, resblks, resrtextents; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 882 | int error; |
| 883 | |
| 884 | trace_xfs_alloc_file_space(ip); |
| 885 | |
| 886 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 887 | return -EIO; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 888 | |
Darrick J. Wong | c14cfcc | 2018-05-04 15:30:21 -0700 | [diff] [blame] | 889 | error = xfs_qm_dqattach(ip); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 890 | if (error) |
| 891 | return error; |
| 892 | |
| 893 | if (len <= 0) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 894 | return -EINVAL; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 895 | |
| 896 | rt = XFS_IS_REALTIME_INODE(ip); |
| 897 | extsz = xfs_get_extsz_hint(ip); |
| 898 | |
| 899 | count = len; |
| 900 | imapp = &imaps[0]; |
| 901 | nimaps = 1; |
| 902 | startoffset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 903 | allocatesize_fsb = XFS_B_TO_FSB(mp, count); |
| 904 | |
| 905 | /* |
| 906 | * Allocate file space until done or until there is an error |
| 907 | */ |
| 908 | while (allocatesize_fsb && !error) { |
| 909 | xfs_fileoff_t s, e; |
| 910 | |
| 911 | /* |
| 912 | * Determine space reservations for data/realtime. |
| 913 | */ |
| 914 | if (unlikely(extsz)) { |
| 915 | s = startoffset_fsb; |
| 916 | do_div(s, extsz); |
| 917 | s *= extsz; |
| 918 | e = startoffset_fsb + allocatesize_fsb; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 919 | div_u64_rem(startoffset_fsb, extsz, &temp); |
| 920 | if (temp) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 921 | e += temp; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 922 | div_u64_rem(e, extsz, &temp); |
| 923 | if (temp) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 924 | e += extsz - temp; |
| 925 | } else { |
| 926 | s = 0; |
| 927 | e = allocatesize_fsb; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * The transaction reservation is limited to a 32-bit block |
| 932 | * count, hence we need to limit the number of blocks we are |
| 933 | * trying to reserve to avoid an overflow. We can't allocate |
| 934 | * more than @nimaps extents, and an extent is limited on disk |
| 935 | * to MAXEXTLEN (21 bits), so use that to enforce the limit. |
| 936 | */ |
| 937 | resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps)); |
| 938 | if (unlikely(rt)) { |
| 939 | resrtextents = qblocks = resblks; |
| 940 | resrtextents /= mp->m_sb.sb_rextsize; |
| 941 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 942 | quota_flag = XFS_QMOPT_RES_RTBLKS; |
| 943 | } else { |
| 944 | resrtextents = 0; |
| 945 | resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks); |
| 946 | quota_flag = XFS_QMOPT_RES_REGBLKS; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Allocate and setup the transaction. |
| 951 | */ |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 952 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, |
| 953 | resrtextents, 0, &tp); |
| 954 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 955 | /* |
| 956 | * Check for running out of space |
| 957 | */ |
| 958 | if (error) { |
| 959 | /* |
| 960 | * Free the transaction structure. |
| 961 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 962 | ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp)); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 963 | break; |
| 964 | } |
| 965 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 966 | error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, |
| 967 | 0, quota_flag); |
| 968 | if (error) |
| 969 | goto error1; |
| 970 | |
| 971 | xfs_trans_ijoin(tp, ip, 0); |
| 972 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 973 | error = xfs_bmapi_write(tp, ip, startoffset_fsb, |
Brian Foster | a7beabe | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 974 | allocatesize_fsb, alloc_type, resblks, |
| 975 | imapp, &nimaps); |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 976 | if (error) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 977 | goto error0; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 978 | |
| 979 | /* |
| 980 | * Complete the transaction |
| 981 | */ |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 982 | error = xfs_trans_commit(tp); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 983 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 984 | if (error) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 985 | break; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 986 | |
| 987 | allocated_fsb = imapp->br_blockcount; |
| 988 | |
| 989 | if (nimaps == 0) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 990 | error = -ENOSPC; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 991 | break; |
| 992 | } |
| 993 | |
| 994 | startoffset_fsb += allocated_fsb; |
| 995 | allocatesize_fsb -= allocated_fsb; |
| 996 | } |
| 997 | |
| 998 | return error; |
| 999 | |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1000 | error0: /* unlock inode, unreserve quota blocks, cancel trans */ |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1001 | xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag); |
| 1002 | |
| 1003 | error1: /* Just cancel transaction */ |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1004 | xfs_trans_cancel(tp); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1005 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1006 | return error; |
| 1007 | } |
| 1008 | |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1009 | static int |
| 1010 | xfs_unmap_extent( |
| 1011 | struct xfs_inode *ip, |
| 1012 | xfs_fileoff_t startoffset_fsb, |
| 1013 | xfs_filblks_t len_fsb, |
| 1014 | int *done) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1015 | { |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1016 | struct xfs_mount *mp = ip->i_mount; |
| 1017 | struct xfs_trans *tp; |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1018 | uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 1019 | int error; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1020 | |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1021 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp); |
| 1022 | if (error) { |
| 1023 | ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp)); |
| 1024 | return error; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1025 | } |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1026 | |
| 1027 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1028 | error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, ip->i_gdquot, |
| 1029 | ip->i_pdquot, resblks, 0, XFS_QMOPT_RES_REGBLKS); |
| 1030 | if (error) |
| 1031 | goto out_trans_cancel; |
| 1032 | |
| 1033 | xfs_trans_ijoin(tp, ip, 0); |
| 1034 | |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 1035 | error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done); |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1036 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1037 | goto out_trans_cancel; |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1038 | |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1039 | error = xfs_trans_commit(tp); |
| 1040 | out_unlock: |
| 1041 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1042 | return error; |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1043 | |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1044 | out_trans_cancel: |
| 1045 | xfs_trans_cancel(tp); |
| 1046 | goto out_unlock; |
| 1047 | } |
| 1048 | |
| 1049 | static int |
| 1050 | xfs_adjust_extent_unmap_boundaries( |
| 1051 | struct xfs_inode *ip, |
| 1052 | xfs_fileoff_t *startoffset_fsb, |
| 1053 | xfs_fileoff_t *endoffset_fsb) |
| 1054 | { |
| 1055 | struct xfs_mount *mp = ip->i_mount; |
| 1056 | struct xfs_bmbt_irec imap; |
| 1057 | int nimap, error; |
| 1058 | xfs_extlen_t mod = 0; |
| 1059 | |
| 1060 | nimap = 1; |
| 1061 | error = xfs_bmapi_read(ip, *startoffset_fsb, 1, &imap, &nimap, 0); |
| 1062 | if (error) |
| 1063 | return error; |
| 1064 | |
| 1065 | if (nimap && imap.br_startblock != HOLESTARTBLOCK) { |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1066 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 1067 | div_u64_rem(imap.br_startblock, mp->m_sb.sb_rextsize, &mod); |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1068 | if (mod) |
| 1069 | *startoffset_fsb += mp->m_sb.sb_rextsize - mod; |
| 1070 | } |
| 1071 | |
| 1072 | nimap = 1; |
| 1073 | error = xfs_bmapi_read(ip, *endoffset_fsb - 1, 1, &imap, &nimap, 0); |
| 1074 | if (error) |
| 1075 | return error; |
| 1076 | |
| 1077 | if (nimap && imap.br_startblock != HOLESTARTBLOCK) { |
| 1078 | ASSERT(imap.br_startblock != DELAYSTARTBLOCK); |
| 1079 | mod++; |
| 1080 | if (mod && mod != mp->m_sb.sb_rextsize) |
| 1081 | *endoffset_fsb -= mod; |
| 1082 | } |
| 1083 | |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | static int |
| 1088 | xfs_flush_unmap_range( |
| 1089 | struct xfs_inode *ip, |
| 1090 | xfs_off_t offset, |
| 1091 | xfs_off_t len) |
| 1092 | { |
| 1093 | struct xfs_mount *mp = ip->i_mount; |
| 1094 | struct inode *inode = VFS_I(ip); |
| 1095 | xfs_off_t rounding, start, end; |
| 1096 | int error; |
| 1097 | |
| 1098 | /* wait for the completion of any pending DIOs */ |
| 1099 | inode_dio_wait(inode); |
| 1100 | |
| 1101 | rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_SIZE); |
| 1102 | start = round_down(offset, rounding); |
| 1103 | end = round_up(offset + len, rounding) - 1; |
| 1104 | |
| 1105 | error = filemap_write_and_wait_range(inode->i_mapping, start, end); |
| 1106 | if (error) |
| 1107 | return error; |
| 1108 | truncate_pagecache_range(inode, start, end); |
| 1109 | return 0; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1110 | } |
| 1111 | |
Christoph Hellwig | 83aee9e | 2013-10-12 00:55:07 -0700 | [diff] [blame] | 1112 | int |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1113 | xfs_free_file_space( |
Christoph Hellwig | 83aee9e | 2013-10-12 00:55:07 -0700 | [diff] [blame] | 1114 | struct xfs_inode *ip, |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1115 | xfs_off_t offset, |
Christoph Hellwig | 5f8aca8 | 2013-10-12 00:55:06 -0700 | [diff] [blame] | 1116 | xfs_off_t len) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1117 | { |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1118 | struct xfs_mount *mp = ip->i_mount; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1119 | xfs_fileoff_t startoffset_fsb; |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1120 | xfs_fileoff_t endoffset_fsb; |
Christoph Hellwig | 3c2bdc9 | 2016-06-21 10:02:23 +1000 | [diff] [blame] | 1121 | int done = 0, error; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1122 | |
| 1123 | trace_xfs_free_file_space(ip); |
| 1124 | |
Darrick J. Wong | c14cfcc | 2018-05-04 15:30:21 -0700 | [diff] [blame] | 1125 | error = xfs_qm_dqattach(ip); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1126 | if (error) |
| 1127 | return error; |
| 1128 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1129 | if (len <= 0) /* if nothing being freed */ |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1130 | return 0; |
| 1131 | |
| 1132 | error = xfs_flush_unmap_range(ip, offset, len); |
| 1133 | if (error) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1134 | return error; |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1135 | |
| 1136 | startoffset_fsb = XFS_B_TO_FSB(mp, offset); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1137 | endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len); |
| 1138 | |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1139 | /* |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1140 | * Need to zero the stuff we're not freeing, on disk. If it's a RT file |
| 1141 | * and we can't use unwritten extents then we actually need to ensure |
| 1142 | * to zero the whole extent, otherwise we just need to take of block |
| 1143 | * boundaries, and xfs_bunmapi will handle the rest. |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1144 | */ |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1145 | if (XFS_IS_REALTIME_INODE(ip) && |
| 1146 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
| 1147 | error = xfs_adjust_extent_unmap_boundaries(ip, &startoffset_fsb, |
| 1148 | &endoffset_fsb); |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1149 | if (error) |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1150 | return error; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1151 | } |
Christoph Hellwig | bdb0d04 | 2016-06-21 10:00:55 +1000 | [diff] [blame] | 1152 | |
Christoph Hellwig | 3c2bdc9 | 2016-06-21 10:02:23 +1000 | [diff] [blame] | 1153 | if (endoffset_fsb > startoffset_fsb) { |
| 1154 | while (!done) { |
| 1155 | error = xfs_unmap_extent(ip, startoffset_fsb, |
| 1156 | endoffset_fsb - startoffset_fsb, &done); |
| 1157 | if (error) |
| 1158 | return error; |
| 1159 | } |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /* |
Christoph Hellwig | 3c2bdc9 | 2016-06-21 10:02:23 +1000 | [diff] [blame] | 1163 | * Now that we've unmap all full blocks we'll have to zero out any |
Christoph Hellwig | f5c54717 | 2018-03-13 23:15:32 -0700 | [diff] [blame] | 1164 | * partial block at the beginning and/or end. iomap_zero_range is smart |
| 1165 | * enough to skip any holes, including those we just created, but we |
| 1166 | * must take care not to zero beyond EOF and enlarge i_size. |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1167 | */ |
Calvin Owens | 3dd09d5 | 2017-04-03 12:22:29 -0700 | [diff] [blame] | 1168 | if (offset >= XFS_ISIZE(ip)) |
| 1169 | return 0; |
Calvin Owens | 3dd09d5 | 2017-04-03 12:22:29 -0700 | [diff] [blame] | 1170 | if (offset + len > XFS_ISIZE(ip)) |
| 1171 | len = XFS_ISIZE(ip) - offset; |
Darrick J. Wong | e53c4b5 | 2018-06-21 23:26:58 -0700 | [diff] [blame] | 1172 | error = iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops); |
| 1173 | if (error) |
| 1174 | return error; |
| 1175 | |
| 1176 | /* |
| 1177 | * If we zeroed right up to EOF and EOF straddles a page boundary we |
| 1178 | * must make sure that the post-EOF area is also zeroed because the |
| 1179 | * page could be mmap'd and iomap_zero_range doesn't do that for us. |
| 1180 | * Writeback of the eof page will do this, albeit clumsily. |
| 1181 | */ |
| 1182 | if (offset + len >= XFS_ISIZE(ip) && ((offset + len) & PAGE_MASK)) { |
| 1183 | error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, |
| 1184 | (offset + len) & ~PAGE_MASK, LLONG_MAX); |
| 1185 | } |
| 1186 | |
| 1187 | return error; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1188 | } |
| 1189 | |
Brian Foster | 5d11fb4 | 2014-10-30 10:35:11 +1100 | [diff] [blame] | 1190 | /* |
| 1191 | * Preallocate and zero a range of a file. This mechanism has the allocation |
| 1192 | * semantics of fallocate and in addition converts data in the range to zeroes. |
| 1193 | */ |
Christoph Hellwig | 865e944 | 2013-10-12 00:55:08 -0700 | [diff] [blame] | 1194 | int |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1195 | xfs_zero_file_space( |
| 1196 | struct xfs_inode *ip, |
| 1197 | xfs_off_t offset, |
Christoph Hellwig | 5f8aca8 | 2013-10-12 00:55:06 -0700 | [diff] [blame] | 1198 | xfs_off_t len) |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1199 | { |
| 1200 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | 5d11fb4 | 2014-10-30 10:35:11 +1100 | [diff] [blame] | 1201 | uint blksize; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1202 | int error; |
| 1203 | |
Dave Chinner | 897b73b | 2014-04-14 18:15:11 +1000 | [diff] [blame] | 1204 | trace_xfs_zero_file_space(ip); |
| 1205 | |
Brian Foster | 5d11fb4 | 2014-10-30 10:35:11 +1100 | [diff] [blame] | 1206 | blksize = 1 << mp->m_sb.sb_blocklog; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1207 | |
| 1208 | /* |
Brian Foster | 5d11fb4 | 2014-10-30 10:35:11 +1100 | [diff] [blame] | 1209 | * Punch a hole and prealloc the range. We use hole punch rather than |
| 1210 | * unwritten extent conversion for two reasons: |
| 1211 | * |
| 1212 | * 1.) Hole punch handles partial block zeroing for us. |
| 1213 | * |
| 1214 | * 2.) If prealloc returns ENOSPC, the file range is still zero-valued |
| 1215 | * by virtue of the hole punch. |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1216 | */ |
Brian Foster | 5d11fb4 | 2014-10-30 10:35:11 +1100 | [diff] [blame] | 1217 | error = xfs_free_file_space(ip, offset, len); |
| 1218 | if (error) |
| 1219 | goto out; |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1220 | |
Brian Foster | 5d11fb4 | 2014-10-30 10:35:11 +1100 | [diff] [blame] | 1221 | error = xfs_alloc_file_space(ip, round_down(offset, blksize), |
| 1222 | round_up(offset + len, blksize) - |
| 1223 | round_down(offset, blksize), |
| 1224 | XFS_BMAPI_PREALLOC); |
Christoph Hellwig | 5f8aca8 | 2013-10-12 00:55:06 -0700 | [diff] [blame] | 1225 | out: |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 1226 | return error; |
| 1227 | |
| 1228 | } |
| 1229 | |
kbuild test robot | 72c1a73 | 2015-04-13 11:25:04 +1000 | [diff] [blame] | 1230 | static int |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1231 | xfs_prepare_shift( |
| 1232 | struct xfs_inode *ip, |
| 1233 | loff_t offset) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 1234 | { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 1235 | int error; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 1236 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 1237 | /* |
| 1238 | * Trim eofblocks to avoid shifting uninitialized post-eof preallocation |
| 1239 | * into the accessible region of the file. |
| 1240 | */ |
Brian Foster | 41b9d72 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 1241 | if (xfs_can_free_eofblocks(ip, true)) { |
Brian Foster | a36b926 | 2017-01-27 23:22:55 -0800 | [diff] [blame] | 1242 | error = xfs_free_eofblocks(ip); |
Brian Foster | 41b9d72 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 1243 | if (error) |
| 1244 | return error; |
| 1245 | } |
Dave Chinner | 1669a8c | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 1246 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 1247 | /* |
| 1248 | * Writeback and invalidate cache for the remainder of the file as we're |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 1249 | * about to shift down every extent from offset to EOF. |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 1250 | */ |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1251 | error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, offset, -1); |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 1252 | if (error) |
| 1253 | return error; |
| 1254 | error = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1255 | offset >> PAGE_SHIFT, -1); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 1256 | if (error) |
| 1257 | return error; |
| 1258 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 1259 | /* |
Darrick J. Wong | 3af423b | 2017-09-18 09:41:17 -0700 | [diff] [blame] | 1260 | * Clean out anything hanging around in the cow fork now that |
| 1261 | * we've flushed all the dirty data out to disk to avoid having |
| 1262 | * CoW extents at the wrong offsets. |
| 1263 | */ |
Christoph Hellwig | 51d6269 | 2018-07-17 16:51:51 -0700 | [diff] [blame] | 1264 | if (xfs_inode_has_cow_data(ip)) { |
Darrick J. Wong | 3af423b | 2017-09-18 09:41:17 -0700 | [diff] [blame] | 1265 | error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF, |
| 1266 | true); |
| 1267 | if (error) |
| 1268 | return error; |
| 1269 | } |
| 1270 | |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1271 | return 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | /* |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 1275 | * xfs_collapse_file_space() |
| 1276 | * This routine frees disk space and shift extent for the given file. |
| 1277 | * The first thing we do is to free data blocks in the specified range |
| 1278 | * by calling xfs_free_file_space(). It would also sync dirty data |
| 1279 | * and invalidate page cache over the region on which collapse range |
| 1280 | * is working. And Shift extent records to the left to cover a hole. |
| 1281 | * RETURNS: |
| 1282 | * 0 on success |
| 1283 | * errno on error |
| 1284 | * |
| 1285 | */ |
| 1286 | int |
| 1287 | xfs_collapse_file_space( |
| 1288 | struct xfs_inode *ip, |
| 1289 | xfs_off_t offset, |
| 1290 | xfs_off_t len) |
| 1291 | { |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1292 | struct xfs_mount *mp = ip->i_mount; |
| 1293 | struct xfs_trans *tp; |
| 1294 | int error; |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1295 | xfs_fileoff_t next_fsb = XFS_B_TO_FSB(mp, offset + len); |
| 1296 | xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len); |
| 1297 | uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 1298 | bool done = false; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 1299 | |
| 1300 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
Christoph Hellwig | 9ad1a23a | 2017-10-23 16:32:38 -0700 | [diff] [blame] | 1301 | ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL)); |
| 1302 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 1303 | trace_xfs_collapse_file_space(ip); |
| 1304 | |
| 1305 | error = xfs_free_file_space(ip, offset, len); |
| 1306 | if (error) |
| 1307 | return error; |
| 1308 | |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1309 | error = xfs_prepare_shift(ip, offset); |
| 1310 | if (error) |
| 1311 | return error; |
| 1312 | |
| 1313 | while (!error && !done) { |
| 1314 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, |
| 1315 | &tp); |
| 1316 | if (error) |
| 1317 | break; |
| 1318 | |
| 1319 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1320 | error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, |
| 1321 | ip->i_gdquot, ip->i_pdquot, resblks, 0, |
| 1322 | XFS_QMOPT_RES_REGBLKS); |
| 1323 | if (error) |
| 1324 | goto out_trans_cancel; |
| 1325 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 1326 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 1327 | error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb, |
Brian Foster | 333f950 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 1328 | &done); |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1329 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1330 | goto out_trans_cancel; |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1331 | |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1332 | error = xfs_trans_commit(tp); |
| 1333 | } |
| 1334 | |
| 1335 | return error; |
| 1336 | |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1337 | out_trans_cancel: |
| 1338 | xfs_trans_cancel(tp); |
| 1339 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * xfs_insert_file_space() |
| 1344 | * This routine create hole space by shifting extents for the given file. |
| 1345 | * The first thing we do is to sync dirty data and invalidate page cache |
| 1346 | * over the region on which insert range is working. And split an extent |
| 1347 | * to two extents at given offset by calling xfs_bmap_split_extent. |
| 1348 | * And shift all extent records which are laying between [offset, |
| 1349 | * last allocated extent] to the right to reserve hole range. |
| 1350 | * RETURNS: |
| 1351 | * 0 on success |
| 1352 | * errno on error |
| 1353 | */ |
| 1354 | int |
| 1355 | xfs_insert_file_space( |
| 1356 | struct xfs_inode *ip, |
| 1357 | loff_t offset, |
| 1358 | loff_t len) |
| 1359 | { |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1360 | struct xfs_mount *mp = ip->i_mount; |
| 1361 | struct xfs_trans *tp; |
| 1362 | int error; |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1363 | xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, offset); |
| 1364 | xfs_fileoff_t next_fsb = NULLFSBLOCK; |
| 1365 | xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 1366 | bool done = false; |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1367 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 1368 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
Christoph Hellwig | 9ad1a23a | 2017-10-23 16:32:38 -0700 | [diff] [blame] | 1369 | ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL)); |
| 1370 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 1371 | trace_xfs_insert_file_space(ip); |
| 1372 | |
Darrick J. Wong | f62cb48 | 2018-06-21 23:26:57 -0700 | [diff] [blame] | 1373 | error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb); |
| 1374 | if (error) |
| 1375 | return error; |
| 1376 | |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1377 | error = xfs_prepare_shift(ip, offset); |
| 1378 | if (error) |
| 1379 | return error; |
| 1380 | |
| 1381 | /* |
| 1382 | * The extent shifting code works on extent granularity. So, if stop_fsb |
| 1383 | * is not the starting block of extent, we need to split the extent at |
| 1384 | * stop_fsb. |
| 1385 | */ |
| 1386 | error = xfs_bmap_split_extent(ip, stop_fsb); |
| 1387 | if (error) |
| 1388 | return error; |
| 1389 | |
| 1390 | while (!error && !done) { |
| 1391 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, |
| 1392 | &tp); |
| 1393 | if (error) |
| 1394 | break; |
| 1395 | |
| 1396 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1397 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 1398 | error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb, |
Brian Foster | 333f950 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 1399 | &done, stop_fsb); |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1400 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1401 | goto out_trans_cancel; |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1402 | |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1403 | error = xfs_trans_commit(tp); |
| 1404 | } |
| 1405 | |
| 1406 | return error; |
| 1407 | |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1408 | out_trans_cancel: |
Christoph Hellwig | 4ed36c6 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 1409 | xfs_trans_cancel(tp); |
| 1410 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | /* |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1414 | * We need to check that the format of the data fork in the temporary inode is |
| 1415 | * valid for the target inode before doing the swap. This is not a problem with |
| 1416 | * attr1 because of the fixed fork offset, but attr2 has a dynamically sized |
| 1417 | * data fork depending on the space the attribute fork is taking so we can get |
| 1418 | * invalid formats on the target inode. |
| 1419 | * |
| 1420 | * E.g. target has space for 7 extents in extent format, temp inode only has |
| 1421 | * space for 6. If we defragment down to 7 extents, then the tmp format is a |
| 1422 | * btree, but when swapped it needs to be in extent format. Hence we can't just |
| 1423 | * blindly swap data forks on attr2 filesystems. |
| 1424 | * |
| 1425 | * Note that we check the swap in both directions so that we don't end up with |
| 1426 | * a corrupt temporary inode, either. |
| 1427 | * |
| 1428 | * Note that fixing the way xfs_fsr sets up the attribute fork in the source |
| 1429 | * inode will prevent this situation from occurring, so all we do here is |
| 1430 | * reject and log the attempt. basically we are putting the responsibility on |
| 1431 | * userspace to get this right. |
| 1432 | */ |
| 1433 | static int |
| 1434 | xfs_swap_extents_check_format( |
Darrick J. Wong | e06259a | 2016-10-03 09:11:52 -0700 | [diff] [blame] | 1435 | struct xfs_inode *ip, /* target inode */ |
| 1436 | struct xfs_inode *tip) /* tmp inode */ |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1437 | { |
| 1438 | |
| 1439 | /* Should never get a local format */ |
| 1440 | if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL || |
| 1441 | tip->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1442 | return -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1443 | |
| 1444 | /* |
| 1445 | * if the target inode has less extents that then temporary inode then |
| 1446 | * why did userspace call us? |
| 1447 | */ |
| 1448 | if (ip->i_d.di_nextents < tip->i_d.di_nextents) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1449 | return -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1450 | |
| 1451 | /* |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1452 | * If we have to use the (expensive) rmap swap method, we can |
| 1453 | * handle any number of extents and any format. |
| 1454 | */ |
| 1455 | if (xfs_sb_version_hasrmapbt(&ip->i_mount->m_sb)) |
| 1456 | return 0; |
| 1457 | |
| 1458 | /* |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1459 | * if the target inode is in extent form and the temp inode is in btree |
| 1460 | * form then we will end up with the target inode in the wrong format |
| 1461 | * as we already know there are less extents in the temp inode. |
| 1462 | */ |
| 1463 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1464 | tip->i_d.di_format == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1465 | return -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1466 | |
| 1467 | /* Check temp in extent form to max in target */ |
| 1468 | if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1469 | XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) > |
| 1470 | XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1471 | return -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1472 | |
| 1473 | /* Check target in extent form to max in temp */ |
| 1474 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1475 | XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) > |
| 1476 | XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1477 | return -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1478 | |
| 1479 | /* |
| 1480 | * If we are in a btree format, check that the temp root block will fit |
| 1481 | * in the target and that it has enough extents to be in btree format |
| 1482 | * in the target. |
| 1483 | * |
| 1484 | * Note that we have to be careful to allow btree->extent conversions |
| 1485 | * (a common defrag case) which will occur when the temp inode is in |
| 1486 | * extent format... |
| 1487 | */ |
| 1488 | if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) { |
Arnd Bergmann | 0cbe48c | 2017-06-14 21:35:34 -0700 | [diff] [blame] | 1489 | if (XFS_IFORK_Q(ip) && |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1490 | XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1491 | return -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1492 | if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <= |
| 1493 | XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1494 | return -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | /* Reciprocal target->temp btree format checks */ |
| 1498 | if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) { |
Arnd Bergmann | 0cbe48c | 2017-06-14 21:35:34 -0700 | [diff] [blame] | 1499 | if (XFS_IFORK_Q(tip) && |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1500 | XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1501 | return -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1502 | if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <= |
| 1503 | XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1504 | return -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
Dave Chinner | 7abbb8f | 2014-09-23 16:20:11 +1000 | [diff] [blame] | 1510 | static int |
Dave Chinner | 4ef897a | 2014-08-04 13:44:08 +1000 | [diff] [blame] | 1511 | xfs_swap_extent_flush( |
| 1512 | struct xfs_inode *ip) |
| 1513 | { |
| 1514 | int error; |
| 1515 | |
| 1516 | error = filemap_write_and_wait(VFS_I(ip)->i_mapping); |
| 1517 | if (error) |
| 1518 | return error; |
| 1519 | truncate_pagecache_range(VFS_I(ip), 0, -1); |
| 1520 | |
| 1521 | /* Verify O_DIRECT for ftmp */ |
| 1522 | if (VFS_I(ip)->i_mapping->nrpages) |
| 1523 | return -EINVAL; |
Dave Chinner | 4ef897a | 2014-08-04 13:44:08 +1000 | [diff] [blame] | 1524 | return 0; |
| 1525 | } |
| 1526 | |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1527 | /* |
| 1528 | * Move extents from one file to another, when rmap is enabled. |
| 1529 | */ |
| 1530 | STATIC int |
| 1531 | xfs_swap_extent_rmap( |
| 1532 | struct xfs_trans **tpp, |
| 1533 | struct xfs_inode *ip, |
| 1534 | struct xfs_inode *tip) |
| 1535 | { |
Brian Foster | 7a7943c | 2018-07-11 22:26:17 -0700 | [diff] [blame] | 1536 | struct xfs_trans *tp = *tpp; |
| 1537 | struct xfs_mount *mp = tp->t_mountp; |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1538 | struct xfs_bmbt_irec irec; |
| 1539 | struct xfs_bmbt_irec uirec; |
| 1540 | struct xfs_bmbt_irec tirec; |
| 1541 | xfs_fileoff_t offset_fsb; |
| 1542 | xfs_fileoff_t end_fsb; |
| 1543 | xfs_filblks_t count_fsb; |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1544 | int error; |
| 1545 | xfs_filblks_t ilen; |
| 1546 | xfs_filblks_t rlen; |
| 1547 | int nimaps; |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 1548 | uint64_t tip_flags2; |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1549 | |
| 1550 | /* |
| 1551 | * If the source file has shared blocks, we must flag the donor |
| 1552 | * file as having shared blocks so that we get the shared-block |
| 1553 | * rmap functions when we go to fix up the rmaps. The flags |
| 1554 | * will be switch for reals later. |
| 1555 | */ |
| 1556 | tip_flags2 = tip->i_d.di_flags2; |
| 1557 | if (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK) |
| 1558 | tip->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK; |
| 1559 | |
| 1560 | offset_fsb = 0; |
| 1561 | end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip))); |
| 1562 | count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb); |
| 1563 | |
| 1564 | while (count_fsb) { |
| 1565 | /* Read extent from the donor file */ |
| 1566 | nimaps = 1; |
| 1567 | error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec, |
| 1568 | &nimaps, 0); |
| 1569 | if (error) |
| 1570 | goto out; |
| 1571 | ASSERT(nimaps == 1); |
| 1572 | ASSERT(tirec.br_startblock != DELAYSTARTBLOCK); |
| 1573 | |
| 1574 | trace_xfs_swap_extent_rmap_remap(tip, &tirec); |
| 1575 | ilen = tirec.br_blockcount; |
| 1576 | |
| 1577 | /* Unmap the old blocks in the source file. */ |
| 1578 | while (tirec.br_blockcount) { |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1579 | ASSERT(tp->t_firstblock == NULLFSBLOCK); |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1580 | trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec); |
| 1581 | |
| 1582 | /* Read extent from the source file */ |
| 1583 | nimaps = 1; |
| 1584 | error = xfs_bmapi_read(ip, tirec.br_startoff, |
| 1585 | tirec.br_blockcount, &irec, |
| 1586 | &nimaps, 0); |
| 1587 | if (error) |
| 1588 | goto out_defer; |
| 1589 | ASSERT(nimaps == 1); |
| 1590 | ASSERT(tirec.br_startoff == irec.br_startoff); |
| 1591 | trace_xfs_swap_extent_rmap_remap_piece(ip, &irec); |
| 1592 | |
| 1593 | /* Trim the extent. */ |
| 1594 | uirec = tirec; |
| 1595 | uirec.br_blockcount = rlen = min_t(xfs_filblks_t, |
| 1596 | tirec.br_blockcount, |
| 1597 | irec.br_blockcount); |
| 1598 | trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec); |
| 1599 | |
| 1600 | /* Remove the mapping from the donor file. */ |
Brian Foster | 7a7943c | 2018-07-11 22:26:17 -0700 | [diff] [blame] | 1601 | error = xfs_bmap_unmap_extent(mp, tp->t_dfops, tip, |
| 1602 | &uirec); |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1603 | if (error) |
| 1604 | goto out_defer; |
| 1605 | |
| 1606 | /* Remove the mapping from the source file. */ |
Brian Foster | 7a7943c | 2018-07-11 22:26:17 -0700 | [diff] [blame] | 1607 | error = xfs_bmap_unmap_extent(mp, tp->t_dfops, ip, |
| 1608 | &irec); |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1609 | if (error) |
| 1610 | goto out_defer; |
| 1611 | |
| 1612 | /* Map the donor file's blocks into the source file. */ |
Brian Foster | 7a7943c | 2018-07-11 22:26:17 -0700 | [diff] [blame] | 1613 | error = xfs_bmap_map_extent(mp, tp->t_dfops, ip, |
| 1614 | &uirec); |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1615 | if (error) |
| 1616 | goto out_defer; |
| 1617 | |
| 1618 | /* Map the source file's blocks into the donor file. */ |
Brian Foster | 7a7943c | 2018-07-11 22:26:17 -0700 | [diff] [blame] | 1619 | error = xfs_bmap_map_extent(mp, tp->t_dfops, tip, |
| 1620 | &irec); |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1621 | if (error) |
| 1622 | goto out_defer; |
| 1623 | |
Brian Foster | 9e28a24 | 2018-07-24 13:43:15 -0700 | [diff] [blame] | 1624 | error = xfs_defer_finish(tpp); |
Brian Foster | 7a7943c | 2018-07-11 22:26:17 -0700 | [diff] [blame] | 1625 | tp = *tpp; |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1626 | if (error) |
Brian Foster | 9b1f4e9 | 2018-08-01 07:20:33 -0700 | [diff] [blame^] | 1627 | goto out; |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1628 | |
| 1629 | tirec.br_startoff += rlen; |
| 1630 | if (tirec.br_startblock != HOLESTARTBLOCK && |
| 1631 | tirec.br_startblock != DELAYSTARTBLOCK) |
| 1632 | tirec.br_startblock += rlen; |
| 1633 | tirec.br_blockcount -= rlen; |
| 1634 | } |
| 1635 | |
| 1636 | /* Roll on... */ |
| 1637 | count_fsb -= ilen; |
| 1638 | offset_fsb += ilen; |
| 1639 | } |
| 1640 | |
| 1641 | tip->i_d.di_flags2 = tip_flags2; |
| 1642 | return 0; |
| 1643 | |
| 1644 | out_defer: |
Brian Foster | 9e28a24 | 2018-07-24 13:43:15 -0700 | [diff] [blame] | 1645 | xfs_defer_cancel(tp); |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1646 | out: |
| 1647 | trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_); |
| 1648 | tip->i_d.di_flags2 = tip_flags2; |
| 1649 | return error; |
| 1650 | } |
| 1651 | |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1652 | /* Swap the extents of two files by swapping data forks. */ |
| 1653 | STATIC int |
| 1654 | xfs_swap_extent_forks( |
| 1655 | struct xfs_trans *tp, |
| 1656 | struct xfs_inode *ip, |
| 1657 | struct xfs_inode *tip, |
| 1658 | int *src_log_flags, |
| 1659 | int *target_log_flags) |
| 1660 | { |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 1661 | xfs_filblks_t aforkblks = 0; |
| 1662 | xfs_filblks_t taforkblks = 0; |
| 1663 | xfs_extnum_t junk; |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 1664 | uint64_t tmp; |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1665 | int error; |
| 1666 | |
| 1667 | /* |
| 1668 | * Count the number of extended attribute blocks |
| 1669 | */ |
| 1670 | if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) && |
| 1671 | (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) { |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 1672 | error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk, |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1673 | &aforkblks); |
| 1674 | if (error) |
| 1675 | return error; |
| 1676 | } |
| 1677 | if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) && |
| 1678 | (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) { |
Darrick J. Wong | e7f5d5c | 2017-06-16 11:00:12 -0700 | [diff] [blame] | 1679 | error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk, |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1680 | &taforkblks); |
| 1681 | if (error) |
| 1682 | return error; |
| 1683 | } |
| 1684 | |
| 1685 | /* |
Brian Foster | 6fb10d6 | 2017-08-29 10:08:39 -0700 | [diff] [blame] | 1686 | * Btree format (v3) inodes have the inode number stamped in the bmbt |
| 1687 | * block headers. We can't start changing the bmbt blocks until the |
| 1688 | * inode owner change is logged so recovery does the right thing in the |
| 1689 | * event of a crash. Set the owner change log flags now and leave the |
| 1690 | * bmbt scan as the last step. |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1691 | */ |
| 1692 | if (ip->i_d.di_version == 3 && |
Brian Foster | 6fb10d6 | 2017-08-29 10:08:39 -0700 | [diff] [blame] | 1693 | ip->i_d.di_format == XFS_DINODE_FMT_BTREE) |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1694 | (*target_log_flags) |= XFS_ILOG_DOWNER; |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1695 | if (tip->i_d.di_version == 3 && |
Brian Foster | 6fb10d6 | 2017-08-29 10:08:39 -0700 | [diff] [blame] | 1696 | tip->i_d.di_format == XFS_DINODE_FMT_BTREE) |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1697 | (*src_log_flags) |= XFS_ILOG_DOWNER; |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1698 | |
| 1699 | /* |
| 1700 | * Swap the data forks of the inodes |
| 1701 | */ |
Gustavo A. R. Silva | 897992b | 2018-07-11 22:26:38 -0700 | [diff] [blame] | 1702 | swap(ip->i_df, tip->i_df); |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1703 | |
| 1704 | /* |
| 1705 | * Fix the on-disk inode values |
| 1706 | */ |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 1707 | tmp = (uint64_t)ip->i_d.di_nblocks; |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1708 | ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks; |
| 1709 | tip->i_d.di_nblocks = tmp + taforkblks - aforkblks; |
| 1710 | |
Gustavo A. R. Silva | 897992b | 2018-07-11 22:26:38 -0700 | [diff] [blame] | 1711 | swap(ip->i_d.di_nextents, tip->i_d.di_nextents); |
| 1712 | swap(ip->i_d.di_format, tip->i_d.di_format); |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1713 | |
| 1714 | /* |
| 1715 | * The extents in the source inode could still contain speculative |
| 1716 | * preallocation beyond EOF (e.g. the file is open but not modified |
| 1717 | * while defrag is in progress). In that case, we need to copy over the |
| 1718 | * number of delalloc blocks the data fork in the source inode is |
| 1719 | * tracking beyond EOF so that when the fork is truncated away when the |
| 1720 | * temporary inode is unlinked we don't underrun the i_delayed_blks |
| 1721 | * counter on that inode. |
| 1722 | */ |
| 1723 | ASSERT(tip->i_delayed_blks == 0); |
| 1724 | tip->i_delayed_blks = ip->i_delayed_blks; |
| 1725 | ip->i_delayed_blks = 0; |
| 1726 | |
| 1727 | switch (ip->i_d.di_format) { |
| 1728 | case XFS_DINODE_FMT_EXTENTS: |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1729 | (*src_log_flags) |= XFS_ILOG_DEXT; |
| 1730 | break; |
| 1731 | case XFS_DINODE_FMT_BTREE: |
| 1732 | ASSERT(ip->i_d.di_version < 3 || |
| 1733 | (*src_log_flags & XFS_ILOG_DOWNER)); |
| 1734 | (*src_log_flags) |= XFS_ILOG_DBROOT; |
| 1735 | break; |
| 1736 | } |
| 1737 | |
| 1738 | switch (tip->i_d.di_format) { |
| 1739 | case XFS_DINODE_FMT_EXTENTS: |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1740 | (*target_log_flags) |= XFS_ILOG_DEXT; |
| 1741 | break; |
| 1742 | case XFS_DINODE_FMT_BTREE: |
| 1743 | (*target_log_flags) |= XFS_ILOG_DBROOT; |
| 1744 | ASSERT(tip->i_d.di_version < 3 || |
| 1745 | (*target_log_flags & XFS_ILOG_DOWNER)); |
| 1746 | break; |
| 1747 | } |
| 1748 | |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
Brian Foster | 2dd3d70 | 2017-08-29 10:08:40 -0700 | [diff] [blame] | 1752 | /* |
| 1753 | * Fix up the owners of the bmbt blocks to refer to the current inode. The |
| 1754 | * change owner scan attempts to order all modified buffers in the current |
| 1755 | * transaction. In the event of ordered buffer failure, the offending buffer is |
| 1756 | * physically logged as a fallback and the scan returns -EAGAIN. We must roll |
| 1757 | * the transaction in this case to replenish the fallback log reservation and |
| 1758 | * restart the scan. This process repeats until the scan completes. |
| 1759 | */ |
| 1760 | static int |
| 1761 | xfs_swap_change_owner( |
| 1762 | struct xfs_trans **tpp, |
| 1763 | struct xfs_inode *ip, |
| 1764 | struct xfs_inode *tmpip) |
| 1765 | { |
| 1766 | int error; |
| 1767 | struct xfs_trans *tp = *tpp; |
| 1768 | |
| 1769 | do { |
| 1770 | error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino, |
| 1771 | NULL); |
| 1772 | /* success or fatal error */ |
| 1773 | if (error != -EAGAIN) |
| 1774 | break; |
| 1775 | |
| 1776 | error = xfs_trans_roll(tpp); |
| 1777 | if (error) |
| 1778 | break; |
| 1779 | tp = *tpp; |
| 1780 | |
| 1781 | /* |
| 1782 | * Redirty both inodes so they can relog and keep the log tail |
| 1783 | * moving forward. |
| 1784 | */ |
| 1785 | xfs_trans_ijoin(tp, ip, 0); |
| 1786 | xfs_trans_ijoin(tp, tmpip, 0); |
| 1787 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1788 | xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE); |
| 1789 | } while (true); |
| 1790 | |
| 1791 | return error; |
| 1792 | } |
| 1793 | |
Dave Chinner | 4ef897a | 2014-08-04 13:44:08 +1000 | [diff] [blame] | 1794 | int |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1795 | xfs_swap_extents( |
Darrick J. Wong | e06259a | 2016-10-03 09:11:52 -0700 | [diff] [blame] | 1796 | struct xfs_inode *ip, /* target inode */ |
| 1797 | struct xfs_inode *tip, /* tmp inode */ |
| 1798 | struct xfs_swapext *sxp) |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1799 | { |
Darrick J. Wong | e06259a | 2016-10-03 09:11:52 -0700 | [diff] [blame] | 1800 | struct xfs_mount *mp = ip->i_mount; |
| 1801 | struct xfs_trans *tp; |
| 1802 | struct xfs_bstat *sbp = &sxp->sx_stat; |
Darrick J. Wong | e06259a | 2016-10-03 09:11:52 -0700 | [diff] [blame] | 1803 | int src_log_flags, target_log_flags; |
| 1804 | int error = 0; |
Darrick J. Wong | e06259a | 2016-10-03 09:11:52 -0700 | [diff] [blame] | 1805 | int lock_flags; |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 1806 | uint64_t f; |
Brian Foster | 2dd3d70 | 2017-08-29 10:08:40 -0700 | [diff] [blame] | 1807 | int resblks = 0; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1808 | |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1809 | /* |
Dave Chinner | 723cac4 | 2015-02-23 21:47:29 +1100 | [diff] [blame] | 1810 | * Lock the inodes against other IO, page faults and truncate to |
| 1811 | * begin with. Then we can ensure the inodes are flushed and have no |
| 1812 | * page cache safely. Once we have done this we can take the ilocks and |
| 1813 | * do the rest of the checks. |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1814 | */ |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1815 | lock_two_nondirectories(VFS_I(ip), VFS_I(tip)); |
| 1816 | lock_flags = XFS_MMAPLOCK_EXCL; |
Darrick J. Wong | 7c2d238 | 2018-01-26 15:27:33 -0800 | [diff] [blame] | 1817 | xfs_lock_two_inodes(ip, XFS_MMAPLOCK_EXCL, tip, XFS_MMAPLOCK_EXCL); |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1818 | |
| 1819 | /* Verify that both files have the same format */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1820 | if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1821 | error = -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1822 | goto out_unlock; |
| 1823 | } |
| 1824 | |
| 1825 | /* Verify both files are either real-time or non-realtime */ |
| 1826 | if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1827 | error = -EINVAL; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1828 | goto out_unlock; |
| 1829 | } |
| 1830 | |
Dave Chinner | 4ef897a | 2014-08-04 13:44:08 +1000 | [diff] [blame] | 1831 | error = xfs_swap_extent_flush(ip); |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1832 | if (error) |
| 1833 | goto out_unlock; |
Dave Chinner | 4ef897a | 2014-08-04 13:44:08 +1000 | [diff] [blame] | 1834 | error = xfs_swap_extent_flush(tip); |
| 1835 | if (error) |
| 1836 | goto out_unlock; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1837 | |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1838 | /* |
| 1839 | * Extent "swapping" with rmap requires a permanent reservation and |
| 1840 | * a block reservation because it's really just a remap operation |
| 1841 | * performed with log redo items! |
| 1842 | */ |
| 1843 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) { |
Brian Foster | b3fed43 | 2018-03-09 14:01:58 -0800 | [diff] [blame] | 1844 | int w = XFS_DATA_FORK; |
| 1845 | uint32_t ipnext = XFS_IFORK_NEXTENTS(ip, w); |
| 1846 | uint32_t tipnext = XFS_IFORK_NEXTENTS(tip, w); |
| 1847 | |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1848 | /* |
Brian Foster | b3fed43 | 2018-03-09 14:01:58 -0800 | [diff] [blame] | 1849 | * Conceptually this shouldn't affect the shape of either bmbt, |
| 1850 | * but since we atomically move extents one by one, we reserve |
| 1851 | * enough space to rebuild both trees. |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1852 | */ |
Brian Foster | b3fed43 | 2018-03-09 14:01:58 -0800 | [diff] [blame] | 1853 | resblks = XFS_SWAP_RMAP_SPACE_RES(mp, ipnext, w); |
| 1854 | resblks += XFS_SWAP_RMAP_SPACE_RES(mp, tipnext, w); |
| 1855 | |
| 1856 | /* |
| 1857 | * Handle the corner case where either inode might straddle the |
| 1858 | * btree format boundary. If so, the inode could bounce between |
| 1859 | * btree <-> extent format on unmap -> remap cycles, freeing and |
| 1860 | * allocating a bmapbt block each time. |
| 1861 | */ |
| 1862 | if (ipnext == (XFS_IFORK_MAXEXT(ip, w) + 1)) |
| 1863 | resblks += XFS_IFORK_MAXEXT(ip, w); |
| 1864 | if (tipnext == (XFS_IFORK_MAXEXT(tip, w) + 1)) |
| 1865 | resblks += XFS_IFORK_MAXEXT(tip, w); |
Brian Foster | 2dd3d70 | 2017-08-29 10:08:40 -0700 | [diff] [blame] | 1866 | } |
| 1867 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1868 | if (error) |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1869 | goto out_unlock; |
Dave Chinner | 723cac4 | 2015-02-23 21:47:29 +1100 | [diff] [blame] | 1870 | |
| 1871 | /* |
| 1872 | * Lock and join the inodes to the tansaction so that transaction commit |
| 1873 | * or cancel will unlock the inodes from this point onwards. |
| 1874 | */ |
Darrick J. Wong | 7c2d238 | 2018-01-26 15:27:33 -0800 | [diff] [blame] | 1875 | xfs_lock_two_inodes(ip, XFS_ILOCK_EXCL, tip, XFS_ILOCK_EXCL); |
Dave Chinner | 4ef897a | 2014-08-04 13:44:08 +1000 | [diff] [blame] | 1876 | lock_flags |= XFS_ILOCK_EXCL; |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1877 | xfs_trans_ijoin(tp, ip, 0); |
| 1878 | xfs_trans_ijoin(tp, tip, 0); |
Dave Chinner | 723cac4 | 2015-02-23 21:47:29 +1100 | [diff] [blame] | 1879 | |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1880 | |
| 1881 | /* Verify all data are being swapped */ |
| 1882 | if (sxp->sx_offset != 0 || |
| 1883 | sxp->sx_length != ip->i_d.di_size || |
| 1884 | sxp->sx_length != tip->i_d.di_size) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1885 | error = -EFAULT; |
Dave Chinner | 4ef897a | 2014-08-04 13:44:08 +1000 | [diff] [blame] | 1886 | goto out_trans_cancel; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | trace_xfs_swap_extent_before(ip, 0); |
| 1890 | trace_xfs_swap_extent_before(tip, 1); |
| 1891 | |
| 1892 | /* check inode formats now that data is flushed */ |
| 1893 | error = xfs_swap_extents_check_format(ip, tip); |
| 1894 | if (error) { |
| 1895 | xfs_notice(mp, |
| 1896 | "%s: inode 0x%llx format is incompatible for exchanging.", |
| 1897 | __func__, ip->i_ino); |
Dave Chinner | 4ef897a | 2014-08-04 13:44:08 +1000 | [diff] [blame] | 1898 | goto out_trans_cancel; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /* |
| 1902 | * Compare the current change & modify times with that |
| 1903 | * passed in. If they differ, we abort this swap. |
| 1904 | * This is the mechanism used to ensure the calling |
| 1905 | * process that the file was not changed out from |
| 1906 | * under it. |
| 1907 | */ |
| 1908 | if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) || |
| 1909 | (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) || |
| 1910 | (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) || |
| 1911 | (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1912 | error = -EBUSY; |
Dave Chinner | 8121768 | 2014-08-04 13:29:32 +1000 | [diff] [blame] | 1913 | goto out_trans_cancel; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1914 | } |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1915 | |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 1916 | /* |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 1917 | * Note the trickiness in setting the log flags - we set the owner log |
| 1918 | * flag on the opposite inode (i.e. the inode we are setting the new |
| 1919 | * owner to be) because once we swap the forks and log that, log |
| 1920 | * recovery is going to see the fork as owned by the swapped inode, |
| 1921 | * not the pre-swapped inodes. |
| 1922 | */ |
| 1923 | src_log_flags = XFS_ILOG_CORE; |
| 1924 | target_log_flags = XFS_ILOG_CORE; |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 1925 | |
Darrick J. Wong | 1f08af5 | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1926 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 1927 | error = xfs_swap_extent_rmap(&tp, ip, tip); |
| 1928 | else |
| 1929 | error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags, |
| 1930 | &target_log_flags); |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1931 | if (error) |
| 1932 | goto out_trans_cancel; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1933 | |
Darrick J. Wong | f0bc4d1 | 2016-10-03 09:11:42 -0700 | [diff] [blame] | 1934 | /* Do we have to swap reflink flags? */ |
| 1935 | if ((ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK) ^ |
| 1936 | (tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)) { |
| 1937 | f = ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK; |
| 1938 | ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK; |
| 1939 | ip->i_d.di_flags2 |= tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK; |
| 1940 | tip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK; |
| 1941 | tip->i_d.di_flags2 |= f & XFS_DIFLAG2_REFLINK; |
Darrick J. Wong | 52bfcdd7 | 2017-09-18 09:41:18 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | /* Swap the cow forks. */ |
| 1945 | if (xfs_sb_version_hasreflink(&mp->m_sb)) { |
Darrick J. Wong | 52bfcdd7 | 2017-09-18 09:41:18 -0700 | [diff] [blame] | 1946 | ASSERT(ip->i_cformat == XFS_DINODE_FMT_EXTENTS); |
| 1947 | ASSERT(tip->i_cformat == XFS_DINODE_FMT_EXTENTS); |
| 1948 | |
Gustavo A. R. Silva | 897992b | 2018-07-11 22:26:38 -0700 | [diff] [blame] | 1949 | swap(ip->i_cnextents, tip->i_cnextents); |
| 1950 | swap(ip->i_cowfp, tip->i_cowfp); |
Darrick J. Wong | 52bfcdd7 | 2017-09-18 09:41:18 -0700 | [diff] [blame] | 1951 | |
Christoph Hellwig | 5bcffe3 | 2018-03-13 23:15:30 -0700 | [diff] [blame] | 1952 | if (ip->i_cowfp && ip->i_cowfp->if_bytes) |
Darrick J. Wong | 52bfcdd7 | 2017-09-18 09:41:18 -0700 | [diff] [blame] | 1953 | xfs_inode_set_cowblocks_tag(ip); |
| 1954 | else |
| 1955 | xfs_inode_clear_cowblocks_tag(ip); |
Christoph Hellwig | 5bcffe3 | 2018-03-13 23:15:30 -0700 | [diff] [blame] | 1956 | if (tip->i_cowfp && tip->i_cowfp->if_bytes) |
Darrick J. Wong | 52bfcdd7 | 2017-09-18 09:41:18 -0700 | [diff] [blame] | 1957 | xfs_inode_set_cowblocks_tag(tip); |
| 1958 | else |
| 1959 | xfs_inode_clear_cowblocks_tag(tip); |
Darrick J. Wong | f0bc4d1 | 2016-10-03 09:11:42 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1962 | xfs_trans_log_inode(tp, ip, src_log_flags); |
| 1963 | xfs_trans_log_inode(tp, tip, target_log_flags); |
| 1964 | |
| 1965 | /* |
Brian Foster | 6fb10d6 | 2017-08-29 10:08:39 -0700 | [diff] [blame] | 1966 | * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems |
| 1967 | * have inode number owner values in the bmbt blocks that still refer to |
| 1968 | * the old inode. Scan each bmbt to fix up the owner values with the |
| 1969 | * inode number of the current inode. |
| 1970 | */ |
| 1971 | if (src_log_flags & XFS_ILOG_DOWNER) { |
Brian Foster | 2dd3d70 | 2017-08-29 10:08:40 -0700 | [diff] [blame] | 1972 | error = xfs_swap_change_owner(&tp, ip, tip); |
Brian Foster | 6fb10d6 | 2017-08-29 10:08:39 -0700 | [diff] [blame] | 1973 | if (error) |
| 1974 | goto out_trans_cancel; |
| 1975 | } |
| 1976 | if (target_log_flags & XFS_ILOG_DOWNER) { |
Brian Foster | 2dd3d70 | 2017-08-29 10:08:40 -0700 | [diff] [blame] | 1977 | error = xfs_swap_change_owner(&tp, tip, ip); |
Brian Foster | 6fb10d6 | 2017-08-29 10:08:39 -0700 | [diff] [blame] | 1978 | if (error) |
| 1979 | goto out_trans_cancel; |
| 1980 | } |
| 1981 | |
| 1982 | /* |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1983 | * If this is a synchronous mount, make sure that the |
| 1984 | * transaction goes to disk before returning to the user. |
| 1985 | */ |
| 1986 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
| 1987 | xfs_trans_set_sync(tp); |
| 1988 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1989 | error = xfs_trans_commit(tp); |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1990 | |
| 1991 | trace_xfs_swap_extent_after(ip, 0); |
| 1992 | trace_xfs_swap_extent_after(tip, 1); |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1993 | |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1994 | out_unlock: |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 1995 | xfs_iunlock(ip, lock_flags); |
| 1996 | xfs_iunlock(tip, lock_flags); |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1997 | unlock_two_nondirectories(VFS_I(ip), VFS_I(tip)); |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 1998 | return error; |
| 1999 | |
Darrick J. Wong | 39aff5f | 2016-10-03 09:11:53 -0700 | [diff] [blame] | 2000 | out_trans_cancel: |
| 2001 | xfs_trans_cancel(tp); |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 2002 | goto out_unlock; |
Dave Chinner | a133d95 | 2013-08-12 20:49:48 +1000 | [diff] [blame] | 2003 | } |