Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 7 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 9 | #include "xfs_format.h" |
| 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 12 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_sb.h" |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 14 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 15 | #include "xfs_defer.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 16 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 18 | #include "xfs_btree.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 19 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_alloc.h" |
| 21 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 22 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 23 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_rtalloc.h" |
Darrick J. Wong | e9e899a | 2017-10-31 12:04:49 -0700 | [diff] [blame] | 25 | #include "xfs_errortag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_quota.h" |
| 28 | #include "xfs_trans_space.h" |
| 29 | #include "xfs_buf_item.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 30 | #include "xfs_trace.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 31 | #include "xfs_attr_leaf.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 32 | #include "xfs_filestream.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 33 | #include "xfs_rmap.h" |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 34 | #include "xfs_ag_resv.h" |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 35 | #include "xfs_refcount.h" |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 36 | #include "xfs_icache.h" |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 37 | #include "xfs_iomap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 41 | |
| 42 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 43 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | */ |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 47 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 48 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 50 | void |
| 51 | xfs_bmap_compute_maxlevels( |
| 52 | xfs_mount_t *mp, /* file system mount structure */ |
| 53 | int whichfork) /* data or attr fork */ |
| 54 | { |
| 55 | int level; /* btree level */ |
| 56 | uint maxblocks; /* max blocks at this level */ |
| 57 | uint maxleafents; /* max leaf entries possible */ |
| 58 | int maxrootrecs; /* max records in root block */ |
| 59 | int minleafrecs; /* min records in leaf block */ |
| 60 | int minnoderecs; /* min records in node block */ |
| 61 | int sz; /* root block size */ |
| 62 | |
| 63 | /* |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 64 | * The maximum number of extents in a file, hence the maximum number of |
| 65 | * leaf entries, is controlled by the size of the on-disk extent count, |
| 66 | * either a signed 32-bit number for the data fork, or a signed 16-bit |
| 67 | * number for the attr fork. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 68 | * |
| 69 | * Note that we can no longer assume that if we are in ATTR1 that |
| 70 | * the fork offset of all the inodes will be |
| 71 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 72 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 73 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 74 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 75 | * of a minimum size available. |
| 76 | */ |
| 77 | if (whichfork == XFS_DATA_FORK) { |
| 78 | maxleafents = MAXEXTNUM; |
| 79 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 80 | } else { |
| 81 | maxleafents = MAXAEXTNUM; |
| 82 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 83 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 84 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 85 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 86 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 87 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 88 | for (level = 1; maxblocks > 1; level++) { |
| 89 | if (maxblocks <= maxrootrecs) |
| 90 | maxblocks = 1; |
| 91 | else |
| 92 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 93 | } |
| 94 | mp->m_bm_maxlevels[whichfork] = level; |
| 95 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 97 | STATIC int /* error */ |
| 98 | xfs_bmbt_lookup_eq( |
| 99 | struct xfs_btree_cur *cur, |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 100 | struct xfs_bmbt_irec *irec, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 101 | int *stat) /* success/failure */ |
| 102 | { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 103 | cur->bc_rec.b = *irec; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 104 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 105 | } |
| 106 | |
| 107 | STATIC int /* error */ |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 108 | xfs_bmbt_lookup_first( |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 109 | struct xfs_btree_cur *cur, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 110 | int *stat) /* success/failure */ |
| 111 | { |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 112 | cur->bc_rec.b.br_startoff = 0; |
| 113 | cur->bc_rec.b.br_startblock = 0; |
| 114 | cur->bc_rec.b.br_blockcount = 0; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 115 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 116 | } |
| 117 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 118 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 119 | * Check if the inode needs to be converted to btree format. |
| 120 | */ |
| 121 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 122 | { |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 123 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 124 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 125 | return whichfork != XFS_COW_FORK && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 126 | ifp->if_format == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 127 | ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Check if the inode should be converted to extent format. |
| 132 | */ |
| 133 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 134 | { |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 135 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 136 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 137 | return whichfork != XFS_COW_FORK && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 138 | ifp->if_format == XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 139 | ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 143 | * Update the record referred to by cur to the value given by irec |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 144 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 145 | */ |
| 146 | STATIC int |
| 147 | xfs_bmbt_update( |
| 148 | struct xfs_btree_cur *cur, |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 149 | struct xfs_bmbt_irec *irec) |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 150 | { |
| 151 | union xfs_btree_rec rec; |
| 152 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 153 | xfs_bmbt_disk_set_all(&rec.bmbt, irec); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 154 | return xfs_btree_update(cur, &rec); |
| 155 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 158 | * Compute the worst-case number of indirect blocks that will be used |
| 159 | * for ip's delayed extent of length "len". |
| 160 | */ |
| 161 | STATIC xfs_filblks_t |
| 162 | xfs_bmap_worst_indlen( |
| 163 | xfs_inode_t *ip, /* incore inode pointer */ |
| 164 | xfs_filblks_t len) /* delayed extent length */ |
| 165 | { |
| 166 | int level; /* btree level number */ |
| 167 | int maxrecs; /* maximum record count at this level */ |
| 168 | xfs_mount_t *mp; /* mount structure */ |
| 169 | xfs_filblks_t rval; /* return value */ |
| 170 | |
| 171 | mp = ip->i_mount; |
| 172 | maxrecs = mp->m_bmap_dmxr[0]; |
| 173 | for (level = 0, rval = 0; |
| 174 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 175 | level++) { |
| 176 | len += maxrecs - 1; |
| 177 | do_div(len, maxrecs); |
| 178 | rval += len; |
Darrick J. Wong | 5e5c943 | 2017-09-18 09:41:17 -0700 | [diff] [blame] | 179 | if (len == 1) |
| 180 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 181 | level - 1; |
| 182 | if (level == 0) |
| 183 | maxrecs = mp->m_bmap_dmxr[1]; |
| 184 | } |
| 185 | return rval; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Calculate the default attribute fork offset for newly created inodes. |
| 190 | */ |
| 191 | uint |
| 192 | xfs_default_attroffset( |
| 193 | struct xfs_inode *ip) |
| 194 | { |
| 195 | struct xfs_mount *mp = ip->i_mount; |
| 196 | uint offset; |
| 197 | |
Christoph Hellwig | e9e2eae | 2020-03-18 08:15:10 -0700 | [diff] [blame] | 198 | if (mp->m_sb.sb_inodesize == 256) |
| 199 | offset = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 200 | else |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 201 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 202 | |
Christoph Hellwig | e9e2eae | 2020-03-18 08:15:10 -0700 | [diff] [blame] | 203 | ASSERT(offset < XFS_LITINO(mp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 204 | return offset; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Helper routine to reset inode di_forkoff field when switching |
| 209 | * attribute fork from local to extent format - we reset it where |
| 210 | * possible to make space available for inline data fork extents. |
| 211 | */ |
| 212 | STATIC void |
| 213 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 214 | xfs_inode_t *ip, |
| 215 | int whichfork) |
| 216 | { |
| 217 | if (whichfork == XFS_ATTR_FORK && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 218 | ip->i_df.if_format != XFS_DINODE_FMT_DEV && |
| 219 | ip->i_df.if_format != XFS_DINODE_FMT_BTREE) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 220 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 221 | |
| 222 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 223 | ip->i_d.di_forkoff = dfl_forkoff; |
| 224 | } |
| 225 | } |
| 226 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 227 | #ifdef DEBUG |
| 228 | STATIC struct xfs_buf * |
| 229 | xfs_bmap_get_bp( |
| 230 | struct xfs_btree_cur *cur, |
| 231 | xfs_fsblock_t bno) |
| 232 | { |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame] | 233 | struct xfs_log_item *lip; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 234 | int i; |
| 235 | |
| 236 | if (!cur) |
| 237 | return NULL; |
| 238 | |
| 239 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 240 | if (!cur->bc_bufs[i]) |
| 241 | break; |
| 242 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 243 | return cur->bc_bufs[i]; |
| 244 | } |
| 245 | |
| 246 | /* Chase down all the log items to see if the bp is there */ |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame] | 247 | list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) { |
| 248 | struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip; |
| 249 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 250 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 251 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 252 | return bip->bli_buf; |
| 253 | } |
| 254 | |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | STATIC void |
| 259 | xfs_check_block( |
| 260 | struct xfs_btree_block *block, |
| 261 | xfs_mount_t *mp, |
| 262 | int root, |
| 263 | short sz) |
| 264 | { |
| 265 | int i, j, dmxr; |
| 266 | __be64 *pp, *thispa; /* pointer to block address */ |
| 267 | xfs_bmbt_key_t *prevp, *keyp; |
| 268 | |
| 269 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 270 | |
| 271 | prevp = NULL; |
| 272 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 273 | dmxr = mp->m_bmap_dmxr[0]; |
| 274 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 275 | |
| 276 | if (prevp) { |
| 277 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 278 | be64_to_cpu(keyp->br_startoff)); |
| 279 | } |
| 280 | prevp = keyp; |
| 281 | |
| 282 | /* |
| 283 | * Compare the block numbers to see if there are dups. |
| 284 | */ |
| 285 | if (root) |
| 286 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 287 | else |
| 288 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 289 | |
| 290 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 291 | if (root) |
| 292 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 293 | else |
| 294 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 295 | if (*thispa == *pp) { |
| 296 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 297 | __func__, j, i, |
| 298 | (unsigned long long)be64_to_cpu(*thispa)); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 299 | xfs_err(mp, "%s: ptrs are equal in node\n", |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 300 | __func__); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 301 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Check that the extents for the inode ip are in the right order in all |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 309 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 310 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 311 | * them. The btree record ordering checks will still be done, so for such large |
| 312 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 313 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 314 | STATIC void |
| 315 | xfs_bmap_check_leaf_extents( |
| 316 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 317 | xfs_inode_t *ip, /* incore inode pointer */ |
| 318 | int whichfork) /* data or attr fork */ |
| 319 | { |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 320 | struct xfs_mount *mp = ip->i_mount; |
| 321 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 322 | struct xfs_btree_block *block; /* current btree block */ |
| 323 | xfs_fsblock_t bno; /* block # of "block" */ |
| 324 | xfs_buf_t *bp; /* buffer for "block" */ |
| 325 | int error; /* error return value */ |
| 326 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 327 | int level; /* btree level, for checking */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 328 | __be64 *pp; /* pointer to block address */ |
| 329 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 330 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 331 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 332 | int bp_release = 0; |
| 333 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 334 | if (ifp->if_format != XFS_DINODE_FMT_BTREE) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 335 | return; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 336 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 337 | /* skip large extent count inodes */ |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 338 | if (ip->i_df.if_nextents > 10000) |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 339 | return; |
| 340 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 341 | bno = NULLFSBLOCK; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 342 | block = ifp->if_broot; |
| 343 | /* |
| 344 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 345 | */ |
| 346 | level = be16_to_cpu(block->bb_level); |
| 347 | ASSERT(level > 0); |
| 348 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 349 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 350 | bno = be64_to_cpu(*pp); |
| 351 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 352 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 353 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 354 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 355 | |
| 356 | /* |
| 357 | * Go down the tree until leaf level is reached, following the first |
| 358 | * pointer (leftmost) at each level. |
| 359 | */ |
| 360 | while (level-- > 0) { |
| 361 | /* See if buf is in cur first */ |
| 362 | bp_release = 0; |
| 363 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 364 | if (!bp) { |
| 365 | bp_release = 1; |
Eric Sandeen | f5b999c | 2019-06-12 09:00:00 -0700 | [diff] [blame] | 366 | error = xfs_btree_read_bufl(mp, NULL, bno, &bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 367 | XFS_BMAP_BTREE_REF, |
| 368 | &xfs_bmbt_buf_ops); |
| 369 | if (error) |
| 370 | goto error_norelse; |
| 371 | } |
| 372 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 373 | if (level == 0) |
| 374 | break; |
| 375 | |
| 376 | /* |
| 377 | * Check this block for basic sanity (increasing keys and |
| 378 | * no duplicate blocks). |
| 379 | */ |
| 380 | |
| 381 | xfs_check_block(block, mp, 0, 0); |
| 382 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 383 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 384 | if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) { |
| 385 | error = -EFSCORRUPTED; |
| 386 | goto error0; |
| 387 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 388 | if (bp_release) { |
| 389 | bp_release = 0; |
| 390 | xfs_trans_brelse(NULL, bp); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 396 | */ |
| 397 | i = 0; |
| 398 | |
| 399 | /* |
| 400 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 401 | */ |
| 402 | for (;;) { |
| 403 | xfs_fsblock_t nextbno; |
| 404 | xfs_extnum_t num_recs; |
| 405 | |
| 406 | |
| 407 | num_recs = xfs_btree_get_numrecs(block); |
| 408 | |
| 409 | /* |
| 410 | * Read-ahead the next leaf block, if any. |
| 411 | */ |
| 412 | |
| 413 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 414 | |
| 415 | /* |
| 416 | * Check all the extents to make sure they are OK. |
| 417 | * If we had a previous block, the last entry should |
| 418 | * conform with the first entry in this one. |
| 419 | */ |
| 420 | |
| 421 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 422 | if (i) { |
| 423 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 424 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 425 | xfs_bmbt_disk_get_startoff(ep)); |
| 426 | } |
| 427 | for (j = 1; j < num_recs; j++) { |
| 428 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 429 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 430 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 431 | xfs_bmbt_disk_get_startoff(nextp)); |
| 432 | ep = nextp; |
| 433 | } |
| 434 | |
| 435 | last = *ep; |
| 436 | i += num_recs; |
| 437 | if (bp_release) { |
| 438 | bp_release = 0; |
| 439 | xfs_trans_brelse(NULL, bp); |
| 440 | } |
| 441 | bno = nextbno; |
| 442 | /* |
| 443 | * If we've reached the end, stop. |
| 444 | */ |
| 445 | if (bno == NULLFSBLOCK) |
| 446 | break; |
| 447 | |
| 448 | bp_release = 0; |
| 449 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 450 | if (!bp) { |
| 451 | bp_release = 1; |
Eric Sandeen | f5b999c | 2019-06-12 09:00:00 -0700 | [diff] [blame] | 452 | error = xfs_btree_read_bufl(mp, NULL, bno, &bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 453 | XFS_BMAP_BTREE_REF, |
| 454 | &xfs_bmbt_buf_ops); |
| 455 | if (error) |
| 456 | goto error_norelse; |
| 457 | } |
| 458 | block = XFS_BUF_TO_BLOCK(bp); |
| 459 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 460 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 461 | return; |
| 462 | |
| 463 | error0: |
| 464 | xfs_warn(mp, "%s: at error0", __func__); |
| 465 | if (bp_release) |
| 466 | xfs_trans_brelse(NULL, bp); |
| 467 | error_norelse: |
| 468 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 469 | __func__, i); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 470 | xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 471 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 472 | return; |
| 473 | } |
| 474 | |
| 475 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 476 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 477 | * given the caller's original parameters. Specifically check the |
| 478 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 479 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 480 | */ |
| 481 | STATIC void |
| 482 | xfs_bmap_validate_ret( |
| 483 | xfs_fileoff_t bno, |
| 484 | xfs_filblks_t len, |
| 485 | int flags, |
| 486 | xfs_bmbt_irec_t *mval, |
| 487 | int nmap, |
| 488 | int ret_nmap) |
| 489 | { |
| 490 | int i; /* index to map values */ |
| 491 | |
| 492 | ASSERT(ret_nmap <= nmap); |
| 493 | |
| 494 | for (i = 0; i < ret_nmap; i++) { |
| 495 | ASSERT(mval[i].br_blockcount > 0); |
| 496 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 497 | ASSERT(mval[i].br_startoff >= bno); |
| 498 | ASSERT(mval[i].br_blockcount <= len); |
| 499 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 500 | bno + len); |
| 501 | } else { |
| 502 | ASSERT(mval[i].br_startoff < bno + len); |
| 503 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 504 | bno); |
| 505 | } |
| 506 | ASSERT(i == 0 || |
| 507 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 508 | mval[i].br_startoff); |
| 509 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 510 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 511 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 512 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | #else |
| 517 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
Darrick J. Wong | 7bf7a19 | 2017-08-31 15:11:06 -0700 | [diff] [blame] | 518 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 519 | #endif /* DEBUG */ |
| 520 | |
| 521 | /* |
| 522 | * bmap free list manipulation functions |
| 523 | */ |
| 524 | |
| 525 | /* |
| 526 | * Add the extent to the list of extents to be free at transaction end. |
| 527 | * The list is maintained sorted (by block number). |
| 528 | */ |
| 529 | void |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 530 | __xfs_bmap_add_free( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 531 | struct xfs_trans *tp, |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 532 | xfs_fsblock_t bno, |
| 533 | xfs_filblks_t len, |
Darrick J. Wong | 66e3237 | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 534 | const struct xfs_owner_info *oinfo, |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 535 | bool skip_discard) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 536 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 537 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 538 | #ifdef DEBUG |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 539 | struct xfs_mount *mp = tp->t_mountp; |
| 540 | xfs_agnumber_t agno; |
| 541 | xfs_agblock_t agbno; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 542 | |
| 543 | ASSERT(bno != NULLFSBLOCK); |
| 544 | ASSERT(len > 0); |
| 545 | ASSERT(len <= MAXEXTLEN); |
| 546 | ASSERT(!isnullstartblock(bno)); |
| 547 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 548 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 549 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 550 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 551 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 552 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 553 | #endif |
| 554 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 555 | |
Carlos Maiolino | 3050bd0 | 2020-07-22 09:23:04 -0700 | [diff] [blame] | 556 | new = kmem_cache_alloc(xfs_bmap_free_item_zone, |
| 557 | GFP_KERNEL | __GFP_NOFAIL); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 558 | new->xefi_startblock = bno; |
| 559 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 560 | if (oinfo) |
| 561 | new->xefi_oinfo = *oinfo; |
| 562 | else |
Darrick J. Wong | 7280fed | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 563 | new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE; |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 564 | new->xefi_skip_discard = skip_discard; |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 565 | trace_xfs_bmap_free_defer(tp->t_mountp, |
| 566 | XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0, |
| 567 | XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len); |
| 568 | xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Inode fork format manipulation functions |
| 573 | */ |
| 574 | |
| 575 | /* |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 576 | * Convert the inode format to extent format if it currently is in btree format, |
| 577 | * but the extent list is small enough that it fits into the extent format. |
| 578 | * |
| 579 | * Since the extents are already in-core, all we have to do is give up the space |
| 580 | * for the btree root and pitch the leaf block. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 581 | */ |
| 582 | STATIC int /* error */ |
| 583 | xfs_bmap_btree_to_extents( |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 584 | struct xfs_trans *tp, /* transaction pointer */ |
| 585 | struct xfs_inode *ip, /* incore inode pointer */ |
| 586 | struct xfs_btree_cur *cur, /* btree cursor */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 587 | int *logflagsp, /* inode logging flags */ |
| 588 | int whichfork) /* data or attr fork */ |
| 589 | { |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 590 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 591 | struct xfs_mount *mp = ip->i_mount; |
| 592 | struct xfs_btree_block *rblock = ifp->if_broot; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 593 | struct xfs_btree_block *cblock;/* child btree block */ |
| 594 | xfs_fsblock_t cbno; /* child block number */ |
| 595 | xfs_buf_t *cbp; /* child block's buffer */ |
| 596 | int error; /* error return value */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 597 | __be64 *pp; /* ptr to block address */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 598 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 599 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 600 | /* check if we actually need the extent format first: */ |
| 601 | if (!xfs_bmap_wants_extents(ip, whichfork)) |
| 602 | return 0; |
| 603 | |
| 604 | ASSERT(cur); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 605 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 606 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 607 | ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 608 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 609 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 610 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 611 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 612 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 613 | cbno = be64_to_cpu(*pp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 614 | #ifdef DEBUG |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 615 | if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1))) |
| 616 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 617 | #endif |
Eric Sandeen | f5b999c | 2019-06-12 09:00:00 -0700 | [diff] [blame] | 618 | error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 619 | &xfs_bmbt_buf_ops); |
| 620 | if (error) |
| 621 | return error; |
| 622 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 623 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 624 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 625 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 626 | xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 627 | ip->i_d.di_nblocks--; |
| 628 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 629 | xfs_trans_binval(tp, cbp); |
| 630 | if (cur->bc_bufs[0] == cbp) |
| 631 | cur->bc_bufs[0] = NULL; |
| 632 | xfs_iroot_realloc(ip, -1, whichfork); |
| 633 | ASSERT(ifp->if_broot == NULL); |
| 634 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 635 | ifp->if_format = XFS_DINODE_FMT_EXTENTS; |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 636 | *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * Convert an extents-format file into a btree-format file. |
| 642 | * The new file will have a root block (in the inode) and a single child block. |
| 643 | */ |
| 644 | STATIC int /* error */ |
| 645 | xfs_bmap_extents_to_btree( |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 646 | struct xfs_trans *tp, /* transaction pointer */ |
| 647 | struct xfs_inode *ip, /* incore inode pointer */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 648 | struct xfs_btree_cur **curp, /* cursor returned to caller */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 649 | int wasdel, /* converting a delayed alloc */ |
| 650 | int *logflagsp, /* inode logging flags */ |
| 651 | int whichfork) /* data or attr fork */ |
| 652 | { |
| 653 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 654 | struct xfs_buf *abp; /* buffer for ablock */ |
| 655 | struct xfs_alloc_arg args; /* allocation arguments */ |
| 656 | struct xfs_bmbt_rec *arp; /* child record pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 657 | struct xfs_btree_block *block; /* btree root block */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 658 | struct xfs_btree_cur *cur; /* bmap btree cursor */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 659 | int error; /* error return value */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 660 | struct xfs_ifork *ifp; /* inode fork pointer */ |
| 661 | struct xfs_bmbt_key *kp; /* root block key pointer */ |
| 662 | struct xfs_mount *mp; /* mount structure */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 663 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 664 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 665 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 666 | xfs_extnum_t cnt = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 667 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 668 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 669 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 670 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 671 | ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 672 | |
| 673 | /* |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 674 | * Make space in the inode incore. This needs to be undone if we fail |
| 675 | * to expand the root. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 676 | */ |
| 677 | xfs_iroot_realloc(ip, 1, whichfork); |
| 678 | ifp->if_flags |= XFS_IFBROOT; |
| 679 | |
| 680 | /* |
| 681 | * Fill in the root. |
| 682 | */ |
| 683 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 684 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 685 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 686 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 687 | /* |
| 688 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 689 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 690 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 8ef5479 | 2020-03-10 17:54:38 -0700 | [diff] [blame] | 691 | cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 692 | /* |
| 693 | * Convert to a btree with two levels, one record in root. |
| 694 | */ |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 695 | ifp->if_format = XFS_DINODE_FMT_BTREE; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 696 | memset(&args, 0, sizeof(args)); |
| 697 | args.tp = tp; |
| 698 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 699 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 700 | if (tp->t_firstblock == NULLFSBLOCK) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 701 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 702 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 703 | } else if (tp->t_flags & XFS_TRANS_LOWMODE) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 704 | args.type = XFS_ALLOCTYPE_START_BNO; |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 705 | args.fsbno = tp->t_firstblock; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 706 | } else { |
| 707 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 708 | args.fsbno = tp->t_firstblock; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 709 | } |
| 710 | args.minlen = args.maxlen = args.prod = 1; |
| 711 | args.wasdel = wasdel; |
| 712 | *logflagsp = 0; |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 713 | error = xfs_alloc_vextent(&args); |
| 714 | if (error) |
| 715 | goto out_root_realloc; |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 716 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 717 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 718 | error = -ENOSPC; |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 719 | goto out_root_realloc; |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 720 | } |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 721 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 722 | /* |
| 723 | * Allocation can't fail, the space was reserved. |
| 724 | */ |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 725 | ASSERT(tp->t_firstblock == NULLFSBLOCK || |
| 726 | args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock)); |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 727 | tp->t_firstblock = args.fsbno; |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 728 | cur->bc_ino.allocated++; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 729 | ip->i_d.di_nblocks++; |
| 730 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
Darrick J. Wong | ee647f8 | 2020-01-23 17:01:19 -0800 | [diff] [blame] | 731 | error = xfs_trans_get_buf(tp, mp->m_ddev_targp, |
| 732 | XFS_FSB_TO_DADDR(mp, args.fsbno), |
| 733 | mp->m_bsize, 0, &abp); |
| 734 | if (error) |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 735 | goto out_unreserve_dquot; |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 736 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 737 | /* |
| 738 | * Fill in the child block. |
| 739 | */ |
| 740 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 741 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 742 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 743 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 744 | XFS_BTREE_LONG_PTRS); |
| 745 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 746 | for_each_xfs_iext(ifp, &icur, &rec) { |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 747 | if (isnullstartblock(rec.br_startblock)) |
| 748 | continue; |
| 749 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt); |
| 750 | xfs_bmbt_disk_set_all(arp, &rec); |
| 751 | cnt++; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 752 | } |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 753 | ASSERT(cnt == ifp->if_nextents); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 754 | xfs_btree_set_numrecs(ablock, cnt); |
| 755 | |
| 756 | /* |
| 757 | * Fill in the root key and pointer. |
| 758 | */ |
| 759 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 760 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 761 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 762 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 763 | be16_to_cpu(block->bb_level))); |
| 764 | *pp = cpu_to_be64(args.fsbno); |
| 765 | |
| 766 | /* |
| 767 | * Do all this logging at the end so that |
| 768 | * the root is at the right level. |
| 769 | */ |
| 770 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 771 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 772 | ASSERT(*curp == NULL); |
| 773 | *curp = cur; |
| 774 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 775 | return 0; |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 776 | |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 777 | out_unreserve_dquot: |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 778 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 779 | out_root_realloc: |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 780 | xfs_iroot_realloc(ip, -1, whichfork); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 781 | ifp->if_format = XFS_DINODE_FMT_EXTENTS; |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 782 | ASSERT(ifp->if_broot == NULL); |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 783 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 784 | |
| 785 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Convert a local file to an extents file. |
| 790 | * This code is out of bounds for data forks of regular files, |
| 791 | * since the file data needs to get logged so things will stay consistent. |
| 792 | * (The bmap-level manipulations are ok, though). |
| 793 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 794 | void |
| 795 | xfs_bmap_local_to_extents_empty( |
Brian Foster | aeea4b7 | 2019-10-07 12:54:16 -0700 | [diff] [blame] | 796 | struct xfs_trans *tp, |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 797 | struct xfs_inode *ip, |
| 798 | int whichfork) |
| 799 | { |
| 800 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 801 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 802 | ASSERT(whichfork != XFS_COW_FORK); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 803 | ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 804 | ASSERT(ifp->if_bytes == 0); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 805 | ASSERT(ifp->if_nextents == 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 806 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 807 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 808 | ifp->if_flags &= ~XFS_IFINLINE; |
| 809 | ifp->if_flags |= XFS_IFEXTENTS; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 810 | ifp->if_u1.if_root = NULL; |
| 811 | ifp->if_height = 0; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 812 | ifp->if_format = XFS_DINODE_FMT_EXTENTS; |
Brian Foster | aeea4b7 | 2019-10-07 12:54:16 -0700 | [diff] [blame] | 813 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 817 | STATIC int /* error */ |
| 818 | xfs_bmap_local_to_extents( |
| 819 | xfs_trans_t *tp, /* transaction pointer */ |
| 820 | xfs_inode_t *ip, /* incore inode pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 821 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 822 | int *logflagsp, /* inode logging flags */ |
| 823 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 824 | void (*init_fn)(struct xfs_trans *tp, |
| 825 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 826 | struct xfs_inode *ip, |
| 827 | struct xfs_ifork *ifp)) |
| 828 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 829 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 830 | int flags; /* logging flags returned */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 831 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 832 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 833 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 834 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 835 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 836 | |
| 837 | /* |
| 838 | * We don't want to deal with the case of keeping inode data inline yet. |
| 839 | * So sending the data fork of a regular inode is invalid. |
| 840 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 841 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 842 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 843 | ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 844 | |
| 845 | if (!ifp->if_bytes) { |
Brian Foster | aeea4b7 | 2019-10-07 12:54:16 -0700 | [diff] [blame] | 846 | xfs_bmap_local_to_extents_empty(tp, ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 847 | flags = XFS_ILOG_CORE; |
| 848 | goto done; |
| 849 | } |
| 850 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 851 | flags = 0; |
| 852 | error = 0; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 853 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 854 | memset(&args, 0, sizeof(args)); |
| 855 | args.tp = tp; |
| 856 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 857 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 858 | /* |
| 859 | * Allocate a block. We know we need only one, since the |
| 860 | * file currently fits in an inode. |
| 861 | */ |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 862 | if (tp->t_firstblock == NULLFSBLOCK) { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 863 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 864 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 865 | } else { |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 866 | args.fsbno = tp->t_firstblock; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 867 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 868 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 869 | args.total = total; |
| 870 | args.minlen = args.maxlen = args.prod = 1; |
| 871 | error = xfs_alloc_vextent(&args); |
| 872 | if (error) |
| 873 | goto done; |
| 874 | |
| 875 | /* Can't fail, the space was reserved. */ |
| 876 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 877 | ASSERT(args.len == 1); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 878 | tp->t_firstblock = args.fsbno; |
Darrick J. Wong | ee647f8 | 2020-01-23 17:01:19 -0800 | [diff] [blame] | 879 | error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, |
| 880 | XFS_FSB_TO_DADDR(args.mp, args.fsbno), |
| 881 | args.mp->m_bsize, 0, &bp); |
| 882 | if (error) |
| 883 | goto done; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 884 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 885 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 886 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 887 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 888 | * The callout is responsible for logging because the remote format |
| 889 | * might differ from the local format and thus we don't know how much to |
| 890 | * log here. Note that init_fn must also set the buffer log item type |
| 891 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 892 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 893 | init_fn(tp, bp, ip, ifp); |
| 894 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 895 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 896 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
Brian Foster | aeea4b7 | 2019-10-07 12:54:16 -0700 | [diff] [blame] | 897 | xfs_bmap_local_to_extents_empty(tp, ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 898 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 899 | |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 900 | ifp->if_u1.if_root = NULL; |
| 901 | ifp->if_height = 0; |
| 902 | |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 903 | rec.br_startoff = 0; |
| 904 | rec.br_startblock = args.fsbno; |
| 905 | rec.br_blockcount = 1; |
| 906 | rec.br_state = XFS_EXT_NORM; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 907 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 908 | xfs_iext_insert(ip, &icur, &rec, 0); |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 909 | |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 910 | ifp->if_nextents = 1; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 911 | ip->i_d.di_nblocks = 1; |
| 912 | xfs_trans_mod_dquot_byino(tp, ip, |
| 913 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 914 | flags |= xfs_ilog_fext(whichfork); |
| 915 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 916 | done: |
| 917 | *logflagsp = flags; |
| 918 | return error; |
| 919 | } |
| 920 | |
| 921 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 923 | */ |
| 924 | STATIC int /* error */ |
| 925 | xfs_bmap_add_attrfork_btree( |
| 926 | xfs_trans_t *tp, /* transaction pointer */ |
| 927 | xfs_inode_t *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | int *flags) /* inode logging flags */ |
| 929 | { |
| 930 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 931 | int error; /* error return value */ |
| 932 | xfs_mount_t *mp; /* file system mount struct */ |
| 933 | int stat; /* newroot status */ |
| 934 | |
| 935 | mp = ip->i_mount; |
| 936 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 937 | *flags |= XFS_ILOG_DBROOT; |
| 938 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 939 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 940 | error = xfs_bmbt_lookup_first(cur, &stat); |
| 941 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 943 | /* must be at least one entry */ |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 944 | if (XFS_IS_CORRUPT(mp, stat != 1)) { |
| 945 | error = -EFSCORRUPTED; |
| 946 | goto error0; |
| 947 | } |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 948 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | goto error0; |
| 950 | if (stat == 0) { |
| 951 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 952 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | } |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 954 | cur->bc_ino.allocated = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 956 | } |
| 957 | return 0; |
| 958 | error0: |
| 959 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 960 | return error; |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 965 | */ |
| 966 | STATIC int /* error */ |
| 967 | xfs_bmap_add_attrfork_extents( |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 968 | struct xfs_trans *tp, /* transaction pointer */ |
| 969 | struct xfs_inode *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | int *flags) /* inode logging flags */ |
| 971 | { |
| 972 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 973 | int error; /* error return value */ |
| 974 | |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 975 | if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <= |
| 976 | XFS_IFORK_DSIZE(ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | return 0; |
| 978 | cur = NULL; |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 979 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags, |
| 980 | XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | if (cur) { |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 982 | cur->bc_ino.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 983 | xfs_btree_del_cursor(cur, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | } |
| 985 | return error; |
| 986 | } |
| 987 | |
| 988 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 989 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 990 | * different data fork content type needs a different callout to do the |
| 991 | * conversion. Some are basic and only require special block initialisation |
| 992 | * callouts for the data formating, others (directories) are so specialised they |
| 993 | * handle everything themselves. |
| 994 | * |
| 995 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 996 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 997 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | */ |
| 999 | STATIC int /* error */ |
| 1000 | xfs_bmap_add_attrfork_local( |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1001 | struct xfs_trans *tp, /* transaction pointer */ |
| 1002 | struct xfs_inode *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | int *flags) /* inode logging flags */ |
| 1004 | { |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1005 | struct xfs_da_args dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
| 1007 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1008 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1009 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1010 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1012 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | dargs.dp = ip; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1014 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | dargs.whichfork = XFS_DATA_FORK; |
| 1016 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1017 | return xfs_dir2_sf_to_block(&dargs); |
| 1018 | } |
| 1019 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1020 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1021 | return xfs_bmap_local_to_extents(tp, ip, 1, flags, |
| 1022 | XFS_DATA_FORK, |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1023 | xfs_symlink_local_to_remote); |
| 1024 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1025 | /* should only be called for types that support local format data */ |
| 1026 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1027 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 1030 | /* Set an inode attr fork off based on the format */ |
| 1031 | int |
| 1032 | xfs_bmap_set_attrforkoff( |
| 1033 | struct xfs_inode *ip, |
| 1034 | int size, |
| 1035 | int *version) |
| 1036 | { |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1037 | switch (ip->i_df.if_format) { |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 1038 | case XFS_DINODE_FMT_DEV: |
| 1039 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1040 | break; |
| 1041 | case XFS_DINODE_FMT_LOCAL: |
| 1042 | case XFS_DINODE_FMT_EXTENTS: |
| 1043 | case XFS_DINODE_FMT_BTREE: |
| 1044 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1045 | if (!ip->i_d.di_forkoff) |
| 1046 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1047 | else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version) |
| 1048 | *version = 2; |
| 1049 | break; |
| 1050 | default: |
| 1051 | ASSERT(0); |
| 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | |
| 1055 | return 0; |
| 1056 | } |
| 1057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1059 | * Convert inode from non-attributed to attributed. |
| 1060 | * Must not be in a transaction, ip must not be locked. |
| 1061 | */ |
| 1062 | int /* error code */ |
| 1063 | xfs_bmap_add_attrfork( |
| 1064 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1065 | int size, /* space new attribute needs */ |
| 1066 | int rsvd) /* xact may use reserved blks */ |
| 1067 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1068 | xfs_mount_t *mp; /* mount structure */ |
| 1069 | xfs_trans_t *tp; /* transaction pointer */ |
| 1070 | int blks; /* space reservation */ |
| 1071 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1072 | int logflags; /* logging flags */ |
| 1073 | int error; /* error return value */ |
| 1074 | |
| 1075 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1076 | |
| 1077 | mp = ip->i_mount; |
| 1078 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1079 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1080 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1081 | |
| 1082 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1083 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1084 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1085 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1086 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1087 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1088 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1089 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1090 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1091 | if (error) |
| 1092 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1093 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1094 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1095 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1096 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1097 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 1098 | error = xfs_bmap_set_attrforkoff(ip, size, &version); |
| 1099 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1100 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1101 | ASSERT(ip->i_afp == NULL); |
Carlos Maiolino | 32a2b11 | 2020-07-22 09:23:10 -0700 | [diff] [blame] | 1102 | |
| 1103 | ip->i_afp = kmem_cache_zalloc(xfs_ifork_zone, |
| 1104 | GFP_KERNEL | __GFP_NOFAIL); |
| 1105 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1106 | ip->i_afp->if_format = XFS_DINODE_FMT_EXTENTS; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1107 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1108 | logflags = 0; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1109 | switch (ip->i_df.if_format) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1110 | case XFS_DINODE_FMT_LOCAL: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1111 | error = xfs_bmap_add_attrfork_local(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1112 | break; |
| 1113 | case XFS_DINODE_FMT_EXTENTS: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1114 | error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1115 | break; |
| 1116 | case XFS_DINODE_FMT_BTREE: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1117 | error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1118 | break; |
| 1119 | default: |
| 1120 | error = 0; |
| 1121 | break; |
| 1122 | } |
| 1123 | if (logflags) |
| 1124 | xfs_trans_log_inode(tp, ip, logflags); |
| 1125 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1126 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1127 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1128 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1129 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1130 | |
| 1131 | spin_lock(&mp->m_sb_lock); |
| 1132 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1133 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1134 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1135 | } |
| 1136 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1137 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1138 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1139 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1140 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1141 | if (log_sb) |
| 1142 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1143 | } |
| 1144 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1145 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1146 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1147 | return error; |
| 1148 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1149 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1150 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1151 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1152 | return error; |
| 1153 | } |
| 1154 | |
| 1155 | /* |
| 1156 | * Internal and external extent tree search functions. |
| 1157 | */ |
| 1158 | |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1159 | struct xfs_iread_state { |
| 1160 | struct xfs_iext_cursor icur; |
| 1161 | xfs_extnum_t loaded; |
| 1162 | }; |
| 1163 | |
| 1164 | /* Stuff every bmbt record from this block into the incore extent map. */ |
| 1165 | static int |
| 1166 | xfs_iread_bmbt_block( |
| 1167 | struct xfs_btree_cur *cur, |
| 1168 | int level, |
| 1169 | void *priv) |
| 1170 | { |
| 1171 | struct xfs_iread_state *ir = priv; |
| 1172 | struct xfs_mount *mp = cur->bc_mp; |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 1173 | struct xfs_inode *ip = cur->bc_ino.ip; |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1174 | struct xfs_btree_block *block; |
| 1175 | struct xfs_buf *bp; |
| 1176 | struct xfs_bmbt_rec *frp; |
| 1177 | xfs_extnum_t num_recs; |
| 1178 | xfs_extnum_t j; |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 1179 | int whichfork = cur->bc_ino.whichfork; |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1180 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1181 | |
| 1182 | block = xfs_btree_get_block(cur, level, &bp); |
| 1183 | |
| 1184 | /* Abort if we find more records than nextents. */ |
| 1185 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1186 | if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) { |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1187 | xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).", |
| 1188 | (unsigned long long)ip->i_ino); |
| 1189 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block, |
| 1190 | sizeof(*block), __this_address); |
| 1191 | return -EFSCORRUPTED; |
| 1192 | } |
| 1193 | |
| 1194 | /* Copy records into the incore cache. */ |
| 1195 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 1196 | for (j = 0; j < num_recs; j++, frp++, ir->loaded++) { |
| 1197 | struct xfs_bmbt_irec new; |
| 1198 | xfs_failaddr_t fa; |
| 1199 | |
| 1200 | xfs_bmbt_disk_get_all(frp, &new); |
| 1201 | fa = xfs_bmap_validate_extent(ip, whichfork, &new); |
| 1202 | if (fa) { |
| 1203 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
| 1204 | "xfs_iread_extents(2)", frp, |
| 1205 | sizeof(*frp), fa); |
| 1206 | return -EFSCORRUPTED; |
| 1207 | } |
| 1208 | xfs_iext_insert(ip, &ir->icur, &new, |
| 1209 | xfs_bmap_fork_to_state(whichfork)); |
| 1210 | trace_xfs_read_extent(ip, &ir->icur, |
| 1211 | xfs_bmap_fork_to_state(whichfork), _THIS_IP_); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1212 | xfs_iext_next(ifp, &ir->icur); |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1218 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1219 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1220 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1221 | int |
| 1222 | xfs_iread_extents( |
| 1223 | struct xfs_trans *tp, |
| 1224 | struct xfs_inode *ip, |
| 1225 | int whichfork) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1226 | { |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1227 | struct xfs_iread_state ir; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1228 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1229 | struct xfs_mount *mp = ip->i_mount; |
| 1230 | struct xfs_btree_cur *cur; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1231 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1232 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1233 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1234 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1235 | if (XFS_IS_CORRUPT(mp, ifp->if_format != XFS_DINODE_FMT_BTREE)) { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1236 | error = -EFSCORRUPTED; |
| 1237 | goto out; |
| 1238 | } |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1239 | |
| 1240 | ir.loaded = 0; |
| 1241 | xfs_iext_first(ifp, &ir.icur); |
| 1242 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 1243 | error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block, |
| 1244 | XFS_BTREE_VISIT_RECORDS, &ir); |
| 1245 | xfs_btree_del_cursor(cur, error); |
| 1246 | if (error) |
| 1247 | goto out; |
| 1248 | |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1249 | if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) { |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1250 | error = -EFSCORRUPTED; |
| 1251 | goto out; |
| 1252 | } |
| 1253 | ASSERT(ir.loaded == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1254 | |
| 1255 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1256 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1257 | out: |
| 1258 | xfs_iext_destroy(ifp); |
| 1259 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1260 | } |
| 1261 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1262 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1263 | * Returns the relative block number of the first unused block(s) in the given |
| 1264 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1265 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1266 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1267 | */ |
| 1268 | int /* error */ |
| 1269 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1270 | struct xfs_trans *tp, /* transaction pointer */ |
| 1271 | struct xfs_inode *ip, /* incore inode */ |
| 1272 | xfs_extlen_t len, /* size of hole to find */ |
| 1273 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1274 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1275 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1276 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1277 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1278 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1279 | xfs_fileoff_t lastaddr = 0; |
| 1280 | xfs_fileoff_t lowest, max; |
| 1281 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1282 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1283 | if (ifp->if_format == XFS_DINODE_FMT_LOCAL) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1284 | *first_unused = 0; |
| 1285 | return 0; |
| 1286 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1287 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1288 | ASSERT(xfs_ifork_has_extents(ifp)); |
| 1289 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1290 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1291 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1292 | if (error) |
| 1293 | return error; |
| 1294 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1295 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1296 | lowest = max = *first_unused; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1297 | for_each_xfs_iext(ifp, &icur, &got) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1298 | /* |
| 1299 | * See if the hole before this extent will work. |
| 1300 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1301 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1302 | got.br_startoff - max >= len) |
| 1303 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1304 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1305 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1306 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1307 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1308 | *first_unused = max; |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1313 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1314 | * last_block (input value) in the file. |
| 1315 | * This is not based on i_size, it is based on the extent records. |
| 1316 | * Returns 0 for local files, as they do not have extent records. |
| 1317 | */ |
| 1318 | int /* error */ |
| 1319 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1320 | struct xfs_trans *tp, /* transaction pointer */ |
| 1321 | struct xfs_inode *ip, /* incore inode */ |
| 1322 | xfs_fileoff_t *last_block, /* last block */ |
| 1323 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1324 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1325 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1326 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1327 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1328 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1329 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1330 | switch (ifp->if_format) { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1331 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1332 | *last_block = 0; |
| 1333 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1334 | case XFS_DINODE_FMT_BTREE: |
| 1335 | case XFS_DINODE_FMT_EXTENTS: |
| 1336 | break; |
| 1337 | default: |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 1338 | ASSERT(0); |
Darrick J. Wong | c2414ad | 2019-10-28 16:12:34 -0700 | [diff] [blame] | 1339 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1340 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1341 | |
| 1342 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1343 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1344 | if (error) |
| 1345 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1346 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1347 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1348 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1349 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1350 | return 0; |
| 1351 | } |
| 1352 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1353 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1354 | xfs_bmap_last_extent( |
| 1355 | struct xfs_trans *tp, |
| 1356 | struct xfs_inode *ip, |
| 1357 | int whichfork, |
| 1358 | struct xfs_bmbt_irec *rec, |
| 1359 | int *is_empty) |
| 1360 | { |
| 1361 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1362 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1363 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1364 | |
| 1365 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1366 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1367 | if (error) |
| 1368 | return error; |
| 1369 | } |
| 1370 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1371 | xfs_iext_last(ifp, &icur); |
| 1372 | if (!xfs_iext_get_extent(ifp, &icur, rec)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1373 | *is_empty = 1; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1374 | else |
| 1375 | *is_empty = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | * Check the last inode extent to determine whether this allocation will result |
| 1381 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1382 | * blocks at the end of the file which do not start at the previous data block, |
| 1383 | * we will try to align the new blocks at stripe unit boundaries. |
| 1384 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1385 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1386 | * at, or past the EOF. |
| 1387 | */ |
| 1388 | STATIC int |
| 1389 | xfs_bmap_isaeof( |
| 1390 | struct xfs_bmalloca *bma, |
| 1391 | int whichfork) |
| 1392 | { |
| 1393 | struct xfs_bmbt_irec rec; |
| 1394 | int is_empty; |
| 1395 | int error; |
| 1396 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1397 | bma->aeof = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1398 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1399 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1400 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1401 | return error; |
| 1402 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1403 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1404 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1405 | return 0; |
| 1406 | } |
| 1407 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1408 | /* |
| 1409 | * Check if we are allocation or past the last extent, or at least into |
| 1410 | * the last delayed allocated extent. |
| 1411 | */ |
| 1412 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1413 | (bma->offset >= rec.br_startoff && |
| 1414 | isnullstartblock(rec.br_startblock)); |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1419 | * Returns the file-relative block number of the first block past eof in |
| 1420 | * the file. This is not based on i_size, it is based on the extent records. |
| 1421 | * Returns 0 for local files, as they do not have extent records. |
| 1422 | */ |
| 1423 | int |
| 1424 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1425 | struct xfs_inode *ip, |
| 1426 | xfs_fileoff_t *last_block, |
| 1427 | int whichfork) |
| 1428 | { |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1429 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1430 | struct xfs_bmbt_irec rec; |
| 1431 | int is_empty; |
| 1432 | int error; |
| 1433 | |
| 1434 | *last_block = 0; |
| 1435 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1436 | if (ifp->if_format == XFS_DINODE_FMT_LOCAL) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1437 | return 0; |
| 1438 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1439 | if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp))) |
Darrick J. Wong | c2414ad | 2019-10-28 16:12:34 -0700 | [diff] [blame] | 1440 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1441 | |
| 1442 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1443 | if (error || is_empty) |
| 1444 | return error; |
| 1445 | |
| 1446 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Returns whether the selected fork of the inode has exactly one |
| 1452 | * block or not. For the data fork we check this matches di_size, |
| 1453 | * implying the file's range is 0..bsize-1. |
| 1454 | */ |
| 1455 | int /* 1=>1 block, 0=>otherwise */ |
| 1456 | xfs_bmap_one_block( |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1457 | struct xfs_inode *ip, /* incore inode */ |
| 1458 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1459 | { |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1460 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1461 | int rval; /* return value */ |
| 1462 | struct xfs_bmbt_irec s; /* internal version of extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1463 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1464 | |
| 1465 | #ifndef DEBUG |
| 1466 | if (whichfork == XFS_DATA_FORK) |
| 1467 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1468 | #endif /* !DEBUG */ |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1469 | if (ifp->if_nextents != 1) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1470 | return 0; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1471 | if (ifp->if_format != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1472 | return 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1473 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1474 | xfs_iext_first(ifp, &icur); |
| 1475 | xfs_iext_get_extent(ifp, &icur, &s); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1476 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1477 | if (rval && whichfork == XFS_DATA_FORK) |
| 1478 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1479 | return rval; |
| 1480 | } |
| 1481 | |
| 1482 | /* |
| 1483 | * Extent tree manipulation functions used during allocation. |
| 1484 | */ |
| 1485 | |
| 1486 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1487 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | */ |
| 1489 | STATIC int /* error */ |
| 1490 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1491 | struct xfs_bmalloca *bma, |
| 1492 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | { |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1494 | struct xfs_mount *mp = bma->ip->i_mount; |
| 1495 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1496 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | int i; /* temp state */ |
| 1499 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1500 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1501 | /* left is 0, right is 1, prev is 2 */ |
| 1502 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1503 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1504 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1505 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1506 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | int tmp_rval; /* partial logging flags */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1508 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1510 | ASSERT(whichfork != XFS_ATTR_FORK); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1511 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1512 | ASSERT(!bma->cur || |
Dave Chinner | 8ef5479 | 2020-03-10 17:54:38 -0700 | [diff] [blame] | 1513 | (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1514 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1515 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | #define LEFT r[0] |
| 1518 | #define RIGHT r[1] |
| 1519 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | |
| 1521 | /* |
| 1522 | * Set up a bunch of variables to make the tests simpler. |
| 1523 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1524 | xfs_iext_get_extent(ifp, &bma->icur, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1526 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1528 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1529 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1530 | da_old = startblockval(PREV.br_startblock); |
| 1531 | da_new = 0; |
| 1532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | /* |
| 1534 | * Set flags determining what part of the previous delayed allocation |
| 1535 | * extent is being replaced by a real allocation. |
| 1536 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1537 | if (PREV.br_startoff == new->br_startoff) |
| 1538 | state |= BMAP_LEFT_FILLING; |
| 1539 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1540 | state |= BMAP_RIGHT_FILLING; |
| 1541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | /* |
| 1543 | * Check and set flags if this segment has a left neighbor. |
| 1544 | * Don't set contiguous if the combined extent would be too large. |
| 1545 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1546 | if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1547 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1548 | if (isnullstartblock(LEFT.br_startblock)) |
| 1549 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1551 | |
| 1552 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1553 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1554 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1555 | LEFT.br_state == new->br_state && |
| 1556 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1557 | state |= BMAP_LEFT_CONTIG; |
| 1558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | /* |
| 1560 | * Check and set flags if this segment has a right neighbor. |
| 1561 | * Don't set contiguous if the combined extent would be too large. |
| 1562 | * Also check for all-three-contiguous being too large. |
| 1563 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1564 | if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1565 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1566 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1567 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1569 | |
| 1570 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1571 | new_endoff == RIGHT.br_startoff && |
| 1572 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1573 | new->br_state == RIGHT.br_state && |
| 1574 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1575 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1576 | BMAP_RIGHT_FILLING)) != |
| 1577 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1578 | BMAP_RIGHT_FILLING) || |
| 1579 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1580 | <= MAXEXTLEN)) |
| 1581 | state |= BMAP_RIGHT_CONTIG; |
| 1582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | error = 0; |
| 1584 | /* |
| 1585 | * Switch out based on the FILLING and CONTIG state bits. |
| 1586 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1587 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1588 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1589 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1590 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | /* |
| 1592 | * Filling in all of a previously delayed allocation extent. |
| 1593 | * The left and right neighbors are both contiguous with new. |
| 1594 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1595 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1596 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1597 | xfs_iext_remove(bma->ip, &bma->icur, state); |
| 1598 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1599 | xfs_iext_prev(ifp, &bma->icur); |
| 1600 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1601 | ifp->if_nextents--; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1602 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1603 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1605 | else { |
| 1606 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1607 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1608 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1610 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1611 | error = -EFSCORRUPTED; |
| 1612 | goto done; |
| 1613 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1614 | error = xfs_btree_delete(bma->cur, &i); |
| 1615 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1617 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1618 | error = -EFSCORRUPTED; |
| 1619 | goto done; |
| 1620 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1621 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1622 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1624 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1625 | error = -EFSCORRUPTED; |
| 1626 | goto done; |
| 1627 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1628 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1629 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | goto done; |
| 1631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | break; |
| 1633 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1634 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | /* |
| 1636 | * Filling in all of a previously delayed allocation extent. |
| 1637 | * The left neighbor is contiguous, the right is not. |
| 1638 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1639 | old = LEFT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1640 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1641 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1642 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1643 | xfs_iext_prev(ifp, &bma->icur); |
| 1644 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1645 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1646 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | rval = XFS_ILOG_DEXT; |
| 1648 | else { |
| 1649 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1650 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1651 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1653 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1654 | error = -EFSCORRUPTED; |
| 1655 | goto done; |
| 1656 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1657 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1658 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | goto done; |
| 1660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | break; |
| 1662 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1663 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | /* |
| 1665 | * Filling in all of a previously delayed allocation extent. |
Dave Chinner | 9230a0b | 2018-11-19 22:50:08 -0800 | [diff] [blame] | 1666 | * The right neighbor is contiguous, the left is not. Take care |
| 1667 | * with delay -> unwritten extent allocation here because the |
| 1668 | * delalloc record we are overwriting is always written. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1670 | PREV.br_startblock = new->br_startblock; |
| 1671 | PREV.br_blockcount += RIGHT.br_blockcount; |
Dave Chinner | 9230a0b | 2018-11-19 22:50:08 -0800 | [diff] [blame] | 1672 | PREV.br_state = new->br_state; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1673 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1674 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1675 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1676 | xfs_iext_prev(ifp, &bma->icur); |
| 1677 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1678 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1679 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | rval = XFS_ILOG_DEXT; |
| 1681 | else { |
| 1682 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1683 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1684 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1686 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1687 | error = -EFSCORRUPTED; |
| 1688 | goto done; |
| 1689 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1690 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1691 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | goto done; |
| 1693 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | break; |
| 1695 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1696 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | /* |
| 1698 | * Filling in all of a previously delayed allocation extent. |
| 1699 | * Neither the left nor right neighbors are contiguous with |
| 1700 | * the new one. |
| 1701 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1702 | PREV.br_startblock = new->br_startblock; |
| 1703 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1704 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1705 | ifp->if_nextents++; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1706 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1707 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1709 | else { |
| 1710 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1711 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1712 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1714 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 1715 | error = -EFSCORRUPTED; |
| 1716 | goto done; |
| 1717 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1718 | error = xfs_btree_insert(bma->cur, &i); |
| 1719 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1721 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1722 | error = -EFSCORRUPTED; |
| 1723 | goto done; |
| 1724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | break; |
| 1727 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1728 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | /* |
| 1730 | * Filling in the first part of a previous delayed allocation. |
| 1731 | * The left neighbor is contiguous. |
| 1732 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1733 | old = LEFT; |
| 1734 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1735 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1736 | startblockval(PREV.br_startblock)); |
| 1737 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1738 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1739 | |
Christoph Hellwig | bf99971 | 2017-11-03 10:34:38 -0700 | [diff] [blame] | 1740 | PREV.br_blockcount = temp; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1741 | PREV.br_startoff += new->br_blockcount; |
| 1742 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1743 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1744 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1745 | xfs_iext_prev(ifp, &bma->icur); |
| 1746 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1747 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1748 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | rval = XFS_ILOG_DEXT; |
| 1750 | else { |
| 1751 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1752 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1753 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1755 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1756 | error = -EFSCORRUPTED; |
| 1757 | goto done; |
| 1758 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1759 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1760 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | goto done; |
| 1762 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | break; |
| 1764 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1765 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | /* |
| 1767 | * Filling in the first part of a previous delayed allocation. |
| 1768 | * The left neighbor is not contiguous. |
| 1769 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1770 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1771 | ifp->if_nextents++; |
| 1772 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1773 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1775 | else { |
| 1776 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1777 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1778 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1780 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 1781 | error = -EFSCORRUPTED; |
| 1782 | goto done; |
| 1783 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1784 | error = xfs_btree_insert(bma->cur, &i); |
| 1785 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1787 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1788 | error = -EFSCORRUPTED; |
| 1789 | goto done; |
| 1790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1792 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1793 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1794 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1795 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | rval |= tmp_rval; |
| 1797 | if (error) |
| 1798 | goto done; |
| 1799 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1800 | |
| 1801 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1802 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1803 | startblockval(PREV.br_startblock) - |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 1804 | (bma->cur ? bma->cur->bc_ino.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1805 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1806 | PREV.br_startoff = new_endoff; |
| 1807 | PREV.br_blockcount = temp; |
| 1808 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1809 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1810 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1811 | xfs_iext_prev(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | break; |
| 1813 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1814 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | /* |
| 1816 | * Filling in the last part of a previous delayed allocation. |
| 1817 | * The right neighbor is contiguous with the new allocation. |
| 1818 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1819 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1820 | RIGHT.br_startoff = new->br_startoff; |
| 1821 | RIGHT.br_startblock = new->br_startblock; |
| 1822 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1823 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1824 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | rval = XFS_ILOG_DEXT; |
| 1826 | else { |
| 1827 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1828 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1829 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1831 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1832 | error = -EFSCORRUPTED; |
| 1833 | goto done; |
| 1834 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1835 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1836 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | goto done; |
| 1838 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1839 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1840 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1841 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1842 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1843 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1844 | PREV.br_blockcount = temp; |
| 1845 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1846 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1847 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1848 | xfs_iext_next(ifp, &bma->icur); |
| 1849 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | break; |
| 1851 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1852 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | /* |
| 1854 | * Filling in the last part of a previous delayed allocation. |
| 1855 | * The right neighbor is not contiguous. |
| 1856 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1857 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1858 | ifp->if_nextents++; |
| 1859 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1860 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1862 | else { |
| 1863 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1864 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1865 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1867 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 1868 | error = -EFSCORRUPTED; |
| 1869 | goto done; |
| 1870 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1871 | error = xfs_btree_insert(bma->cur, &i); |
| 1872 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1874 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1875 | error = -EFSCORRUPTED; |
| 1876 | goto done; |
| 1877 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1879 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1880 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1881 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1882 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | rval |= tmp_rval; |
| 1884 | if (error) |
| 1885 | goto done; |
| 1886 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1887 | |
| 1888 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1889 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1890 | startblockval(PREV.br_startblock) - |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 1891 | (bma->cur ? bma->cur->bc_ino.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1892 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1893 | PREV.br_startblock = nullstartblock(da_new); |
| 1894 | PREV.br_blockcount = temp; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1895 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1896 | xfs_iext_next(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | break; |
| 1898 | |
| 1899 | case 0: |
| 1900 | /* |
| 1901 | * Filling in the middle part of a previous delayed allocation. |
| 1902 | * Contiguity is impossible here. |
| 1903 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1904 | * |
| 1905 | * We start with a delayed allocation: |
| 1906 | * |
| 1907 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1908 | * PREV @ idx |
| 1909 | * |
| 1910 | * and we are allocating: |
| 1911 | * +rrrrrrrrrrrrrrrrr+ |
| 1912 | * new |
| 1913 | * |
| 1914 | * and we set it up for insertion as: |
| 1915 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1916 | * new |
| 1917 | * PREV @ idx LEFT RIGHT |
| 1918 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1920 | old = PREV; |
| 1921 | |
| 1922 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1923 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1924 | |
| 1925 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1926 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1927 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1928 | RIGHT.br_blockcount = |
| 1929 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1930 | RIGHT.br_startblock = |
| 1931 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1932 | RIGHT.br_blockcount)); |
| 1933 | |
| 1934 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1935 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1936 | PREV.br_startblock = |
| 1937 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1938 | PREV.br_blockcount)); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1939 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1940 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1941 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1942 | xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); |
| 1943 | xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1944 | ifp->if_nextents++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1945 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1946 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1948 | else { |
| 1949 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1950 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1951 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1953 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 1954 | error = -EFSCORRUPTED; |
| 1955 | goto done; |
| 1956 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1957 | error = xfs_btree_insert(bma->cur, &i); |
| 1958 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1960 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1961 | error = -EFSCORRUPTED; |
| 1962 | goto done; |
| 1963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1965 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1966 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1967 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1968 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | rval |= tmp_rval; |
| 1970 | if (error) |
| 1971 | goto done; |
| 1972 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1973 | |
| 1974 | da_new = startblockval(PREV.br_startblock) + |
| 1975 | startblockval(RIGHT.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | break; |
| 1977 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1978 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1979 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1980 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1981 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1982 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1983 | case BMAP_LEFT_CONTIG: |
| 1984 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | /* |
| 1986 | * These cases are all impossible. |
| 1987 | */ |
| 1988 | ASSERT(0); |
| 1989 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1990 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 1991 | /* add reverse mapping unless caller opted out */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 1992 | if (!(bma->flags & XFS_BMAPI_NORMAP)) |
| 1993 | xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 1994 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1995 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1996 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1997 | int tmp_logflags; /* partial log flag return val */ |
| 1998 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1999 | ASSERT(bma->cur == NULL); |
| 2000 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 2001 | &bma->cur, da_old > 0, &tmp_logflags, |
| 2002 | whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2003 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2004 | if (error) |
| 2005 | goto done; |
| 2006 | } |
| 2007 | |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 2008 | if (da_new != da_old) |
| 2009 | xfs_mod_delalloc(mp, (int64_t)da_new - da_old); |
| 2010 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2011 | if (bma->cur) { |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 2012 | da_new += bma->cur->bc_ino.allocated; |
| 2013 | bma->cur->bc_ino.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2016 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2017 | if (da_new != da_old) { |
| 2018 | ASSERT(state == 0 || da_new < da_old); |
| 2019 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2020 | false); |
| 2021 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2022 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2023 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2025 | if (whichfork != XFS_COW_FORK) |
| 2026 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | return error; |
| 2028 | #undef LEFT |
| 2029 | #undef RIGHT |
| 2030 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2034 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | */ |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 2036 | int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2038 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2040 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2041 | struct xfs_iext_cursor *icur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2043 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2044 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | int i; /* temp state */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 2049 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2052 | /* left is 0, right is 1, prev is 2 */ |
| 2053 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2054 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2055 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2056 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2058 | *logflagsp = 0; |
| 2059 | |
| 2060 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2061 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2062 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2063 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2064 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2065 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | #define LEFT r[0] |
| 2068 | #define RIGHT r[1] |
| 2069 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | /* |
| 2072 | * Set up a bunch of variables to make the tests simpler. |
| 2073 | */ |
| 2074 | error = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2075 | xfs_iext_get_extent(ifp, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2076 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2078 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2079 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | /* |
| 2082 | * Set flags determining what part of the previous oldext allocation |
| 2083 | * extent is being replaced by a newext allocation. |
| 2084 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2085 | if (PREV.br_startoff == new->br_startoff) |
| 2086 | state |= BMAP_LEFT_FILLING; |
| 2087 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2088 | state |= BMAP_RIGHT_FILLING; |
| 2089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | /* |
| 2091 | * Check and set flags if this segment has a left neighbor. |
| 2092 | * Don't set contiguous if the combined extent would be too large. |
| 2093 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2094 | if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2095 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2096 | if (isnullstartblock(LEFT.br_startblock)) |
| 2097 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2099 | |
| 2100 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2101 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2102 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2103 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2104 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2105 | state |= BMAP_LEFT_CONTIG; |
| 2106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | /* |
| 2108 | * Check and set flags if this segment has a right neighbor. |
| 2109 | * Don't set contiguous if the combined extent would be too large. |
| 2110 | * Also check for all-three-contiguous being too large. |
| 2111 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2112 | if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2113 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2114 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2115 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2117 | |
| 2118 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2119 | new_endoff == RIGHT.br_startoff && |
| 2120 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2121 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2122 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2123 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2124 | BMAP_RIGHT_FILLING)) != |
| 2125 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2126 | BMAP_RIGHT_FILLING) || |
| 2127 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2128 | <= MAXEXTLEN)) |
| 2129 | state |= BMAP_RIGHT_CONTIG; |
| 2130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | /* |
| 2132 | * Switch out based on the FILLING and CONTIG state bits. |
| 2133 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2134 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2135 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2136 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2137 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | /* |
| 2139 | * Setting all of a previous oldext extent to newext. |
| 2140 | * The left and right neighbors are both contiguous with new. |
| 2141 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2142 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2143 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2144 | xfs_iext_remove(ip, icur, state); |
| 2145 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2146 | xfs_iext_prev(ifp, icur); |
| 2147 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2148 | ifp->if_nextents -= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | if (cur == NULL) |
| 2150 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2151 | else { |
| 2152 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2153 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2154 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2156 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2157 | error = -EFSCORRUPTED; |
| 2158 | goto done; |
| 2159 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2160 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2162 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2163 | error = -EFSCORRUPTED; |
| 2164 | goto done; |
| 2165 | } |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2166 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2168 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2169 | error = -EFSCORRUPTED; |
| 2170 | goto done; |
| 2171 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2172 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2174 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2175 | error = -EFSCORRUPTED; |
| 2176 | goto done; |
| 2177 | } |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2178 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2180 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2181 | error = -EFSCORRUPTED; |
| 2182 | goto done; |
| 2183 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2184 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2185 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | goto done; |
| 2187 | } |
| 2188 | break; |
| 2189 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2190 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | /* |
| 2192 | * Setting all of a previous oldext extent to newext. |
| 2193 | * The left neighbor is contiguous, the right is not. |
| 2194 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2195 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2196 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2197 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2198 | xfs_iext_prev(ifp, icur); |
| 2199 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2200 | ifp->if_nextents--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | if (cur == NULL) |
| 2202 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2203 | else { |
| 2204 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2205 | error = xfs_bmbt_lookup_eq(cur, &PREV, &i); |
| 2206 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2208 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2209 | error = -EFSCORRUPTED; |
| 2210 | goto done; |
| 2211 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2212 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2214 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2215 | error = -EFSCORRUPTED; |
| 2216 | goto done; |
| 2217 | } |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2218 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2220 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2221 | error = -EFSCORRUPTED; |
| 2222 | goto done; |
| 2223 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2224 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2225 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | goto done; |
| 2227 | } |
| 2228 | break; |
| 2229 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2230 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | /* |
| 2232 | * Setting all of a previous oldext extent to newext. |
| 2233 | * The right neighbor is contiguous, the left is not. |
| 2234 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2235 | PREV.br_blockcount += RIGHT.br_blockcount; |
| 2236 | PREV.br_state = new->br_state; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2237 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2238 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2239 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2240 | xfs_iext_prev(ifp, icur); |
| 2241 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2242 | ifp->if_nextents--; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | if (cur == NULL) |
| 2245 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2246 | else { |
| 2247 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2248 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2249 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2251 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2252 | error = -EFSCORRUPTED; |
| 2253 | goto done; |
| 2254 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2255 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2257 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2258 | error = -EFSCORRUPTED; |
| 2259 | goto done; |
| 2260 | } |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2261 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2263 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2264 | error = -EFSCORRUPTED; |
| 2265 | goto done; |
| 2266 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2267 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2268 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | goto done; |
| 2270 | } |
| 2271 | break; |
| 2272 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2273 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | /* |
| 2275 | * Setting all of a previous oldext extent to newext. |
| 2276 | * Neither the left nor right neighbors are contiguous with |
| 2277 | * the new one. |
| 2278 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2279 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2280 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | if (cur == NULL) |
| 2283 | rval = XFS_ILOG_DEXT; |
| 2284 | else { |
| 2285 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2286 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2287 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2289 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2290 | error = -EFSCORRUPTED; |
| 2291 | goto done; |
| 2292 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2293 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2294 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | goto done; |
| 2296 | } |
| 2297 | break; |
| 2298 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2299 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | /* |
| 2301 | * Setting the first part of a previous oldext extent to newext. |
| 2302 | * The left neighbor is contiguous. |
| 2303 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2304 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2305 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2306 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2307 | PREV.br_startoff += new->br_blockcount; |
| 2308 | PREV.br_startblock += new->br_blockcount; |
| 2309 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2310 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2311 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2312 | xfs_iext_prev(ifp, icur); |
| 2313 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | if (cur == NULL) |
| 2316 | rval = XFS_ILOG_DEXT; |
| 2317 | else { |
| 2318 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2319 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2320 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2322 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2323 | error = -EFSCORRUPTED; |
| 2324 | goto done; |
| 2325 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2326 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2327 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2329 | error = xfs_btree_decrement(cur, 0, &i); |
| 2330 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2332 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2333 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | goto done; |
| 2335 | } |
| 2336 | break; |
| 2337 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2338 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | /* |
| 2340 | * Setting the first part of a previous oldext extent to newext. |
| 2341 | * The left neighbor is not contiguous. |
| 2342 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2343 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2344 | PREV.br_startoff += new->br_blockcount; |
| 2345 | PREV.br_startblock += new->br_blockcount; |
| 2346 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2347 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2348 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2349 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2350 | ifp->if_nextents++; |
| 2351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | if (cur == NULL) |
| 2353 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2354 | else { |
| 2355 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2356 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2357 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2359 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2360 | error = -EFSCORRUPTED; |
| 2361 | goto done; |
| 2362 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2363 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2364 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | goto done; |
| 2366 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2367 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2369 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2370 | error = -EFSCORRUPTED; |
| 2371 | goto done; |
| 2372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | } |
| 2374 | break; |
| 2375 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2376 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | /* |
| 2378 | * Setting the last part of a previous oldext extent to newext. |
| 2379 | * The right neighbor is contiguous with the new allocation. |
| 2380 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2381 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2382 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2383 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2384 | RIGHT.br_startoff = new->br_startoff; |
| 2385 | RIGHT.br_startblock = new->br_startblock; |
| 2386 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2387 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2388 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2389 | xfs_iext_next(ifp, icur); |
| 2390 | xfs_iext_update_extent(ip, state, icur, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | if (cur == NULL) |
| 2393 | rval = XFS_ILOG_DEXT; |
| 2394 | else { |
| 2395 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2396 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2397 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2399 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2400 | error = -EFSCORRUPTED; |
| 2401 | goto done; |
| 2402 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2403 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2404 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2406 | error = xfs_btree_increment(cur, 0, &i); |
| 2407 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2409 | error = xfs_bmbt_update(cur, &RIGHT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2410 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | goto done; |
| 2412 | } |
| 2413 | break; |
| 2414 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2415 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | /* |
| 2417 | * Setting the last part of a previous oldext extent to newext. |
| 2418 | * The right neighbor is not contiguous. |
| 2419 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2420 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2421 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2422 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2423 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2424 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2425 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2426 | ifp->if_nextents++; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | if (cur == NULL) |
| 2429 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2430 | else { |
| 2431 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2432 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2433 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2435 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2436 | error = -EFSCORRUPTED; |
| 2437 | goto done; |
| 2438 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2439 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2440 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2442 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2443 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2445 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 2446 | error = -EFSCORRUPTED; |
| 2447 | goto done; |
| 2448 | } |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2449 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2451 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2452 | error = -EFSCORRUPTED; |
| 2453 | goto done; |
| 2454 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | } |
| 2456 | break; |
| 2457 | |
| 2458 | case 0: |
| 2459 | /* |
| 2460 | * Setting the middle part of a previous oldext extent to |
| 2461 | * newext. Contiguity is impossible here. |
| 2462 | * One extent becomes three extents. |
| 2463 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2464 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2465 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | r[0] = *new; |
| 2468 | r[1].br_startoff = new_endoff; |
| 2469 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2470 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2472 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2473 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2474 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2475 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2476 | xfs_iext_insert(ip, icur, &r[1], state); |
| 2477 | xfs_iext_insert(ip, icur, &r[0], state); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2478 | ifp->if_nextents += 2; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | if (cur == NULL) |
| 2481 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2482 | else { |
| 2483 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2484 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2485 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2487 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2488 | error = -EFSCORRUPTED; |
| 2489 | goto done; |
| 2490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2492 | error = xfs_bmbt_update(cur, &r[1]); |
| 2493 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | goto done; |
| 2495 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2497 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2499 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2500 | error = -EFSCORRUPTED; |
| 2501 | goto done; |
| 2502 | } |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2503 | /* |
| 2504 | * Reset the cursor to the position of the new extent |
| 2505 | * we are about to insert as we can't trust it after |
| 2506 | * the previous insert. |
| 2507 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2508 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2509 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2511 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 2512 | error = -EFSCORRUPTED; |
| 2513 | goto done; |
| 2514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2516 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2518 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2519 | error = -EFSCORRUPTED; |
| 2520 | goto done; |
| 2521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | } |
| 2523 | break; |
| 2524 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2525 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2526 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2527 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2528 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2529 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2530 | case BMAP_LEFT_CONTIG: |
| 2531 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | /* |
| 2533 | * These cases are all impossible. |
| 2534 | */ |
| 2535 | ASSERT(0); |
| 2536 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2537 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2538 | /* update reverse mappings */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 2539 | xfs_rmap_convert_extent(mp, tp, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2540 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2541 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2542 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2543 | int tmp_logflags; /* partial log flag return val */ |
| 2544 | |
| 2545 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 2546 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 2547 | &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2548 | *logflagsp |= tmp_logflags; |
| 2549 | if (error) |
| 2550 | goto done; |
| 2551 | } |
| 2552 | |
| 2553 | /* clear out the allocated field, done with it now in any case. */ |
| 2554 | if (cur) { |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 2555 | cur->bc_ino.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2556 | *curp = cur; |
| 2557 | } |
| 2558 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2559 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2561 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | return error; |
| 2563 | #undef LEFT |
| 2564 | #undef RIGHT |
| 2565 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2569 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2571 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | xfs_bmap_add_extent_hole_delay( |
| 2573 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2574 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2575 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2576 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | { |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 2578 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2580 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2581 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2582 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2583 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2584 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2586 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2587 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | /* |
| 2590 | * Check and set flags if this segment has a left neighbor |
| 2591 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2592 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2593 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2594 | if (isnullstartblock(left.br_startblock)) |
| 2595 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | /* |
| 2599 | * Check and set flags if the current (right) segment exists. |
| 2600 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2601 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2602 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2603 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2604 | if (isnullstartblock(right.br_startblock)) |
| 2605 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | /* |
| 2609 | * Set contiguity flags on the left and right neighbors. |
| 2610 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2611 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2612 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2613 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2614 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2615 | state |= BMAP_LEFT_CONTIG; |
| 2616 | |
| 2617 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2618 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2619 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2620 | (!(state & BMAP_LEFT_CONTIG) || |
| 2621 | (left.br_blockcount + new->br_blockcount + |
| 2622 | right.br_blockcount <= MAXEXTLEN))) |
| 2623 | state |= BMAP_RIGHT_CONTIG; |
| 2624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | /* |
| 2626 | * Switch out based on the contiguity flags. |
| 2627 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2628 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2629 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | /* |
| 2631 | * New allocation is contiguous with delayed allocations |
| 2632 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2633 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | */ |
| 2635 | temp = left.br_blockcount + new->br_blockcount + |
| 2636 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2637 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2638 | oldlen = startblockval(left.br_startblock) + |
| 2639 | startblockval(new->br_startblock) + |
| 2640 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2641 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2642 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2643 | left.br_startblock = nullstartblock(newlen); |
| 2644 | left.br_blockcount = temp; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2645 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2646 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2647 | xfs_iext_prev(ifp, icur); |
| 2648 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | break; |
| 2650 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2651 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | /* |
| 2653 | * New allocation is contiguous with a delayed allocation |
| 2654 | * on the left. |
| 2655 | * Merge the new allocation with the left neighbor. |
| 2656 | */ |
| 2657 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2658 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2659 | oldlen = startblockval(left.br_startblock) + |
| 2660 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2661 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2662 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2663 | left.br_blockcount = temp; |
| 2664 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | 41d196f | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 2665 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2666 | xfs_iext_prev(ifp, icur); |
| 2667 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | break; |
| 2669 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2670 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | /* |
| 2672 | * New allocation is contiguous with a delayed allocation |
| 2673 | * on the right. |
| 2674 | * Merge the new allocation with the right neighbor. |
| 2675 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2677 | oldlen = startblockval(new->br_startblock) + |
| 2678 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2679 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2680 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2681 | right.br_startoff = new->br_startoff; |
| 2682 | right.br_startblock = nullstartblock(newlen); |
| 2683 | right.br_blockcount = temp; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2684 | xfs_iext_update_extent(ip, state, icur, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | break; |
| 2686 | |
| 2687 | case 0: |
| 2688 | /* |
| 2689 | * New allocation is not contiguous with another |
| 2690 | * delayed allocation. |
| 2691 | * Insert a new entry. |
| 2692 | */ |
| 2693 | oldlen = newlen = 0; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2694 | xfs_iext_insert(ip, icur, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | break; |
| 2696 | } |
| 2697 | if (oldlen != newlen) { |
| 2698 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2699 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2700 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | /* |
| 2702 | * Nothing to do for disk quota accounting here. |
| 2703 | */ |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 2704 | xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2709 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | */ |
| 2711 | STATIC int /* error */ |
| 2712 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2713 | struct xfs_trans *tp, |
| 2714 | struct xfs_inode *ip, |
| 2715 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2716 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2717 | struct xfs_btree_cur **curp, |
| 2718 | struct xfs_bmbt_irec *new, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2719 | int *logflagsp, |
| 2720 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2722 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2723 | struct xfs_mount *mp = ip->i_mount; |
| 2724 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2728 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2729 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2730 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2731 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2733 | ASSERT(!isnullstartblock(new->br_startblock)); |
Dave Chinner | 8ef5479 | 2020-03-10 17:54:38 -0700 | [diff] [blame] | 2734 | ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2735 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2736 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | /* |
| 2739 | * Check and set flags if this segment has a left neighbor. |
| 2740 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2741 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2742 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2743 | if (isnullstartblock(left.br_startblock)) |
| 2744 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | /* |
| 2748 | * Check and set flags if this segment has a current value. |
| 2749 | * Not true if we're inserting into the "hole" at eof. |
| 2750 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2751 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2752 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2753 | if (isnullstartblock(right.br_startblock)) |
| 2754 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | /* |
| 2758 | * We're inserting a real allocation between "left" and "right". |
| 2759 | * Set the contiguity flags. Don't let extents get too large. |
| 2760 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2761 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2762 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2763 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2764 | left.br_state == new->br_state && |
| 2765 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2766 | state |= BMAP_LEFT_CONTIG; |
| 2767 | |
| 2768 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2769 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2770 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2771 | new->br_state == right.br_state && |
| 2772 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2773 | (!(state & BMAP_LEFT_CONTIG) || |
| 2774 | left.br_blockcount + new->br_blockcount + |
| 2775 | right.br_blockcount <= MAXEXTLEN)) |
| 2776 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2778 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | /* |
| 2780 | * Select which case we're in here, and implement it. |
| 2781 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2782 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2783 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | /* |
| 2785 | * New allocation is contiguous with real allocations on the |
| 2786 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2787 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2789 | left.br_blockcount += new->br_blockcount + right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2790 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2791 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2792 | xfs_iext_prev(ifp, icur); |
| 2793 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2794 | ifp->if_nextents--; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2795 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2796 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2797 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2798 | } else { |
| 2799 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2800 | error = xfs_bmbt_lookup_eq(cur, &right, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2801 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2802 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2803 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2804 | error = -EFSCORRUPTED; |
| 2805 | goto done; |
| 2806 | } |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2807 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2808 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2809 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2810 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2811 | error = -EFSCORRUPTED; |
| 2812 | goto done; |
| 2813 | } |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2814 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2815 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2816 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2817 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2818 | error = -EFSCORRUPTED; |
| 2819 | goto done; |
| 2820 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2821 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2822 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2823 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2825 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2827 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | /* |
| 2829 | * New allocation is contiguous with a real allocation |
| 2830 | * on the left. |
| 2831 | * Merge the new allocation with the left neighbor. |
| 2832 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2833 | old = left; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2834 | left.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 1d2e008 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2835 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2836 | xfs_iext_prev(ifp, icur); |
| 2837 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2838 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2839 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2840 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2841 | } else { |
| 2842 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2843 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2844 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2845 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2846 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2847 | error = -EFSCORRUPTED; |
| 2848 | goto done; |
| 2849 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2850 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2851 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2852 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2854 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2856 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | /* |
| 2858 | * New allocation is contiguous with a real allocation |
| 2859 | * on the right. |
| 2860 | * Merge the new allocation with the right neighbor. |
| 2861 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2862 | old = right; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2863 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2864 | right.br_startoff = new->br_startoff; |
| 2865 | right.br_startblock = new->br_startblock; |
| 2866 | right.br_blockcount += new->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2867 | xfs_iext_update_extent(ip, state, icur, &right); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2868 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2869 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2870 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2871 | } else { |
| 2872 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2873 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2874 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2875 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2876 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2877 | error = -EFSCORRUPTED; |
| 2878 | goto done; |
| 2879 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2880 | error = xfs_bmbt_update(cur, &right); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2881 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2882 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2884 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | |
| 2886 | case 0: |
| 2887 | /* |
| 2888 | * New allocation is not contiguous with another |
| 2889 | * real allocation. |
| 2890 | * Insert a new entry. |
| 2891 | */ |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2892 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2893 | ifp->if_nextents++; |
| 2894 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2895 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2896 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2897 | } else { |
| 2898 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2899 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2900 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2901 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2902 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 2903 | error = -EFSCORRUPTED; |
| 2904 | goto done; |
| 2905 | } |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2906 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2907 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2908 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2909 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2910 | error = -EFSCORRUPTED; |
| 2911 | goto done; |
| 2912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2914 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2916 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2917 | /* add reverse mapping unless caller opted out */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 2918 | if (!(flags & XFS_BMAPI_NORMAP)) |
| 2919 | xfs_rmap_map_extent(tp, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2920 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2921 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2922 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2923 | int tmp_logflags; /* partial log flag return val */ |
| 2924 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2925 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 2926 | error = xfs_bmap_extents_to_btree(tp, ip, curp, 0, |
| 2927 | &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2928 | *logflagsp |= tmp_logflags; |
| 2929 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2930 | if (error) |
| 2931 | goto done; |
| 2932 | } |
| 2933 | |
| 2934 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2935 | if (cur) |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 2936 | cur->bc_ino.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2937 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2938 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2939 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2940 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2941 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | } |
| 2943 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2944 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2945 | * Functions used in the extent read, allocate and remove paths |
| 2946 | */ |
| 2947 | |
| 2948 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2949 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2950 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2951 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2952 | xfs_bmap_extsize_align( |
| 2953 | xfs_mount_t *mp, |
| 2954 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2955 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2956 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2957 | int rt, /* is this a realtime inode? */ |
| 2958 | int eof, /* is extent at end-of-file? */ |
| 2959 | int delay, /* creating delalloc extent? */ |
| 2960 | int convert, /* overwriting unwritten extent? */ |
| 2961 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2962 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2963 | { |
| 2964 | xfs_fileoff_t orig_off; /* original offset */ |
| 2965 | xfs_extlen_t orig_alen; /* original length */ |
| 2966 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2967 | xfs_fileoff_t nexto; /* next file offset */ |
| 2968 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2969 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2970 | xfs_extlen_t align_alen; /* temp for length */ |
| 2971 | xfs_extlen_t temp; /* temp for calculations */ |
| 2972 | |
| 2973 | if (convert) |
| 2974 | return 0; |
| 2975 | |
| 2976 | orig_off = align_off = *offp; |
| 2977 | orig_alen = align_alen = *lenp; |
| 2978 | orig_end = orig_off + orig_alen; |
| 2979 | |
| 2980 | /* |
| 2981 | * If this request overlaps an existing extent, then don't |
| 2982 | * attempt to perform any additional alignment. |
| 2983 | */ |
| 2984 | if (!delay && !eof && |
| 2985 | (orig_off >= gotp->br_startoff) && |
| 2986 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | /* |
| 2991 | * If the file offset is unaligned vs. the extent size |
| 2992 | * we need to align it. This will be possible unless |
| 2993 | * the file was previously written with a kernel that didn't |
| 2994 | * perform this alignment, or if a truncate shot us in the |
| 2995 | * foot. |
| 2996 | */ |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 2997 | div_u64_rem(orig_off, extsz, &temp); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2998 | if (temp) { |
| 2999 | align_alen += temp; |
| 3000 | align_off -= temp; |
| 3001 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3002 | |
| 3003 | /* Same adjustment for the end of the requested area. */ |
| 3004 | temp = (align_alen % extsz); |
| 3005 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3006 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3007 | |
| 3008 | /* |
| 3009 | * For large extent hint sizes, the aligned extent might be larger than |
| 3010 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 3011 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 3012 | * short allocation just fine, so it is safe to do this. We only want to |
| 3013 | * do it when we are forced to, though, because it means more allocation |
| 3014 | * operations are required. |
| 3015 | */ |
| 3016 | while (align_alen > MAXEXTLEN) |
| 3017 | align_alen -= extsz; |
| 3018 | ASSERT(align_alen <= MAXEXTLEN); |
| 3019 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3020 | /* |
| 3021 | * If the previous block overlaps with this proposed allocation |
| 3022 | * then move the start forward without adjusting the length. |
| 3023 | */ |
| 3024 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3025 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3026 | prevo = prevp->br_startoff; |
| 3027 | else |
| 3028 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3029 | } else |
| 3030 | prevo = 0; |
| 3031 | if (align_off != orig_off && align_off < prevo) |
| 3032 | align_off = prevo; |
| 3033 | /* |
| 3034 | * If the next block overlaps with this proposed allocation |
| 3035 | * then move the start back without adjusting the length, |
| 3036 | * but not before offset 0. |
| 3037 | * This may of course make the start overlap previous block, |
| 3038 | * and if we hit the offset 0 limit then the next block |
| 3039 | * can still overlap too. |
| 3040 | */ |
| 3041 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3042 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3043 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3044 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3045 | else |
| 3046 | nexto = gotp->br_startoff; |
| 3047 | } else |
| 3048 | nexto = NULLFILEOFF; |
| 3049 | if (!eof && |
| 3050 | align_off + align_alen != orig_end && |
| 3051 | align_off + align_alen > nexto) |
| 3052 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3053 | /* |
| 3054 | * If we're now overlapping the next or previous extent that |
| 3055 | * means we can't fit an extsz piece in this hole. Just move |
| 3056 | * the start forward to the first valid spot and set |
| 3057 | * the length so we hit the end. |
| 3058 | */ |
| 3059 | if (align_off != orig_off && align_off < prevo) |
| 3060 | align_off = prevo; |
| 3061 | if (align_off + align_alen != orig_end && |
| 3062 | align_off + align_alen > nexto && |
| 3063 | nexto != NULLFILEOFF) { |
| 3064 | ASSERT(nexto > prevo); |
| 3065 | align_alen = nexto - align_off; |
| 3066 | } |
| 3067 | |
| 3068 | /* |
| 3069 | * If realtime, and the result isn't a multiple of the realtime |
| 3070 | * extent size we need to remove blocks until it is. |
| 3071 | */ |
| 3072 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3073 | /* |
| 3074 | * We're not covering the original request, or |
| 3075 | * we won't be able to once we fix the length. |
| 3076 | */ |
| 3077 | if (orig_off < align_off || |
| 3078 | orig_end > align_off + align_alen || |
| 3079 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3080 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3081 | /* |
| 3082 | * Try to fix it by moving the start up. |
| 3083 | */ |
| 3084 | if (align_off + temp <= orig_off) { |
| 3085 | align_alen -= temp; |
| 3086 | align_off += temp; |
| 3087 | } |
| 3088 | /* |
| 3089 | * Try to fix it by moving the end in. |
| 3090 | */ |
| 3091 | else if (align_off + align_alen - temp >= orig_end) |
| 3092 | align_alen -= temp; |
| 3093 | /* |
| 3094 | * Set the start to the minimum then trim the length. |
| 3095 | */ |
| 3096 | else { |
| 3097 | align_alen -= orig_off - align_off; |
| 3098 | align_off = orig_off; |
| 3099 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3100 | } |
| 3101 | /* |
| 3102 | * Result doesn't cover the request, fail it. |
| 3103 | */ |
| 3104 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3105 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3106 | } else { |
| 3107 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3108 | /* see MAXEXTLEN handling above */ |
| 3109 | ASSERT(orig_end <= align_off + align_alen || |
| 3110 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | #ifdef DEBUG |
| 3114 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3115 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3116 | if (prevp->br_startoff != NULLFILEOFF) |
| 3117 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3118 | #endif |
| 3119 | |
| 3120 | *lenp = align_alen; |
| 3121 | *offp = align_off; |
| 3122 | return 0; |
| 3123 | } |
| 3124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3126 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3127 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3128 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3129 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | { |
| 3131 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3133 | xfs_mount_t *mp; /* mount point structure */ |
| 3134 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3135 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | |
| 3137 | #define ISVALID(x,y) \ |
| 3138 | (rt ? \ |
| 3139 | (x) < mp->m_sb.sb_rblocks : \ |
| 3140 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3141 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3142 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | mp = ap->ip->i_mount; |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3145 | nullfb = ap->tp->t_firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3146 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
Christoph Hellwig | c34d570 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 3147 | (ap->datatype & XFS_ALLOC_USERDATA); |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3148 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, |
| 3149 | ap->tp->t_firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | /* |
| 3151 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3152 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3154 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3155 | !isnullstartblock(ap->prev.br_startblock) && |
| 3156 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3157 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3158 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | /* |
| 3160 | * Adjust for the gap between prevp and us. |
| 3161 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3162 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3163 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3165 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3166 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | } |
| 3168 | /* |
| 3169 | * If not at eof, then compare the two neighbor blocks. |
| 3170 | * Figure out whether either one gives us a good starting point, |
| 3171 | * and pick the better one. |
| 3172 | */ |
| 3173 | else if (!ap->eof) { |
| 3174 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3175 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3176 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3177 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3178 | |
| 3179 | /* |
| 3180 | * If there's a previous (left) block, select a requested |
| 3181 | * start block based on it. |
| 3182 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3183 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3184 | !isnullstartblock(ap->prev.br_startblock) && |
| 3185 | (prevbno = ap->prev.br_startblock + |
| 3186 | ap->prev.br_blockcount) && |
| 3187 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3188 | /* |
| 3189 | * Calculate gap to end of previous block. |
| 3190 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3191 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3192 | (ap->prev.br_startoff + |
| 3193 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | /* |
| 3195 | * Figure the startblock based on the previous block's |
| 3196 | * end and the gap size. |
| 3197 | * Heuristic! |
| 3198 | * If the gap is large relative to the piece we're |
| 3199 | * allocating, or using it gives us an invalid block |
| 3200 | * number, then just use the end of the previous block. |
| 3201 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3202 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3204 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | prevbno += adjust; |
| 3206 | else |
| 3207 | prevdiff += adjust; |
| 3208 | /* |
| 3209 | * If the firstblock forbids it, can't use it, |
| 3210 | * must use default. |
| 3211 | */ |
| 3212 | if (!rt && !nullfb && |
| 3213 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3214 | prevbno = NULLFSBLOCK; |
| 3215 | } |
| 3216 | /* |
| 3217 | * No previous block or can't follow it, just default. |
| 3218 | */ |
| 3219 | else |
| 3220 | prevbno = NULLFSBLOCK; |
| 3221 | /* |
| 3222 | * If there's a following (right) block, select a requested |
| 3223 | * start block based on it. |
| 3224 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3225 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | /* |
| 3227 | * Calculate gap to start of next block. |
| 3228 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3229 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | /* |
| 3231 | * Figure the startblock based on the next block's |
| 3232 | * start and the gap size. |
| 3233 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3234 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | /* |
| 3236 | * Heuristic! |
| 3237 | * If the gap is large relative to the piece we're |
| 3238 | * allocating, or using it gives us an invalid block |
| 3239 | * number, then just use the start of the next block |
| 3240 | * offset by our length. |
| 3241 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3242 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3244 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3245 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3246 | gotbno -= ap->length; |
| 3247 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | } else |
| 3249 | gotdiff += adjust; |
| 3250 | /* |
| 3251 | * If the firstblock forbids it, can't use it, |
| 3252 | * must use default. |
| 3253 | */ |
| 3254 | if (!rt && !nullfb && |
| 3255 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3256 | gotbno = NULLFSBLOCK; |
| 3257 | } |
| 3258 | /* |
| 3259 | * No next block, just default. |
| 3260 | */ |
| 3261 | else |
| 3262 | gotbno = NULLFSBLOCK; |
| 3263 | /* |
| 3264 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3265 | * one, else ap->blkno is already set (to 0 or the inode block). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | */ |
| 3267 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3268 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3270 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3272 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3274 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3275 | } |
| 3276 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3277 | static int |
| 3278 | xfs_bmap_longest_free_extent( |
| 3279 | struct xfs_trans *tp, |
| 3280 | xfs_agnumber_t ag, |
| 3281 | xfs_extlen_t *blen, |
| 3282 | int *notinit) |
| 3283 | { |
| 3284 | struct xfs_mount *mp = tp->t_mountp; |
| 3285 | struct xfs_perag *pag; |
| 3286 | xfs_extlen_t longest; |
| 3287 | int error = 0; |
| 3288 | |
| 3289 | pag = xfs_perag_get(mp, ag); |
| 3290 | if (!pag->pagf_init) { |
| 3291 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
Darrick J. Wong | f48e2df | 2020-01-23 17:01:19 -0800 | [diff] [blame] | 3292 | if (error) { |
| 3293 | /* Couldn't lock the AGF, so skip this AG. */ |
| 3294 | if (error == -EAGAIN) { |
| 3295 | *notinit = 1; |
| 3296 | error = 0; |
| 3297 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3298 | goto out; |
| 3299 | } |
| 3300 | } |
| 3301 | |
Eric Sandeen | a1f6941 | 2018-04-06 10:09:42 -0700 | [diff] [blame] | 3302 | longest = xfs_alloc_longest_free_extent(pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3303 | xfs_alloc_min_freelist(mp, pag), |
| 3304 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3305 | if (*blen < longest) |
| 3306 | *blen = longest; |
| 3307 | |
| 3308 | out: |
| 3309 | xfs_perag_put(pag); |
| 3310 | return error; |
| 3311 | } |
| 3312 | |
| 3313 | static void |
| 3314 | xfs_bmap_select_minlen( |
| 3315 | struct xfs_bmalloca *ap, |
| 3316 | struct xfs_alloc_arg *args, |
| 3317 | xfs_extlen_t *blen, |
| 3318 | int notinit) |
| 3319 | { |
| 3320 | if (notinit || *blen < ap->minlen) { |
| 3321 | /* |
| 3322 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3323 | * there is space for this request. |
| 3324 | */ |
| 3325 | args->minlen = ap->minlen; |
| 3326 | } else if (*blen < args->maxlen) { |
| 3327 | /* |
| 3328 | * If the best seen length is less than the request length, |
| 3329 | * use the best as the minimum. |
| 3330 | */ |
| 3331 | args->minlen = *blen; |
| 3332 | } else { |
| 3333 | /* |
| 3334 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3335 | * as the minimum. |
| 3336 | */ |
| 3337 | args->minlen = args->maxlen; |
| 3338 | } |
| 3339 | } |
| 3340 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3341 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3342 | xfs_bmap_btalloc_nullfb( |
| 3343 | struct xfs_bmalloca *ap, |
| 3344 | struct xfs_alloc_arg *args, |
| 3345 | xfs_extlen_t *blen) |
| 3346 | { |
| 3347 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3348 | xfs_agnumber_t ag, startag; |
| 3349 | int notinit = 0; |
| 3350 | int error; |
| 3351 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3352 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3353 | args->total = ap->total; |
| 3354 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3355 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3356 | if (startag == NULLAGNUMBER) |
| 3357 | startag = ag = 0; |
| 3358 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3359 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3360 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3361 | ¬init); |
| 3362 | if (error) |
| 3363 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3364 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3365 | if (++ag == mp->m_sb.sb_agcount) |
| 3366 | ag = 0; |
| 3367 | if (ag == startag) |
| 3368 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3369 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3370 | |
| 3371 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3372 | return 0; |
| 3373 | } |
| 3374 | |
| 3375 | STATIC int |
| 3376 | xfs_bmap_btalloc_filestreams( |
| 3377 | struct xfs_bmalloca *ap, |
| 3378 | struct xfs_alloc_arg *args, |
| 3379 | xfs_extlen_t *blen) |
| 3380 | { |
| 3381 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3382 | xfs_agnumber_t ag; |
| 3383 | int notinit = 0; |
| 3384 | int error; |
| 3385 | |
| 3386 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3387 | args->total = ap->total; |
| 3388 | |
| 3389 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3390 | if (ag == NULLAGNUMBER) |
| 3391 | ag = 0; |
| 3392 | |
| 3393 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3394 | if (error) |
| 3395 | return error; |
| 3396 | |
| 3397 | if (*blen < args->maxlen) { |
| 3398 | error = xfs_filestream_new_ag(ap, &ag); |
| 3399 | if (error) |
| 3400 | return error; |
| 3401 | |
| 3402 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3403 | ¬init); |
| 3404 | if (error) |
| 3405 | return error; |
| 3406 | |
| 3407 | } |
| 3408 | |
| 3409 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3410 | |
| 3411 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3412 | * Set the failure fallback case to look in the selected AG as stream |
| 3413 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3414 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3415 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3416 | return 0; |
| 3417 | } |
| 3418 | |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3419 | /* Update all inode and quota accounting for the allocation we just did. */ |
| 3420 | static void |
| 3421 | xfs_bmap_btalloc_accounting( |
| 3422 | struct xfs_bmalloca *ap, |
| 3423 | struct xfs_alloc_arg *args) |
| 3424 | { |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3425 | if (ap->flags & XFS_BMAPI_COWFORK) { |
| 3426 | /* |
| 3427 | * COW fork blocks are in-core only and thus are treated as |
| 3428 | * in-core quota reservation (like delalloc blocks) even when |
| 3429 | * converted to real blocks. The quota reservation is not |
| 3430 | * accounted to disk until blocks are remapped to the data |
| 3431 | * fork. So if these blocks were previously delalloc, we |
| 3432 | * already have quota reservation and there's nothing to do |
| 3433 | * yet. |
| 3434 | */ |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3435 | if (ap->wasdel) { |
| 3436 | xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len); |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3437 | return; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3438 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3439 | |
| 3440 | /* |
| 3441 | * Otherwise, we've allocated blocks in a hole. The transaction |
| 3442 | * has acquired in-core quota reservation for this extent. |
| 3443 | * Rather than account these as real blocks, however, we reduce |
| 3444 | * the transaction quota reservation based on the allocation. |
| 3445 | * This essentially transfers the transaction quota reservation |
| 3446 | * to that of a delalloc extent. |
| 3447 | */ |
| 3448 | ap->ip->i_delayed_blks += args->len; |
| 3449 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS, |
| 3450 | -(long)args->len); |
| 3451 | return; |
| 3452 | } |
| 3453 | |
| 3454 | /* data/attr fork only */ |
| 3455 | ap->ip->i_d.di_nblocks += args->len; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3456 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3457 | if (ap->wasdel) { |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3458 | ap->ip->i_delayed_blks -= args->len; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3459 | xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len); |
| 3460 | } |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3461 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 3462 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT, |
| 3463 | args->len); |
| 3464 | } |
| 3465 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3466 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3467 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3468 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3469 | { |
| 3470 | xfs_mount_t *mp; /* mount point structure */ |
| 3471 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3472 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3473 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3474 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3475 | xfs_alloc_arg_t args; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3476 | xfs_fileoff_t orig_offset; |
| 3477 | xfs_extlen_t orig_length; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3478 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3479 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3480 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3481 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3482 | int tryagain; |
| 3483 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3484 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3485 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3486 | ASSERT(ap->length); |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3487 | orig_offset = ap->offset; |
| 3488 | orig_length = ap->length; |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3489 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3490 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3491 | |
| 3492 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3493 | stripe_align = 0; |
| 3494 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3495 | stripe_align = mp->m_swidth; |
| 3496 | else if (mp->m_dalign) |
| 3497 | stripe_align = mp->m_dalign; |
| 3498 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3499 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3500 | align = xfs_get_cowextsz_hint(ap->ip); |
Christoph Hellwig | c34d570 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 3501 | else if (ap->datatype & XFS_ALLOC_USERDATA) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3502 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3503 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3504 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3505 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3506 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3507 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3508 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3509 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3510 | |
| 3511 | |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3512 | nullfb = ap->tp->t_firstblock == NULLFSBLOCK; |
| 3513 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, |
| 3514 | ap->tp->t_firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3515 | if (nullfb) { |
Christoph Hellwig | c34d570 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 3516 | if ((ap->datatype & XFS_ALLOC_USERDATA) && |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3517 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3518 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3519 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3520 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3521 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3522 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3523 | } |
| 3524 | } else |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3525 | ap->blkno = ap->tp->t_firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3526 | |
| 3527 | xfs_bmap_adjacent(ap); |
| 3528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3530 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | * it's in the right allocation group. |
| 3532 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3533 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | ; |
| 3535 | else |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3536 | ap->blkno = ap->tp->t_firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | * Normal allocation, done through xfs_alloc_vextent. |
| 3539 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3540 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3541 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3542 | args.tp = ap->tp; |
| 3543 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3544 | args.fsbno = ap->blkno; |
Darrick J. Wong | 7280fed | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 3545 | args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3546 | |
| 3547 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 9bb54cb | 2018-06-07 07:54:02 -0700 | [diff] [blame] | 3548 | args.maxlen = min(ap->length, mp->m_ag_max_usable); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3549 | blen = 0; |
| 3550 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3551 | /* |
| 3552 | * Search for an allocation group with a single extent large |
| 3553 | * enough for the request. If one isn't found, then adjust |
| 3554 | * the minimum allocation size to the largest space found. |
| 3555 | */ |
Christoph Hellwig | c34d570 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 3556 | if ((ap->datatype & XFS_ALLOC_USERDATA) && |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3557 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3558 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3559 | else |
| 3560 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3561 | if (error) |
| 3562 | return error; |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3563 | } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3564 | if (xfs_inode_is_filestream(ap->ip)) |
| 3565 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3566 | else |
| 3567 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3568 | args.total = args.minlen = ap->minlen; |
| 3569 | } else { |
| 3570 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3571 | args.total = ap->total; |
| 3572 | args.minlen = ap->minlen; |
| 3573 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3574 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3575 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3576 | args.prod = align; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3577 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3578 | if (args.mod) |
| 3579 | args.mod = args.prod - args.mod; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3580 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3581 | args.prod = 1; |
| 3582 | args.mod = 0; |
| 3583 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3584 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3585 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3586 | if (args.mod) |
| 3587 | args.mod = args.prod - args.mod; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3588 | } |
| 3589 | /* |
Dave Chinner | 1c74357 | 2019-10-21 09:26:34 -0700 | [diff] [blame] | 3590 | * If we are not low on available data blocks, and the underlying |
| 3591 | * logical volume manager is a stripe, and the file offset is zero then |
| 3592 | * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof |
| 3593 | * is only set if the allocation length is >= the stripe unit and the |
| 3594 | * allocation offset is at the end of file. |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3595 | */ |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3596 | if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3597 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3598 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3599 | atype = args.type; |
| 3600 | isaligned = 1; |
| 3601 | /* |
Dave Chinner | 1c74357 | 2019-10-21 09:26:34 -0700 | [diff] [blame] | 3602 | * Adjust minlen to try and preserve alignment if we |
| 3603 | * can't guarantee an aligned maxlen extent. |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3604 | */ |
Dave Chinner | 1c74357 | 2019-10-21 09:26:34 -0700 | [diff] [blame] | 3605 | if (blen > args.alignment && |
| 3606 | blen <= args.maxlen + args.alignment) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3607 | args.minlen = blen - args.alignment; |
| 3608 | args.minalignslop = 0; |
| 3609 | } else { |
| 3610 | /* |
| 3611 | * First try an exact bno allocation. |
| 3612 | * If it fails then do a near or start bno |
| 3613 | * allocation with alignment turned on. |
| 3614 | */ |
| 3615 | atype = args.type; |
| 3616 | tryagain = 1; |
| 3617 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3618 | args.alignment = 1; |
| 3619 | /* |
| 3620 | * Compute the minlen+alignment for the |
| 3621 | * next case. Set slop so that the value |
| 3622 | * of minlen+alignment+slop doesn't go up |
| 3623 | * between the calls. |
| 3624 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3625 | if (blen > stripe_align && blen <= args.maxlen) |
| 3626 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3627 | else |
| 3628 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3629 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3630 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3631 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3632 | args.minlen - 1; |
| 3633 | else |
| 3634 | args.minalignslop = 0; |
| 3635 | } |
| 3636 | } else { |
| 3637 | args.alignment = 1; |
| 3638 | args.minalignslop = 0; |
| 3639 | } |
| 3640 | args.minleft = ap->minleft; |
| 3641 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3642 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3643 | args.datatype = ap->datatype; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3644 | |
| 3645 | error = xfs_alloc_vextent(&args); |
| 3646 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3647 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3648 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3649 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3650 | /* |
| 3651 | * Exact allocation failed. Now try with alignment |
| 3652 | * turned on. |
| 3653 | */ |
| 3654 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3655 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3656 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3657 | args.minlen = nextminlen; |
| 3658 | args.minalignslop = 0; |
| 3659 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | if ((error = xfs_alloc_vextent(&args))) |
| 3661 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3662 | } |
| 3663 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3664 | /* |
| 3665 | * allocation failed, so turn off alignment and |
| 3666 | * try again. |
| 3667 | */ |
| 3668 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3669 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3670 | args.alignment = 0; |
| 3671 | if ((error = xfs_alloc_vextent(&args))) |
| 3672 | return error; |
| 3673 | } |
| 3674 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3675 | args.minlen > ap->minlen) { |
| 3676 | args.minlen = ap->minlen; |
| 3677 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3678 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3679 | if ((error = xfs_alloc_vextent(&args))) |
| 3680 | return error; |
| 3681 | } |
| 3682 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3683 | args.fsbno = 0; |
| 3684 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3685 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3686 | if ((error = xfs_alloc_vextent(&args))) |
| 3687 | return error; |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3688 | ap->tp->t_flags |= XFS_TRANS_LOWMODE; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3689 | } |
| 3690 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3691 | /* |
| 3692 | * check the allocation happened at the same or higher AG than |
| 3693 | * the first block that was allocated. |
| 3694 | */ |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3695 | ASSERT(ap->tp->t_firstblock == NULLFSBLOCK || |
| 3696 | XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <= |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3697 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3698 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3699 | ap->blkno = args.fsbno; |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3700 | if (ap->tp->t_firstblock == NULLFSBLOCK) |
| 3701 | ap->tp->t_firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3702 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3703 | ap->length = args.len; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3704 | /* |
| 3705 | * If the extent size hint is active, we tried to round the |
| 3706 | * caller's allocation request offset down to extsz and the |
| 3707 | * length up to another extsz boundary. If we found a free |
| 3708 | * extent we mapped it in starting at this new offset. If the |
| 3709 | * newly mapped space isn't long enough to cover any of the |
| 3710 | * range of offsets that was originally requested, move the |
| 3711 | * mapping up so that we can fill as much of the caller's |
| 3712 | * original request as possible. Free space is apparently |
| 3713 | * very fragmented so we're unlikely to be able to satisfy the |
| 3714 | * hints anyway. |
| 3715 | */ |
| 3716 | if (ap->length <= orig_length) |
| 3717 | ap->offset = orig_offset; |
| 3718 | else if (ap->offset + ap->length < orig_offset + orig_length) |
| 3719 | ap->offset = orig_offset + orig_length - ap->length; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3720 | xfs_bmap_btalloc_accounting(ap, &args); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3721 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3722 | ap->blkno = NULLFSBLOCK; |
| 3723 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | } |
| 3725 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3726 | } |
| 3727 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3728 | /* Trim extent to fit a logical block range. */ |
| 3729 | void |
| 3730 | xfs_trim_extent( |
| 3731 | struct xfs_bmbt_irec *irec, |
| 3732 | xfs_fileoff_t bno, |
| 3733 | xfs_filblks_t len) |
| 3734 | { |
| 3735 | xfs_fileoff_t distance; |
| 3736 | xfs_fileoff_t end = bno + len; |
| 3737 | |
| 3738 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3739 | irec->br_startoff >= end) { |
| 3740 | irec->br_blockcount = 0; |
| 3741 | return; |
| 3742 | } |
| 3743 | |
| 3744 | if (irec->br_startoff < bno) { |
| 3745 | distance = bno - irec->br_startoff; |
| 3746 | if (isnullstartblock(irec->br_startblock)) |
| 3747 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3748 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3749 | irec->br_startblock != HOLESTARTBLOCK) |
| 3750 | irec->br_startblock += distance; |
| 3751 | irec->br_startoff += distance; |
| 3752 | irec->br_blockcount -= distance; |
| 3753 | } |
| 3754 | |
| 3755 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3756 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3757 | irec->br_blockcount -= distance; |
| 3758 | } |
| 3759 | } |
| 3760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3762 | * Trim the returned map to the required bounds |
| 3763 | */ |
| 3764 | STATIC void |
| 3765 | xfs_bmapi_trim_map( |
| 3766 | struct xfs_bmbt_irec *mval, |
| 3767 | struct xfs_bmbt_irec *got, |
| 3768 | xfs_fileoff_t *bno, |
| 3769 | xfs_filblks_t len, |
| 3770 | xfs_fileoff_t obno, |
| 3771 | xfs_fileoff_t end, |
| 3772 | int n, |
| 3773 | int flags) |
| 3774 | { |
| 3775 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3776 | got->br_startoff + got->br_blockcount <= obno) { |
| 3777 | *mval = *got; |
| 3778 | if (isnullstartblock(got->br_startblock)) |
| 3779 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3780 | return; |
| 3781 | } |
| 3782 | |
| 3783 | if (obno > *bno) |
| 3784 | *bno = obno; |
| 3785 | ASSERT((*bno >= obno) || (n == 0)); |
| 3786 | ASSERT(*bno < end); |
| 3787 | mval->br_startoff = *bno; |
| 3788 | if (isnullstartblock(got->br_startblock)) |
| 3789 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3790 | else |
| 3791 | mval->br_startblock = got->br_startblock + |
| 3792 | (*bno - got->br_startoff); |
| 3793 | /* |
| 3794 | * Return the minimum of what we got and what we asked for for |
| 3795 | * the length. We can use the len variable here because it is |
| 3796 | * modified below and we could have been there before coming |
| 3797 | * here if the first part of the allocation didn't overlap what |
| 3798 | * was asked for. |
| 3799 | */ |
| 3800 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3801 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3802 | mval->br_state = got->br_state; |
| 3803 | ASSERT(mval->br_blockcount <= len); |
| 3804 | return; |
| 3805 | } |
| 3806 | |
| 3807 | /* |
| 3808 | * Update and validate the extent map to return |
| 3809 | */ |
| 3810 | STATIC void |
| 3811 | xfs_bmapi_update_map( |
| 3812 | struct xfs_bmbt_irec **map, |
| 3813 | xfs_fileoff_t *bno, |
| 3814 | xfs_filblks_t *len, |
| 3815 | xfs_fileoff_t obno, |
| 3816 | xfs_fileoff_t end, |
| 3817 | int *n, |
| 3818 | int flags) |
| 3819 | { |
| 3820 | xfs_bmbt_irec_t *mval = *map; |
| 3821 | |
| 3822 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3823 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3824 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3825 | (mval->br_startoff < obno)); |
| 3826 | |
| 3827 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3828 | *len = end - *bno; |
| 3829 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3830 | /* update previous map with new information */ |
| 3831 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3832 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3833 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3834 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3835 | mval[-1].br_state = mval->br_state; |
| 3836 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3837 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3838 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3839 | mval->br_startblock == mval[-1].br_startblock + |
| 3840 | mval[-1].br_blockcount && |
Christoph Hellwig | c3a2f9f | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 3841 | mval[-1].br_state == mval->br_state) { |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3842 | ASSERT(mval->br_startoff == |
| 3843 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3844 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3845 | } else if (*n > 0 && |
| 3846 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3847 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3848 | mval->br_startoff == |
| 3849 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3850 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3851 | mval[-1].br_state = mval->br_state; |
| 3852 | } else if (!((*n == 0) && |
| 3853 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3854 | obno))) { |
| 3855 | mval++; |
| 3856 | (*n)++; |
| 3857 | } |
| 3858 | *map = mval; |
| 3859 | } |
| 3860 | |
| 3861 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3862 | * Map file blocks to filesystem blocks without allocation. |
| 3863 | */ |
| 3864 | int |
| 3865 | xfs_bmapi_read( |
| 3866 | struct xfs_inode *ip, |
| 3867 | xfs_fileoff_t bno, |
| 3868 | xfs_filblks_t len, |
| 3869 | struct xfs_bmbt_irec *mval, |
| 3870 | int *nmap, |
| 3871 | int flags) |
| 3872 | { |
| 3873 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 4b516ff | 2020-05-14 14:06:41 -0700 | [diff] [blame] | 3874 | int whichfork = xfs_bmapi_whichfork(flags); |
| 3875 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3876 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3877 | xfs_fileoff_t obno; |
| 3878 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3879 | struct xfs_iext_cursor icur; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3880 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3881 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3882 | int n = 0; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3883 | |
| 3884 | ASSERT(*nmap >= 1); |
Christoph Hellwig | 1a1c57b | 2020-05-14 14:06:40 -0700 | [diff] [blame] | 3885 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3886 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3887 | |
Christoph Hellwig | 4b516ff | 2020-05-14 14:06:41 -0700 | [diff] [blame] | 3888 | if (WARN_ON_ONCE(!ifp)) |
| 3889 | return -EFSCORRUPTED; |
| 3890 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 3891 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
| 3892 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3893 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3894 | |
| 3895 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3896 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3897 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3898 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3899 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3900 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3901 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3902 | if (error) |
| 3903 | return error; |
| 3904 | } |
| 3905 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3906 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3907 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3908 | end = bno + len; |
| 3909 | obno = bno; |
| 3910 | |
| 3911 | while (bno < end && n < *nmap) { |
| 3912 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3913 | if (eof) |
| 3914 | got.br_startoff = end; |
| 3915 | if (got.br_startoff > bno) { |
| 3916 | /* Reading in a hole. */ |
| 3917 | mval->br_startoff = bno; |
| 3918 | mval->br_startblock = HOLESTARTBLOCK; |
| 3919 | mval->br_blockcount = |
| 3920 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3921 | mval->br_state = XFS_EXT_NORM; |
| 3922 | bno += mval->br_blockcount; |
| 3923 | len -= mval->br_blockcount; |
| 3924 | mval++; |
| 3925 | n++; |
| 3926 | continue; |
| 3927 | } |
| 3928 | |
| 3929 | /* set up the extent map to return. */ |
| 3930 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3931 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3932 | |
| 3933 | /* If we're done, stop now. */ |
| 3934 | if (bno >= end || n >= *nmap) |
| 3935 | break; |
| 3936 | |
| 3937 | /* Else go on to the next record. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3938 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3939 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3940 | } |
| 3941 | *nmap = n; |
| 3942 | return 0; |
| 3943 | } |
| 3944 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3945 | /* |
| 3946 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3947 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3948 | * |
| 3949 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3950 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3951 | * to the extent record that was inserted to the inode fork. |
| 3952 | * |
| 3953 | * Note that the allocated extent may have been merged with contiguous extents |
| 3954 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3955 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3956 | * look up the updated record in the inode fork. |
| 3957 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3958 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3959 | xfs_bmapi_reserve_delalloc( |
| 3960 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3961 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3962 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3963 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3964 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3965 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3966 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3967 | int eof) |
| 3968 | { |
| 3969 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3970 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3971 | xfs_extlen_t alen; |
| 3972 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3973 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3974 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3975 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3976 | /* |
| 3977 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3978 | * tag the inode before we return. |
| 3979 | */ |
| 3980 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3981 | if (!eof) |
| 3982 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3983 | if (prealloc && alen >= len) |
| 3984 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3985 | |
| 3986 | /* Figure out the extent size, adjust alen */ |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3987 | if (whichfork == XFS_COW_FORK) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3988 | struct xfs_bmbt_irec prev; |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3989 | xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3990 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3991 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3992 | prev.br_startoff = NULLFILEOFF; |
| 3993 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3994 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3995 | 1, 0, &aoff, &alen); |
| 3996 | ASSERT(!error); |
| 3997 | } |
| 3998 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3999 | /* |
| 4000 | * Make a transaction-less quota reservation for delayed allocation |
| 4001 | * blocks. This number gets adjusted later. We return if we haven't |
| 4002 | * allocated blocks already inside this loop. |
| 4003 | */ |
| 4004 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4005 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4006 | if (error) |
| 4007 | return error; |
| 4008 | |
| 4009 | /* |
| 4010 | * Split changing sb for alen and indlen since they could be coming |
| 4011 | * from different places. |
| 4012 | */ |
| 4013 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4014 | ASSERT(indlen > 0); |
| 4015 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4016 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4017 | if (error) |
| 4018 | goto out_unreserve_quota; |
| 4019 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4020 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4021 | if (error) |
| 4022 | goto out_unreserve_blocks; |
| 4023 | |
| 4024 | |
| 4025 | ip->i_delayed_blks += alen; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 4026 | xfs_mod_delalloc(ip->i_mount, alen + indlen); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4027 | |
| 4028 | got->br_startoff = aoff; |
| 4029 | got->br_startblock = nullstartblock(indlen); |
| 4030 | got->br_blockcount = alen; |
| 4031 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4032 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4033 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4034 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4035 | /* |
| 4036 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 4037 | * preallocation can occur at the start or end of the extent, even when |
| 4038 | * prealloc == 0, so we must also check the aligned offset and length. |
| 4039 | */ |
| 4040 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4041 | xfs_inode_set_eofblocks_tag(ip); |
| 4042 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4043 | xfs_inode_set_cowblocks_tag(ip); |
| 4044 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4045 | return 0; |
| 4046 | |
| 4047 | out_unreserve_blocks: |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4048 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4049 | out_unreserve_quota: |
| 4050 | if (XFS_IS_QUOTA_ON(mp)) |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4051 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4052 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4053 | return error; |
| 4054 | } |
| 4055 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4056 | static int |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4057 | xfs_bmap_alloc_userdata( |
| 4058 | struct xfs_bmalloca *bma) |
| 4059 | { |
| 4060 | struct xfs_mount *mp = bma->ip->i_mount; |
| 4061 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
| 4062 | int error; |
| 4063 | |
| 4064 | /* |
| 4065 | * Set the data type being allocated. For the data fork, the first data |
| 4066 | * in the file is treated differently to all other allocations. For the |
| 4067 | * attribute fork, we only need to ensure the allocated range is not on |
| 4068 | * the busy list. |
| 4069 | */ |
| 4070 | bma->datatype = XFS_ALLOC_NOBUSY; |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4071 | if (whichfork == XFS_DATA_FORK) { |
Christoph Hellwig | c34d570 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 4072 | bma->datatype |= XFS_ALLOC_USERDATA; |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4073 | if (bma->offset == 0) |
| 4074 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4075 | |
| 4076 | if (mp->m_dalign && bma->length >= mp->m_dalign) { |
| 4077 | error = xfs_bmap_isaeof(bma, whichfork); |
| 4078 | if (error) |
| 4079 | return error; |
| 4080 | } |
| 4081 | |
| 4082 | if (XFS_IS_REALTIME_INODE(bma->ip)) |
| 4083 | return xfs_bmap_rtalloc(bma); |
| 4084 | } |
| 4085 | |
| 4086 | return xfs_bmap_btalloc(bma); |
| 4087 | } |
| 4088 | |
| 4089 | static int |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4090 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4091 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4092 | { |
| 4093 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4094 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4095 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4096 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4097 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4098 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4099 | ASSERT(bma->length > 0); |
| 4100 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4101 | /* |
| 4102 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4103 | * for in this bmap call but that wouldn't be as good. |
| 4104 | */ |
| 4105 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4106 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4107 | bma->offset = bma->got.br_startoff; |
Darrick J. Wong | f5be084 | 2019-11-06 08:53:54 -0800 | [diff] [blame] | 4108 | if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev)) |
| 4109 | bma->prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4110 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4111 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4112 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4113 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4114 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4117 | if (bma->flags & XFS_BMAPI_CONTIG) |
| 4118 | bma->minlen = bma->length; |
| 4119 | else |
| 4120 | bma->minlen = 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4121 | |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4122 | if (bma->flags & XFS_BMAPI_METADATA) |
| 4123 | error = xfs_bmap_btalloc(bma); |
| 4124 | else |
| 4125 | error = xfs_bmap_alloc_userdata(bma); |
| 4126 | if (error || bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4127 | return error; |
| 4128 | |
Christoph Hellwig | fd638f1 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 4129 | if (bma->flags & XFS_BMAPI_ZERO) { |
| 4130 | error = xfs_zero_extent(bma->ip, bma->blkno, bma->length); |
| 4131 | if (error) |
| 4132 | return error; |
| 4133 | } |
| 4134 | |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 4135 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4136 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4137 | /* |
| 4138 | * Bump the number of extents we've allocated |
| 4139 | * in this call. |
| 4140 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4141 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4142 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4143 | if (bma->cur) |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 4144 | bma->cur->bc_ino.flags = |
Dave Chinner | 8ef5479 | 2020-03-10 17:54:38 -0700 | [diff] [blame] | 4145 | bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4146 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4147 | bma->got.br_startoff = bma->offset; |
| 4148 | bma->got.br_startblock = bma->blkno; |
| 4149 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4150 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4151 | |
Darrick J. Wong | a5949d3 | 2020-05-23 09:43:31 -0700 | [diff] [blame] | 4152 | if (bma->flags & XFS_BMAPI_PREALLOC) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4153 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4154 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4155 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4156 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4157 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4158 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4159 | whichfork, &bma->icur, &bma->cur, &bma->got, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4160 | &bma->logflags, bma->flags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4161 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4162 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4163 | if (error) |
| 4164 | return error; |
| 4165 | |
| 4166 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4167 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4168 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4169 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4170 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4171 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4172 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4173 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4174 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4175 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4176 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4177 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4178 | return 0; |
| 4179 | } |
| 4180 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4181 | STATIC int |
| 4182 | xfs_bmapi_convert_unwritten( |
| 4183 | struct xfs_bmalloca *bma, |
| 4184 | struct xfs_bmbt_irec *mval, |
| 4185 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4186 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4187 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4188 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4189 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4190 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4191 | int error; |
| 4192 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4193 | /* check if we need to do unwritten->real conversion */ |
| 4194 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4195 | (flags & XFS_BMAPI_PREALLOC)) |
| 4196 | return 0; |
| 4197 | |
| 4198 | /* check if we need to do real->unwritten conversion */ |
| 4199 | if (mval->br_state == XFS_EXT_NORM && |
| 4200 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4201 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4202 | return 0; |
| 4203 | |
| 4204 | /* |
| 4205 | * Modify (by adding) the state flag, if writing. |
| 4206 | */ |
| 4207 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4208 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4209 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4210 | bma->ip, whichfork); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4211 | } |
| 4212 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4213 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4214 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4215 | /* |
| 4216 | * Before insertion into the bmbt, zero the range being converted |
| 4217 | * if required. |
| 4218 | */ |
| 4219 | if (flags & XFS_BMAPI_ZERO) { |
| 4220 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4221 | mval->br_blockcount); |
| 4222 | if (error) |
| 4223 | return error; |
| 4224 | } |
| 4225 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4226 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4227 | &bma->icur, &bma->cur, mval, &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4228 | /* |
| 4229 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4230 | * path because the conversion might not have done so (e.g., if the |
| 4231 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4232 | * in the transaction for the sake of fsync(), even if nothing has |
| 4233 | * changed, because fsync() will not force the log for this transaction |
| 4234 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4235 | * |
| 4236 | * Note: If we're only converting cow fork extents, there aren't |
| 4237 | * any on-disk updates to make, so we don't need to log anything. |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4238 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4239 | if (whichfork != XFS_COW_FORK) |
| 4240 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4241 | if (error) |
| 4242 | return error; |
| 4243 | |
| 4244 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4245 | * Update our extent pointer, given that |
| 4246 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4247 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4248 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4249 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4250 | |
| 4251 | /* |
| 4252 | * We may have combined previously unwritten space with written space, |
| 4253 | * so generate another request. |
| 4254 | */ |
| 4255 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4256 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4257 | return 0; |
| 4258 | } |
| 4259 | |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4260 | static inline xfs_extlen_t |
| 4261 | xfs_bmapi_minleft( |
| 4262 | struct xfs_trans *tp, |
| 4263 | struct xfs_inode *ip, |
| 4264 | int fork) |
| 4265 | { |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4266 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, fork); |
| 4267 | |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4268 | if (tp && tp->t_firstblock != NULLFSBLOCK) |
| 4269 | return 0; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4270 | if (ifp->if_format != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4271 | return 1; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4272 | return be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | /* |
| 4276 | * Log whatever the flags say, even if error. Otherwise we might miss detecting |
| 4277 | * a case where the data is changed, there's an error, and it's not logged so we |
| 4278 | * don't shutdown when we should. Don't bother logging extents/btree changes if |
| 4279 | * we converted to the other format. |
| 4280 | */ |
| 4281 | static void |
| 4282 | xfs_bmapi_finish( |
| 4283 | struct xfs_bmalloca *bma, |
| 4284 | int whichfork, |
| 4285 | int error) |
| 4286 | { |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4287 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
| 4288 | |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4289 | if ((bma->logflags & xfs_ilog_fext(whichfork)) && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4290 | ifp->if_format != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4291 | bma->logflags &= ~xfs_ilog_fext(whichfork); |
| 4292 | else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4293 | ifp->if_format != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4294 | bma->logflags &= ~xfs_ilog_fbroot(whichfork); |
| 4295 | |
| 4296 | if (bma->logflags) |
| 4297 | xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags); |
| 4298 | if (bma->cur) |
| 4299 | xfs_btree_del_cursor(bma->cur, error); |
| 4300 | } |
| 4301 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4302 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4303 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4304 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4305 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4306 | * locking problems. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4308 | int |
| 4309 | xfs_bmapi_write( |
| 4310 | struct xfs_trans *tp, /* transaction pointer */ |
| 4311 | struct xfs_inode *ip, /* incore inode */ |
| 4312 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4313 | xfs_filblks_t len, /* length to map in file */ |
| 4314 | int flags, /* XFS_BMAPI_... */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4315 | xfs_extlen_t total, /* total blocks needed */ |
| 4316 | struct xfs_bmbt_irec *mval, /* output: map values */ |
Brian Foster | 6e702a5 | 2018-07-11 22:26:12 -0700 | [diff] [blame] | 4317 | int *nmap) /* i/o: mval size/count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | { |
Darrick J. Wong | 4b0bce3 | 2019-03-19 08:16:22 -0700 | [diff] [blame] | 4319 | struct xfs_bmalloca bma = { |
| 4320 | .tp = tp, |
| 4321 | .ip = ip, |
| 4322 | .total = total, |
| 4323 | }; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4324 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4325 | int whichfork = xfs_bmapi_whichfork(flags); |
| 4326 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4327 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4328 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4329 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4330 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4331 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4334 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4335 | int orig_flags; /* original flags arg value */ |
| 4336 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4337 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4338 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | |
| 4340 | orig_bno = bno; |
| 4341 | orig_len = len; |
| 4342 | orig_flags = flags; |
| 4343 | orig_mval = mval; |
| 4344 | orig_nmap = *nmap; |
| 4345 | #endif |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4348 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 4349 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4350 | ASSERT(len > 0); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4351 | ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4352 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4353 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4354 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4355 | /* zeroing is for currently only for data extents, not metadata */ |
| 4356 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4357 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4358 | /* |
| 4359 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4360 | * but it makes no sense to do both at once. This would result in |
| 4361 | * zeroing the unwritten extent twice, but it still being an |
| 4362 | * unwritten extent.... |
| 4363 | */ |
| 4364 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4365 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4366 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4367 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 4368 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4369 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4373 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4374 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4375 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4376 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4377 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4378 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4379 | if (error) |
| 4380 | goto error0; |
| 4381 | } |
| 4382 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4383 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4384 | eof = true; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4385 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4386 | bma.prev.br_startoff = NULLFILEOFF; |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4387 | bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4388 | |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4389 | n = 0; |
| 4390 | end = bno + len; |
| 4391 | obno = bno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4393 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4394 | |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4395 | /* in hole or beyond EOF? */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4396 | if (eof || bma.got.br_startoff > bno) { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4397 | /* |
| 4398 | * CoW fork conversions should /never/ hit EOF or |
| 4399 | * holes. There should always be something for us |
| 4400 | * to work on. |
| 4401 | */ |
| 4402 | ASSERT(!((flags & XFS_BMAPI_CONVERT) && |
| 4403 | (flags & XFS_BMAPI_COWFORK))); |
| 4404 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4405 | need_alloc = true; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4406 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4407 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4408 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4409 | |
| 4410 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | * First, deal with the hole before the allocated space |
| 4412 | * that we found, if any. |
| 4413 | */ |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 4414 | if (need_alloc || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4415 | bma.eof = eof; |
| 4416 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4417 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4418 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4419 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4420 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4421 | /* |
| 4422 | * There's a 32/64 bit type mismatch between the |
| 4423 | * allocation length request (which can be 64 bits in |
| 4424 | * length) and the bma length request, which is |
| 4425 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4426 | * check for 32-bit overflows and handle them here. |
| 4427 | */ |
| 4428 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4429 | bma.length = MAXEXTLEN; |
| 4430 | else |
| 4431 | bma.length = len; |
| 4432 | |
| 4433 | ASSERT(len > 0); |
| 4434 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4435 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | if (error) |
| 4437 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4438 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4439 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4440 | |
| 4441 | /* |
| 4442 | * If this is a CoW allocation, record the data in |
| 4443 | * the refcount btree for orphan recovery. |
| 4444 | */ |
Darrick J. Wong | 74b4c5d | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 4445 | if (whichfork == XFS_COW_FORK) |
| 4446 | xfs_refcount_alloc_cow_extent(tp, bma.blkno, |
| 4447 | bma.length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4449 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4450 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4451 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4452 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4454 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4455 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4456 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4457 | continue; |
| 4458 | if (error) |
| 4459 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4461 | /* update the extent map to return */ |
| 4462 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | /* |
| 4465 | * If we're done, stop now. Stop when we've allocated |
| 4466 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4467 | * the transaction may get too big. |
| 4468 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4469 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4471 | |
| 4472 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4473 | bma.prev = bma.got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4474 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4475 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4478 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 4479 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, |
| 4480 | whichfork); |
| 4481 | if (error) |
| 4482 | goto error0; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4483 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4484 | ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 4485 | ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork)); |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4486 | xfs_bmapi_finish(&bma, whichfork, 0); |
| 4487 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4488 | orig_nmap, *nmap); |
| 4489 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | error0: |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4491 | xfs_bmapi_finish(&bma, whichfork, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | return error; |
| 4493 | } |
| 4494 | |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4495 | /* |
| 4496 | * Convert an existing delalloc extent to real blocks based on file offset. This |
| 4497 | * attempts to allocate the entire delalloc extent and may require multiple |
| 4498 | * invocations to allocate the target offset if a large enough physical extent |
| 4499 | * is not available. |
| 4500 | */ |
| 4501 | int |
| 4502 | xfs_bmapi_convert_delalloc( |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4503 | struct xfs_inode *ip, |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4504 | int whichfork, |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 4505 | xfs_off_t offset, |
| 4506 | struct iomap *iomap, |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4507 | unsigned int *seq) |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4508 | { |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4509 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4510 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 4511 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4512 | struct xfs_bmalloca bma = { NULL }; |
Darrick J. Wong | af952ae | 2019-12-16 11:14:09 -0800 | [diff] [blame] | 4513 | uint16_t flags = 0; |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4514 | struct xfs_trans *tp; |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4515 | int error; |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4516 | |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 4517 | if (whichfork == XFS_COW_FORK) |
| 4518 | flags |= IOMAP_F_SHARED; |
| 4519 | |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4520 | /* |
| 4521 | * Space for the extent and indirect blocks was reserved when the |
| 4522 | * delalloc extent was created so there's no need to do so here. |
| 4523 | */ |
| 4524 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, |
| 4525 | XFS_TRANS_RESERVE, &tp); |
| 4526 | if (error) |
| 4527 | return error; |
| 4528 | |
| 4529 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 4530 | xfs_trans_ijoin(tp, ip, 0); |
| 4531 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4532 | if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) || |
| 4533 | bma.got.br_startoff > offset_fsb) { |
| 4534 | /* |
| 4535 | * No extent found in the range we are trying to convert. This |
| 4536 | * should only happen for the COW fork, where another thread |
| 4537 | * might have moved the extent to the data fork in the meantime. |
| 4538 | */ |
| 4539 | WARN_ON_ONCE(whichfork != XFS_COW_FORK); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4540 | error = -EAGAIN; |
| 4541 | goto out_trans_cancel; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4542 | } |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4543 | |
| 4544 | /* |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4545 | * If we find a real extent here we raced with another thread converting |
| 4546 | * the extent. Just return the real extent at this offset. |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4547 | */ |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4548 | if (!isnullstartblock(bma.got.br_startblock)) { |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 4549 | xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4550 | *seq = READ_ONCE(ifp->if_seq); |
| 4551 | goto out_trans_cancel; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4552 | } |
| 4553 | |
| 4554 | bma.tp = tp; |
| 4555 | bma.ip = ip; |
| 4556 | bma.wasdel = true; |
| 4557 | bma.offset = bma.got.br_startoff; |
| 4558 | bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4559 | bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); |
Darrick J. Wong | a5949d3 | 2020-05-23 09:43:31 -0700 | [diff] [blame] | 4560 | |
| 4561 | /* |
| 4562 | * When we're converting the delalloc reservations backing dirty pages |
| 4563 | * in the page cache, we must be careful about how we create the new |
| 4564 | * extents: |
| 4565 | * |
| 4566 | * New CoW fork extents are created unwritten, turned into real extents |
| 4567 | * when we're about to write the data to disk, and mapped into the data |
| 4568 | * fork after the write finishes. End of story. |
| 4569 | * |
| 4570 | * New data fork extents must be mapped in as unwritten and converted |
| 4571 | * to real extents after the write succeeds to avoid exposing stale |
| 4572 | * disk contents if we crash. |
| 4573 | */ |
| 4574 | bma.flags = XFS_BMAPI_PREALLOC; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4575 | if (whichfork == XFS_COW_FORK) |
Darrick J. Wong | a5949d3 | 2020-05-23 09:43:31 -0700 | [diff] [blame] | 4576 | bma.flags |= XFS_BMAPI_COWFORK; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4577 | |
| 4578 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
| 4579 | bma.prev.br_startoff = NULLFILEOFF; |
| 4580 | |
| 4581 | error = xfs_bmapi_allocate(&bma); |
| 4582 | if (error) |
| 4583 | goto out_finish; |
| 4584 | |
| 4585 | error = -ENOSPC; |
| 4586 | if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK)) |
| 4587 | goto out_finish; |
| 4588 | error = -EFSCORRUPTED; |
Christoph Hellwig | eb77b23 | 2019-09-03 08:13:13 -0700 | [diff] [blame] | 4589 | if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock))) |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4590 | goto out_finish; |
| 4591 | |
Christoph Hellwig | 125851a | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4592 | XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length)); |
| 4593 | XFS_STATS_INC(mp, xs_xstrat_quick); |
| 4594 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4595 | ASSERT(!isnullstartblock(bma.got.br_startblock)); |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 4596 | xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4597 | *seq = READ_ONCE(ifp->if_seq); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4598 | |
Darrick J. Wong | 74b4c5d | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 4599 | if (whichfork == XFS_COW_FORK) |
| 4600 | xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4601 | |
| 4602 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, |
| 4603 | whichfork); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4604 | if (error) |
| 4605 | goto out_finish; |
| 4606 | |
| 4607 | xfs_bmapi_finish(&bma, whichfork, 0); |
| 4608 | error = xfs_trans_commit(tp); |
| 4609 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 4610 | return error; |
| 4611 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4612 | out_finish: |
| 4613 | xfs_bmapi_finish(&bma, whichfork, error); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4614 | out_trans_cancel: |
| 4615 | xfs_trans_cancel(tp); |
| 4616 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4617 | return error; |
| 4618 | } |
| 4619 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4620 | int |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4621 | xfs_bmapi_remap( |
| 4622 | struct xfs_trans *tp, |
| 4623 | struct xfs_inode *ip, |
| 4624 | xfs_fileoff_t bno, |
| 4625 | xfs_filblks_t len, |
| 4626 | xfs_fsblock_t startblock, |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4627 | int flags) |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4628 | { |
| 4629 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4630 | struct xfs_ifork *ifp; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4631 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4632 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4633 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4634 | int whichfork = xfs_bmapi_whichfork(flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4635 | int logflags = 0, error; |
| 4636 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4637 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4638 | ASSERT(len > 0); |
| 4639 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4640 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4641 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC | |
| 4642 | XFS_BMAPI_NORMAP))); |
| 4643 | ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) != |
| 4644 | (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4645 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4646 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 4647 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4648 | return -EFSCORRUPTED; |
| 4649 | } |
| 4650 | |
| 4651 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4652 | return -EIO; |
| 4653 | |
| 4654 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4655 | error = xfs_iread_extents(tp, ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4656 | if (error) |
| 4657 | return error; |
| 4658 | } |
| 4659 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4660 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4661 | /* make sure we only reflink into a hole. */ |
| 4662 | ASSERT(got.br_startoff > bno); |
| 4663 | ASSERT(got.br_startoff - bno >= len); |
| 4664 | } |
| 4665 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4666 | ip->i_d.di_nblocks += len; |
| 4667 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4668 | |
| 4669 | if (ifp->if_flags & XFS_IFBROOT) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4670 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 4671 | cur->bc_ino.flags = 0; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | got.br_startoff = bno; |
| 4675 | got.br_startblock = startblock; |
| 4676 | got.br_blockcount = len; |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4677 | if (flags & XFS_BMAPI_PREALLOC) |
| 4678 | got.br_state = XFS_EXT_UNWRITTEN; |
| 4679 | else |
| 4680 | got.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4681 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4682 | error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4683 | &cur, &got, &logflags, flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4684 | if (error) |
| 4685 | goto error0; |
| 4686 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 4687 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4688 | |
| 4689 | error0: |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4690 | if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4691 | logflags &= ~XFS_ILOG_DEXT; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4692 | else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4693 | logflags &= ~XFS_ILOG_DBROOT; |
| 4694 | |
| 4695 | if (logflags) |
| 4696 | xfs_trans_log_inode(tp, ip, logflags); |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 4697 | if (cur) |
| 4698 | xfs_btree_del_cursor(cur, error); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4699 | return error; |
| 4700 | } |
| 4701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4703 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4704 | * indlen reservation must be shared across the two new extents that are left |
| 4705 | * behind. |
| 4706 | * |
| 4707 | * Given the original reservation and the worst case indlen for the two new |
| 4708 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4709 | * reservation fairly across the two new extents. If necessary, steal available |
| 4710 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4711 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4712 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4713 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4714 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4715 | xfs_bmap_split_indlen( |
| 4716 | xfs_filblks_t ores, /* original res. */ |
| 4717 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4718 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4719 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4720 | { |
| 4721 | xfs_filblks_t len1 = *indlen1; |
| 4722 | xfs_filblks_t len2 = *indlen2; |
| 4723 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4724 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4725 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4726 | |
| 4727 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4728 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4729 | * indlen for both new extents. |
| 4730 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4731 | if (ores < nres && avail) |
| 4732 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4733 | ores += stolen; |
| 4734 | |
| 4735 | /* nothing else to do if we've satisfied the new reservation */ |
| 4736 | if (ores >= nres) |
| 4737 | return stolen; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4738 | |
| 4739 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4740 | * We can't meet the total required reservation for the two extents. |
| 4741 | * Calculate the percent of the overall shortage between both extents |
| 4742 | * and apply this percentage to each of the requested indlen values. |
| 4743 | * This distributes the shortage fairly and reduces the chances that one |
| 4744 | * of the two extents is left with nothing when extents are repeatedly |
| 4745 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4746 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4747 | resfactor = (ores * 100); |
| 4748 | do_div(resfactor, nres); |
| 4749 | len1 *= resfactor; |
| 4750 | do_div(len1, 100); |
| 4751 | len2 *= resfactor; |
| 4752 | do_div(len2, 100); |
| 4753 | ASSERT(len1 + len2 <= ores); |
| 4754 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4755 | |
| 4756 | /* |
| 4757 | * Hand out the remainder to each extent. If one of the two reservations |
| 4758 | * is zero, we want to make sure that one gets a block first. The loop |
| 4759 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4760 | * is zero. |
| 4761 | */ |
| 4762 | ores -= (len1 + len2); |
| 4763 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4764 | if (ores && !len2 && *indlen2) { |
| 4765 | len2++; |
| 4766 | ores--; |
| 4767 | } |
| 4768 | while (ores) { |
| 4769 | if (len1 < *indlen1) { |
| 4770 | len1++; |
| 4771 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4772 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4773 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4774 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4775 | if (len2 < *indlen2) { |
| 4776 | len2++; |
| 4777 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | *indlen1 = len1; |
| 4782 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4783 | |
| 4784 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4785 | } |
| 4786 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4787 | int |
| 4788 | xfs_bmap_del_extent_delay( |
| 4789 | struct xfs_inode *ip, |
| 4790 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4791 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4792 | struct xfs_bmbt_irec *got, |
| 4793 | struct xfs_bmbt_irec *del) |
| 4794 | { |
| 4795 | struct xfs_mount *mp = ip->i_mount; |
| 4796 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4797 | struct xfs_bmbt_irec new; |
| 4798 | int64_t da_old, da_new, da_diff = 0; |
| 4799 | xfs_fileoff_t del_endoff, got_endoff; |
| 4800 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4801 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4802 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4803 | bool isrt; |
| 4804 | |
| 4805 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4806 | |
| 4807 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4808 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4809 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4810 | da_old = startblockval(got->br_startblock); |
| 4811 | da_new = 0; |
| 4812 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4813 | ASSERT(del->br_blockcount > 0); |
| 4814 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4815 | ASSERT(got_endoff >= del_endoff); |
| 4816 | |
| 4817 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4818 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4819 | |
| 4820 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4821 | xfs_mod_frextents(mp, rtexts); |
| 4822 | } |
| 4823 | |
| 4824 | /* |
| 4825 | * Update the inode delalloc counter now and wait to update the |
| 4826 | * sb counters as we might have to borrow some blocks for the |
| 4827 | * indirect block accounting. |
| 4828 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4829 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4830 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4831 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4832 | if (error) |
| 4833 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4834 | ip->i_delayed_blks -= del->br_blockcount; |
| 4835 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4836 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4837 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4838 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4839 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4840 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4841 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4842 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4843 | /* |
| 4844 | * Matches the whole extent. Delete the entry. |
| 4845 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4846 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4847 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4848 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4849 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4850 | /* |
| 4851 | * Deleting the first part of the extent. |
| 4852 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4853 | got->br_startoff = del_endoff; |
| 4854 | got->br_blockcount -= del->br_blockcount; |
| 4855 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4856 | got->br_blockcount), da_old); |
| 4857 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4858 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4859 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4860 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4861 | /* |
| 4862 | * Deleting the last part of the extent. |
| 4863 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4864 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4865 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4866 | got->br_blockcount), da_old); |
| 4867 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4868 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4869 | break; |
| 4870 | case 0: |
| 4871 | /* |
| 4872 | * Deleting the middle of the extent. |
| 4873 | * |
| 4874 | * Distribute the original indlen reservation across the two new |
| 4875 | * extents. Steal blocks from the deleted extent if necessary. |
| 4876 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4877 | * Warn if either of the new indlen reservations is zero as this |
| 4878 | * can lead to delalloc problems. |
| 4879 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4880 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4881 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4882 | |
| 4883 | new.br_blockcount = got_endoff - del_endoff; |
| 4884 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4885 | |
| 4886 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4887 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4888 | del->br_blockcount); |
| 4889 | |
| 4890 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4891 | |
| 4892 | new.br_startoff = del_endoff; |
| 4893 | new.br_state = got->br_state; |
| 4894 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4895 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4896 | xfs_iext_update_extent(ip, state, icur, got); |
| 4897 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4898 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4899 | |
| 4900 | da_new = got_indlen + new_indlen - stolen; |
| 4901 | del->br_blockcount -= stolen; |
| 4902 | break; |
| 4903 | } |
| 4904 | |
| 4905 | ASSERT(da_old >= da_new); |
| 4906 | da_diff = da_old - da_new; |
| 4907 | if (!isrt) |
| 4908 | da_diff += del->br_blockcount; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 4909 | if (da_diff) { |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4910 | xfs_mod_fdblocks(mp, da_diff, false); |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 4911 | xfs_mod_delalloc(mp, -da_diff); |
| 4912 | } |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4913 | return error; |
| 4914 | } |
| 4915 | |
| 4916 | void |
| 4917 | xfs_bmap_del_extent_cow( |
| 4918 | struct xfs_inode *ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4919 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4920 | struct xfs_bmbt_irec *got, |
| 4921 | struct xfs_bmbt_irec *del) |
| 4922 | { |
| 4923 | struct xfs_mount *mp = ip->i_mount; |
| 4924 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4925 | struct xfs_bmbt_irec new; |
| 4926 | xfs_fileoff_t del_endoff, got_endoff; |
| 4927 | int state = BMAP_COWFORK; |
| 4928 | |
| 4929 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4930 | |
| 4931 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4932 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4933 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4934 | ASSERT(del->br_blockcount > 0); |
| 4935 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4936 | ASSERT(got_endoff >= del_endoff); |
| 4937 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4938 | |
| 4939 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4940 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4941 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4942 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4943 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4944 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4945 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4946 | /* |
| 4947 | * Matches the whole extent. Delete the entry. |
| 4948 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4949 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4950 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4951 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4952 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4953 | /* |
| 4954 | * Deleting the first part of the extent. |
| 4955 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4956 | got->br_startoff = del_endoff; |
| 4957 | got->br_blockcount -= del->br_blockcount; |
| 4958 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4959 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4960 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4961 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4962 | /* |
| 4963 | * Deleting the last part of the extent. |
| 4964 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4965 | got->br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4966 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4967 | break; |
| 4968 | case 0: |
| 4969 | /* |
| 4970 | * Deleting the middle of the extent. |
| 4971 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4972 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4973 | |
| 4974 | new.br_startoff = del_endoff; |
| 4975 | new.br_blockcount = got_endoff - del_endoff; |
| 4976 | new.br_state = got->br_state; |
| 4977 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4978 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4979 | xfs_iext_update_extent(ip, state, icur, got); |
| 4980 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4981 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4982 | break; |
| 4983 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 4984 | ip->i_delayed_blks -= del->br_blockcount; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4985 | } |
| 4986 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4987 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4988 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4989 | * after removing space. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4990 | */ |
| 4991 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4992 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4993 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4994 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4995 | struct xfs_iext_cursor *icur, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4996 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4997 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4998 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4999 | int whichfork, /* data or attr fork */ |
| 5000 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5001 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5002 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 5003 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5004 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5005 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 5006 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5007 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5008 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 5009 | int i; /* temp state */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 5010 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5011 | xfs_mount_t *mp; /* mount structure */ |
| 5012 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 5013 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 5014 | /* REFERENCED */ |
| 5015 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 5016 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5017 | struct xfs_bmbt_irec old; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5018 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5019 | mp = ip->i_mount; |
| 5020 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5021 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5022 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5023 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5024 | xfs_iext_get_extent(ifp, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5025 | ASSERT(got.br_startoff <= del->br_startoff); |
| 5026 | del_endoff = del->br_startoff + del->br_blockcount; |
| 5027 | got_endoff = got.br_startoff + got.br_blockcount; |
| 5028 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5029 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5030 | qfield = 0; |
| 5031 | error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5032 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 5033 | /* |
| 5034 | * If it's the case where the directory code is running with no block |
| 5035 | * reservation, and the deleted block is in the middle of its extent, |
| 5036 | * and the resulting insert of an extent would cause transformation to |
| 5037 | * btree format, then reject it. The calling code will then swap blocks |
| 5038 | * around instead. We have to do this now, rather than waiting for the |
| 5039 | * conversion to btree format, since the transaction will be dirty then. |
| 5040 | */ |
| 5041 | if (tp->t_blk_res == 0 && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5042 | ifp->if_format == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5043 | ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) && |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 5044 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 5045 | return -ENOSPC; |
| 5046 | |
| 5047 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5048 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 5049 | xfs_fsblock_t bno; |
| 5050 | xfs_filblks_t len; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5051 | xfs_extlen_t mod; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5052 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5053 | bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize, |
| 5054 | &mod); |
| 5055 | ASSERT(mod == 0); |
| 5056 | len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize, |
| 5057 | &mod); |
| 5058 | ASSERT(mod == 0); |
| 5059 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5060 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 5061 | if (error) |
| 5062 | goto done; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5063 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5064 | nblks = len * mp->m_sb.sb_rextsize; |
| 5065 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 5066 | } else { |
| 5067 | do_fx = 1; |
| 5068 | nblks = del->br_blockcount; |
| 5069 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 5070 | } |
| 5071 | |
| 5072 | del_endblock = del->br_startblock + del->br_blockcount; |
| 5073 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5074 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5075 | if (error) |
| 5076 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5077 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 5078 | error = -EFSCORRUPTED; |
| 5079 | goto done; |
| 5080 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5081 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 5082 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5083 | if (got.br_startoff == del->br_startoff) |
| 5084 | state |= BMAP_LEFT_FILLING; |
| 5085 | if (got_endoff == del_endoff) |
| 5086 | state |= BMAP_RIGHT_FILLING; |
| 5087 | |
| 5088 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 5089 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5090 | /* |
| 5091 | * Matches the whole extent. Delete the entry. |
| 5092 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5093 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5094 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5095 | ifp->if_nextents--; |
| 5096 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5097 | flags |= XFS_ILOG_CORE; |
| 5098 | if (!cur) { |
| 5099 | flags |= xfs_ilog_fext(whichfork); |
| 5100 | break; |
| 5101 | } |
| 5102 | if ((error = xfs_btree_delete(cur, &i))) |
| 5103 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5104 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 5105 | error = -EFSCORRUPTED; |
| 5106 | goto done; |
| 5107 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5108 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5109 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5110 | /* |
| 5111 | * Deleting the first part of the extent. |
| 5112 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5113 | got.br_startoff = del_endoff; |
| 5114 | got.br_startblock = del_endblock; |
| 5115 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5116 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5117 | if (!cur) { |
| 5118 | flags |= xfs_ilog_fext(whichfork); |
| 5119 | break; |
| 5120 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5121 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5122 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5123 | goto done; |
| 5124 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5125 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5126 | /* |
| 5127 | * Deleting the last part of the extent. |
| 5128 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5129 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5130 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5131 | if (!cur) { |
| 5132 | flags |= xfs_ilog_fext(whichfork); |
| 5133 | break; |
| 5134 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5135 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5136 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5137 | goto done; |
| 5138 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5139 | case 0: |
| 5140 | /* |
| 5141 | * Deleting the middle of the extent. |
| 5142 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5143 | old = got; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5144 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5145 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5146 | xfs_iext_update_extent(ip, state, icur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5147 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5148 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5149 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5150 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5151 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5152 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5153 | flags |= XFS_ILOG_CORE; |
| 5154 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5155 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5156 | if (error) |
| 5157 | goto done; |
| 5158 | error = xfs_btree_increment(cur, 0, &i); |
| 5159 | if (error) |
| 5160 | goto done; |
| 5161 | cur->bc_rec.b = new; |
| 5162 | error = xfs_btree_insert(cur, &i); |
| 5163 | if (error && error != -ENOSPC) |
| 5164 | goto done; |
| 5165 | /* |
| 5166 | * If get no-space back from btree insert, it tried a |
| 5167 | * split, and we have a zero block reservation. Fix up |
| 5168 | * our state and return the error. |
| 5169 | */ |
| 5170 | if (error == -ENOSPC) { |
| 5171 | /* |
| 5172 | * Reset the cursor, don't trust it after any |
| 5173 | * insert operation. |
| 5174 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5175 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5176 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5177 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5178 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 5179 | error = -EFSCORRUPTED; |
| 5180 | goto done; |
| 5181 | } |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5182 | /* |
| 5183 | * Update the btree record back |
| 5184 | * to the original value. |
| 5185 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5186 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5187 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5188 | goto done; |
| 5189 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5190 | * Reset the extent record back |
| 5191 | * to the original value. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5192 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5193 | xfs_iext_update_extent(ip, state, icur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5194 | flags = 0; |
| 5195 | error = -ENOSPC; |
| 5196 | goto done; |
| 5197 | } |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5198 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 5199 | error = -EFSCORRUPTED; |
| 5200 | goto done; |
| 5201 | } |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5202 | } else |
| 5203 | flags |= xfs_ilog_fext(whichfork); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5204 | |
| 5205 | ifp->if_nextents++; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5206 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5207 | xfs_iext_insert(ip, icur, &new, state); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5208 | break; |
| 5209 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5210 | |
| 5211 | /* remove reverse mapping */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5212 | xfs_rmap_unmap_extent(tp, ip, whichfork, del); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5213 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5214 | /* |
| 5215 | * If we need to, add to list of extents to delete. |
| 5216 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5217 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5218 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
Darrick J. Wong | 74b4c5d | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 5219 | xfs_refcount_decrease_extent(tp, del); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5220 | } else { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5221 | __xfs_bmap_add_free(tp, del->br_startblock, |
Brian Foster | 4e52933 | 2018-05-10 09:35:42 -0700 | [diff] [blame] | 5222 | del->br_blockcount, NULL, |
| 5223 | (bflags & XFS_BMAPI_NODISCARD) || |
| 5224 | del->br_state == XFS_EXT_UNWRITTEN); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5225 | } |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5226 | } |
| 5227 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5228 | /* |
| 5229 | * Adjust inode # blocks in the file. |
| 5230 | */ |
| 5231 | if (nblks) |
| 5232 | ip->i_d.di_nblocks -= nblks; |
| 5233 | /* |
| 5234 | * Adjust quota data. |
| 5235 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5236 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5237 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5238 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5239 | done: |
| 5240 | *logflagsp = flags; |
| 5241 | return error; |
| 5242 | } |
| 5243 | |
| 5244 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | * Unmap (remove) blocks from a file. |
| 5246 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5247 | * that value. If not all extents in the block range can be removed then |
| 5248 | * *done is set. |
| 5249 | */ |
| 5250 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5251 | __xfs_bunmapi( |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5252 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5254 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5255 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | int flags, /* misc flags */ |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 5257 | xfs_extnum_t nexts) /* number of extents max */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | { |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5259 | struct xfs_btree_cur *cur; /* bmap btree cursor */ |
| 5260 | struct xfs_bmbt_irec del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | int error; /* error return value */ |
| 5262 | xfs_extnum_t extno; /* extent number in list */ |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5263 | struct xfs_bmbt_irec got; /* current extent record */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 5264 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | int isrt; /* freeing in rt area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | int logflags; /* transaction logging flags */ |
| 5267 | xfs_extlen_t mod; /* rt extent offset */ |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 5268 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | int tmp_logflags; /* partial logging flags */ |
| 5270 | int wasdel; /* was a delayed alloc extent */ |
| 5271 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5273 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5274 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5275 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5276 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5277 | struct xfs_iext_cursor icur; |
| 5278 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5280 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5281 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5282 | whichfork = xfs_bmapi_whichfork(flags); |
| 5283 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5285 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5286 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5288 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5289 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5290 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | ASSERT(len > 0); |
| 5292 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5293 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5294 | /* |
| 5295 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5296 | * blowing out the transaction with a mix of EFIs and reflink |
| 5297 | * adjustments. |
| 5298 | */ |
Darrick J. Wong | 8c57b88 | 2017-12-10 18:03:53 -0800 | [diff] [blame] | 5299 | if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5300 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5301 | else |
| 5302 | max_len = len; |
| 5303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5305 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5306 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5307 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5308 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | return 0; |
| 5310 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5311 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5312 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5313 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5314 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5315 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5316 | *rlen = 0; |
| 5317 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5319 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | logflags = 0; |
| 5322 | if (ifp->if_flags & XFS_IFBROOT) { |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5323 | ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5324 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 5325 | cur->bc_ino.flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5326 | } else |
| 5327 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5328 | |
| 5329 | if (isrt) { |
| 5330 | /* |
| 5331 | * Synchronize by locking the bitmap inode. |
| 5332 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5333 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5334 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5335 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5336 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | extno = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5340 | while (end != (xfs_fileoff_t)-1 && end >= start && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5341 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5343 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | * Just back up to the previous extent, if so. |
| 5345 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5346 | if (got.br_startoff > end && |
| 5347 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5348 | done = true; |
| 5349 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | } |
| 5351 | /* |
| 5352 | * Is the last block of this extent before the range |
| 5353 | * we're supposed to delete? If so, we're done. |
| 5354 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5355 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5357 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | break; |
| 5359 | /* |
| 5360 | * Then deal with the (possibly delayed) allocated space |
| 5361 | * we found. |
| 5362 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5364 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5365 | |
| 5366 | /* |
| 5367 | * Make sure we don't touch multiple AGF headers out of order |
| 5368 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5369 | */ |
Omar Sandoval | 69ffe59 | 2019-11-26 16:58:08 -0800 | [diff] [blame] | 5370 | if (!wasdel && !isrt) { |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5371 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5372 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5373 | break; |
| 5374 | prev_agno = agno; |
| 5375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | if (got.br_startoff < start) { |
| 5377 | del.br_startoff = start; |
| 5378 | del.br_blockcount -= start - got.br_startoff; |
| 5379 | if (!wasdel) |
| 5380 | del.br_startblock += start - got.br_startoff; |
| 5381 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5382 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5383 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5384 | |
| 5385 | /* How much can we safely unmap? */ |
| 5386 | if (max_len < del.br_blockcount) { |
| 5387 | del.br_startoff += del.br_blockcount - max_len; |
| 5388 | if (!wasdel) |
| 5389 | del.br_startblock += del.br_blockcount - max_len; |
| 5390 | del.br_blockcount = max_len; |
| 5391 | } |
| 5392 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5393 | if (!isrt) |
| 5394 | goto delete; |
| 5395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | sum = del.br_startblock + del.br_blockcount; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5397 | div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod); |
| 5398 | if (mod) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | /* |
| 5400 | * Realtime extent not lined up at the end. |
| 5401 | * The extent could have been split into written |
| 5402 | * and unwritten pieces, or we could just be |
| 5403 | * unmapping part of it. But we can't really |
| 5404 | * get rid of part of a realtime extent. |
| 5405 | */ |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 5406 | if (del.br_state == XFS_EXT_UNWRITTEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | /* |
| 5408 | * This piece is unwritten, or we're not |
| 5409 | * using unwritten extents. Skip over it. |
| 5410 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5411 | ASSERT(end >= mod); |
| 5412 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | del.br_blockcount : mod; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5414 | if (end < got.br_startoff && |
| 5415 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5416 | done = true; |
| 5417 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5418 | } |
| 5419 | continue; |
| 5420 | } |
| 5421 | /* |
| 5422 | * It's written, turn it unwritten. |
| 5423 | * This is better than zeroing it. |
| 5424 | */ |
| 5425 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5426 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | /* |
| 5428 | * If this spans a realtime extent boundary, |
| 5429 | * chop it back to the start of the one we end at. |
| 5430 | */ |
| 5431 | if (del.br_blockcount > mod) { |
| 5432 | del.br_startoff += del.br_blockcount - mod; |
| 5433 | del.br_startblock += del.br_blockcount - mod; |
| 5434 | del.br_blockcount = mod; |
| 5435 | } |
| 5436 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5437 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5438 | whichfork, &icur, &cur, &del, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5439 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | if (error) |
| 5441 | goto error0; |
| 5442 | goto nodelete; |
| 5443 | } |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5444 | div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod); |
| 5445 | if (mod) { |
Omar Sandoval | 0c4da70 | 2019-11-26 16:58:07 -0800 | [diff] [blame] | 5446 | xfs_extlen_t off = mp->m_sb.sb_rextsize - mod; |
| 5447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | /* |
| 5449 | * Realtime extent is lined up at the end but not |
| 5450 | * at the front. We'll get rid of full extents if |
| 5451 | * we can. |
| 5452 | */ |
Omar Sandoval | 0c4da70 | 2019-11-26 16:58:07 -0800 | [diff] [blame] | 5453 | if (del.br_blockcount > off) { |
| 5454 | del.br_blockcount -= off; |
| 5455 | del.br_startoff += off; |
| 5456 | del.br_startblock += off; |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 5457 | } else if (del.br_startoff == start && |
| 5458 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5459 | tp->t_blk_res == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | /* |
| 5461 | * Can't make it unwritten. There isn't |
| 5462 | * a full extent here so just skip it. |
| 5463 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5464 | ASSERT(end >= del.br_blockcount); |
| 5465 | end -= del.br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5466 | if (got.br_startoff > end && |
| 5467 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5468 | done = true; |
| 5469 | break; |
| 5470 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | continue; |
| 5472 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5473 | struct xfs_bmbt_irec prev; |
Omar Sandoval | 0c4da70 | 2019-11-26 16:58:07 -0800 | [diff] [blame] | 5474 | xfs_fileoff_t unwrite_start; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | /* |
| 5477 | * This one is already unwritten. |
| 5478 | * It must have a written left neighbor. |
| 5479 | * Unwrite the killed part of that one and |
| 5480 | * try again. |
| 5481 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5482 | if (!xfs_iext_prev_extent(ifp, &icur, &prev)) |
| 5483 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5485 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | ASSERT(del.br_startblock == |
| 5487 | prev.br_startblock + prev.br_blockcount); |
Omar Sandoval | 0c4da70 | 2019-11-26 16:58:07 -0800 | [diff] [blame] | 5488 | unwrite_start = max3(start, |
| 5489 | del.br_startoff - mod, |
| 5490 | prev.br_startoff); |
| 5491 | mod = unwrite_start - prev.br_startoff; |
| 5492 | prev.br_startoff = unwrite_start; |
| 5493 | prev.br_startblock += mod; |
| 5494 | prev.br_blockcount -= mod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5496 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5497 | ip, whichfork, &icur, &cur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5498 | &prev, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | if (error) |
| 5500 | goto error0; |
| 5501 | goto nodelete; |
| 5502 | } else { |
| 5503 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5504 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5505 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5506 | ip, whichfork, &icur, &cur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5507 | &del, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | if (error) |
| 5509 | goto error0; |
| 5510 | goto nodelete; |
| 5511 | } |
| 5512 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5513 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5514 | delete: |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5515 | if (wasdel) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5516 | error = xfs_bmap_del_extent_delay(ip, whichfork, &icur, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5517 | &got, &del); |
| 5518 | } else { |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 5519 | error = xfs_bmap_del_extent_real(ip, tp, &icur, cur, |
| 5520 | &del, &tmp_logflags, whichfork, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5521 | flags); |
| 5522 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5523 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | if (error) |
| 5526 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5527 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5528 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5529 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5531 | /* |
| 5532 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5534 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5535 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
| 5536 | (got.br_startoff > end && |
| 5537 | !xfs_iext_prev_extent(ifp, &icur, &got))) { |
| 5538 | done = true; |
| 5539 | break; |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | extno++; |
| 5542 | } |
| 5543 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5544 | if (done || end == (xfs_fileoff_t)-1 || end < start) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5545 | *rlen = 0; |
| 5546 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5547 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | /* |
| 5550 | * Convert to a btree if necessary. |
| 5551 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5552 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 5554 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 5555 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | logflags |= tmp_logflags; |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 5557 | } else { |
| 5558 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5560 | } |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 5561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5562 | error0: |
| 5563 | /* |
| 5564 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5565 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5567 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5568 | ifp->if_format != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5569 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5570 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5571 | ifp->if_format != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5572 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | /* |
| 5574 | * Log inode even in the error case, if the transaction |
| 5575 | * is dirty we'll need to shut down the filesystem. |
| 5576 | */ |
| 5577 | if (logflags) |
| 5578 | xfs_trans_log_inode(tp, ip, logflags); |
| 5579 | if (cur) { |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 5580 | if (!error) |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 5581 | cur->bc_ino.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5582 | xfs_btree_del_cursor(cur, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | } |
| 5584 | return error; |
| 5585 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5586 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5587 | /* Unmap a range of a file. */ |
| 5588 | int |
| 5589 | xfs_bunmapi( |
| 5590 | xfs_trans_t *tp, |
| 5591 | struct xfs_inode *ip, |
| 5592 | xfs_fileoff_t bno, |
| 5593 | xfs_filblks_t len, |
| 5594 | int flags, |
| 5595 | xfs_extnum_t nexts, |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5596 | int *done) |
| 5597 | { |
| 5598 | int error; |
| 5599 | |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 5600 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts); |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5601 | *done = (len == 0); |
| 5602 | return error; |
| 5603 | } |
| 5604 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5605 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5606 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5607 | * extent that precedes the target hole of the shift. |
| 5608 | */ |
| 5609 | STATIC bool |
| 5610 | xfs_bmse_can_merge( |
| 5611 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5612 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5613 | xfs_fileoff_t shift) /* shift fsb */ |
| 5614 | { |
| 5615 | xfs_fileoff_t startoff; |
| 5616 | |
| 5617 | startoff = got->br_startoff - shift; |
| 5618 | |
| 5619 | /* |
| 5620 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5621 | * the preceding extent. |
| 5622 | */ |
| 5623 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5624 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5625 | (left->br_state != got->br_state) || |
| 5626 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5627 | return false; |
| 5628 | |
| 5629 | return true; |
| 5630 | } |
| 5631 | |
| 5632 | /* |
| 5633 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5634 | * hole in the file. If an extent shift would result in the extent being fully |
| 5635 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5636 | * the preceding extent rather than do the shift. |
| 5637 | * |
| 5638 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5639 | * with the provided extents via xfs_bmse_can_merge(). |
| 5640 | */ |
| 5641 | STATIC int |
| 5642 | xfs_bmse_merge( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5643 | struct xfs_trans *tp, |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5644 | struct xfs_inode *ip, |
| 5645 | int whichfork, |
| 5646 | xfs_fileoff_t shift, /* shift fsb */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5647 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5648 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5649 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5650 | struct xfs_btree_cur *cur, |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5651 | int *logflags) /* output */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5652 | { |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5653 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5654 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5655 | xfs_filblks_t blockcount; |
| 5656 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5657 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5658 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5659 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5660 | |
| 5661 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5662 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5663 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5664 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5665 | new = *left; |
| 5666 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5667 | |
| 5668 | /* |
| 5669 | * Update the on-disk extent count, the btree if necessary and log the |
| 5670 | * inode. |
| 5671 | */ |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5672 | ifp->if_nextents--; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5673 | *logflags |= XFS_ILOG_CORE; |
| 5674 | if (!cur) { |
| 5675 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5676 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5677 | } |
| 5678 | |
| 5679 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5680 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5681 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5682 | return error; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5683 | if (XFS_IS_CORRUPT(mp, i != 1)) |
| 5684 | return -EFSCORRUPTED; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5685 | |
| 5686 | error = xfs_btree_delete(cur, &i); |
| 5687 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5688 | return error; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5689 | if (XFS_IS_CORRUPT(mp, i != 1)) |
| 5690 | return -EFSCORRUPTED; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5691 | |
| 5692 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5693 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5694 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5695 | return error; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5696 | if (XFS_IS_CORRUPT(mp, i != 1)) |
| 5697 | return -EFSCORRUPTED; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5698 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5699 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5700 | if (error) |
| 5701 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5702 | |
Brian Foster | e20e174 | 2019-09-18 17:50:45 -0700 | [diff] [blame] | 5703 | /* change to extent format if required after extent removal */ |
| 5704 | error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork); |
| 5705 | if (error) |
| 5706 | return error; |
| 5707 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5708 | done: |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5709 | xfs_iext_remove(ip, icur, 0); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5710 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5711 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5712 | &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5713 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5714 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5715 | xfs_rmap_unmap_extent(tp, ip, whichfork, got); |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5716 | memcpy(&new, got, sizeof(new)); |
| 5717 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5718 | xfs_rmap_map_extent(tp, ip, whichfork, &new); |
| 5719 | return 0; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5720 | } |
| 5721 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5722 | static int |
| 5723 | xfs_bmap_shift_update_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5724 | struct xfs_trans *tp, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5725 | struct xfs_inode *ip, |
| 5726 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5727 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5728 | struct xfs_bmbt_irec *got, |
| 5729 | struct xfs_btree_cur *cur, |
| 5730 | int *logflags, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5731 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5732 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5733 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5734 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5735 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5736 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5737 | *logflags |= XFS_ILOG_CORE; |
| 5738 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5739 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5740 | |
| 5741 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5742 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5743 | if (error) |
| 5744 | return error; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5745 | if (XFS_IS_CORRUPT(mp, i != 1)) |
| 5746 | return -EFSCORRUPTED; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5747 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5748 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5749 | if (error) |
| 5750 | return error; |
| 5751 | } else { |
| 5752 | *logflags |= XFS_ILOG_DEXT; |
| 5753 | } |
| 5754 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5755 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5756 | got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5757 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5758 | /* update reverse mapping */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5759 | xfs_rmap_unmap_extent(tp, ip, whichfork, &prev); |
| 5760 | xfs_rmap_map_extent(tp, ip, whichfork, got); |
| 5761 | return 0; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5762 | } |
| 5763 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5764 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5765 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5766 | struct xfs_trans *tp, |
| 5767 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5768 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5769 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 333f950 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5770 | bool *done) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5771 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5772 | int whichfork = XFS_DATA_FORK; |
| 5773 | struct xfs_mount *mp = ip->i_mount; |
| 5774 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5775 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5776 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5777 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5778 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5779 | int error = 0; |
| 5780 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5781 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5782 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 5783 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5784 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5785 | } |
| 5786 | |
| 5787 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5788 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5789 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5790 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5791 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5792 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5793 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5794 | if (error) |
| 5795 | return error; |
| 5796 | } |
| 5797 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5798 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5799 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 5800 | cur->bc_ino.flags = 0; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5801 | } |
| 5802 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5803 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5804 | *done = true; |
| 5805 | goto del_cursor; |
| 5806 | } |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5807 | if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) { |
| 5808 | error = -EFSCORRUPTED; |
| 5809 | goto del_cursor; |
| 5810 | } |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5811 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5812 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5813 | if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5814 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5815 | error = -EINVAL; |
| 5816 | goto del_cursor; |
| 5817 | } |
| 5818 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5819 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5820 | error = xfs_bmse_merge(tp, ip, whichfork, |
| 5821 | offset_shift_fsb, &icur, &got, &prev, |
| 5822 | cur, &logflags); |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5823 | if (error) |
| 5824 | goto del_cursor; |
| 5825 | goto done; |
| 5826 | } |
| 5827 | } else { |
| 5828 | if (got.br_startoff < offset_shift_fsb) { |
| 5829 | error = -EINVAL; |
| 5830 | goto del_cursor; |
| 5831 | } |
| 5832 | } |
| 5833 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5834 | error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got, |
| 5835 | cur, &logflags, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5836 | if (error) |
| 5837 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5838 | |
Christoph Hellwig | 4263036 | 2017-11-03 10:34:41 -0700 | [diff] [blame] | 5839 | done: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5840 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
| 5841 | *done = true; |
| 5842 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5843 | } |
| 5844 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5845 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5846 | del_cursor: |
| 5847 | if (cur) |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5848 | xfs_btree_del_cursor(cur, error); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5849 | if (logflags) |
| 5850 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5851 | return error; |
| 5852 | } |
| 5853 | |
Darrick J. Wong | f62cb48 | 2018-06-21 23:26:57 -0700 | [diff] [blame] | 5854 | /* Make sure we won't be right-shifting an extent past the maximum bound. */ |
| 5855 | int |
| 5856 | xfs_bmap_can_insert_extents( |
| 5857 | struct xfs_inode *ip, |
| 5858 | xfs_fileoff_t off, |
| 5859 | xfs_fileoff_t shift) |
| 5860 | { |
| 5861 | struct xfs_bmbt_irec got; |
| 5862 | int is_empty; |
| 5863 | int error = 0; |
| 5864 | |
| 5865 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5866 | |
| 5867 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 5868 | return -EIO; |
| 5869 | |
| 5870 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 5871 | error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty); |
| 5872 | if (!error && !is_empty && got.br_startoff >= off && |
| 5873 | ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff) |
| 5874 | error = -EINVAL; |
| 5875 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 5876 | |
| 5877 | return error; |
| 5878 | } |
| 5879 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5880 | int |
| 5881 | xfs_bmap_insert_extents( |
| 5882 | struct xfs_trans *tp, |
| 5883 | struct xfs_inode *ip, |
| 5884 | xfs_fileoff_t *next_fsb, |
| 5885 | xfs_fileoff_t offset_shift_fsb, |
| 5886 | bool *done, |
Brian Foster | 333f950 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5887 | xfs_fileoff_t stop_fsb) |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5888 | { |
| 5889 | int whichfork = XFS_DATA_FORK; |
| 5890 | struct xfs_mount *mp = ip->i_mount; |
| 5891 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5892 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5893 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5894 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5895 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5896 | int error = 0; |
| 5897 | int logflags = 0; |
| 5898 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5899 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 5900 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5901 | return -EFSCORRUPTED; |
| 5902 | } |
| 5903 | |
| 5904 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5905 | return -EIO; |
| 5906 | |
| 5907 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5908 | |
| 5909 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5910 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5911 | if (error) |
| 5912 | return error; |
| 5913 | } |
| 5914 | |
| 5915 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5916 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 5917 | cur->bc_ino.flags = 0; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5918 | } |
| 5919 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5920 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5921 | xfs_iext_last(ifp, &icur); |
| 5922 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5923 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5924 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5925 | goto del_cursor; |
| 5926 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5927 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5928 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5929 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5930 | goto del_cursor; |
| 5931 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5932 | } |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5933 | if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) { |
| 5934 | error = -EFSCORRUPTED; |
| 5935 | goto del_cursor; |
| 5936 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5937 | |
Brian Foster | d0c2204 | 2019-12-11 13:18:38 -0800 | [diff] [blame] | 5938 | if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) { |
Darrick J. Wong | c2414ad | 2019-10-28 16:12:34 -0700 | [diff] [blame] | 5939 | error = -EFSCORRUPTED; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5940 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5941 | } |
| 5942 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5943 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5944 | if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5945 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5946 | error = -EINVAL; |
| 5947 | goto del_cursor; |
| 5948 | } |
| 5949 | |
| 5950 | /* |
| 5951 | * Unlike a left shift (which involves a hole punch), a right |
| 5952 | * shift does not modify extent neighbors in any way. We should |
| 5953 | * never find mergeable extents in this scenario. Check anyways |
| 5954 | * and warn if we encounter two extents that could be one. |
| 5955 | */ |
| 5956 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5957 | WARN_ON_ONCE(1); |
| 5958 | } |
| 5959 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5960 | error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got, |
| 5961 | cur, &logflags, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5962 | if (error) |
| 5963 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5964 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5965 | if (!xfs_iext_prev_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5966 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5967 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5968 | goto del_cursor; |
| 5969 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5970 | |
| 5971 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5972 | del_cursor: |
| 5973 | if (cur) |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5974 | xfs_btree_del_cursor(cur, error); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5975 | if (logflags) |
| 5976 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5977 | return error; |
| 5978 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5979 | |
| 5980 | /* |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5981 | * Splits an extent into two extents at split_fsb block such that it is the |
| 5982 | * first block of the current_ext. @ext is a target extent to be split. |
| 5983 | * @split_fsb is a block where the extents is split. If split_fsb lies in a |
| 5984 | * hole or the first block of extents, just return 0. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5985 | */ |
Brian Foster | b73df17 | 2020-02-26 09:43:15 -0800 | [diff] [blame] | 5986 | int |
| 5987 | xfs_bmap_split_extent( |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5988 | struct xfs_trans *tp, |
| 5989 | struct xfs_inode *ip, |
Brian Foster | 4b77a08 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5990 | xfs_fileoff_t split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5991 | { |
| 5992 | int whichfork = XFS_DATA_FORK; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5993 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5994 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5995 | struct xfs_bmbt_irec got; |
| 5996 | struct xfs_bmbt_irec new; /* split extent */ |
| 5997 | struct xfs_mount *mp = ip->i_mount; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5998 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5999 | struct xfs_iext_cursor icur; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6000 | int error = 0; |
| 6001 | int logflags = 0; |
| 6002 | int i = 0; |
| 6003 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 6004 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 6005 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6006 | return -EFSCORRUPTED; |
| 6007 | } |
| 6008 | |
| 6009 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 6010 | return -EIO; |
| 6011 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6012 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6013 | /* Read in all the extents */ |
| 6014 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6015 | if (error) |
| 6016 | return error; |
| 6017 | } |
| 6018 | |
| 6019 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 6020 | * If there are not extents, or split_fsb lies in a hole we are done. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6021 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 6022 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 6023 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6024 | return 0; |
| 6025 | |
| 6026 | gotblkcnt = split_fsb - got.br_startoff; |
| 6027 | new.br_startoff = split_fsb; |
| 6028 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 6029 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 6030 | new.br_state = got.br_state; |
| 6031 | |
| 6032 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6033 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 6034 | cur->bc_ino.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 6035 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6036 | if (error) |
| 6037 | goto del_cursor; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 6038 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 6039 | error = -EFSCORRUPTED; |
| 6040 | goto del_cursor; |
| 6041 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6042 | } |
| 6043 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6044 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 6045 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, |
| 6046 | &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6047 | |
| 6048 | logflags = XFS_ILOG_CORE; |
| 6049 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 6050 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6051 | if (error) |
| 6052 | goto del_cursor; |
| 6053 | } else |
| 6054 | logflags |= XFS_ILOG_DEXT; |
| 6055 | |
| 6056 | /* Add new extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 6057 | xfs_iext_next(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 6058 | xfs_iext_insert(ip, &icur, &new, 0); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 6059 | ifp->if_nextents++; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6060 | |
| 6061 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 6062 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6063 | if (error) |
| 6064 | goto del_cursor; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 6065 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 6066 | error = -EFSCORRUPTED; |
| 6067 | goto del_cursor; |
| 6068 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6069 | error = xfs_btree_insert(cur, &i); |
| 6070 | if (error) |
| 6071 | goto del_cursor; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 6072 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 6073 | error = -EFSCORRUPTED; |
| 6074 | goto del_cursor; |
| 6075 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6076 | } |
| 6077 | |
| 6078 | /* |
| 6079 | * Convert to a btree if necessary. |
| 6080 | */ |
| 6081 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 6082 | int tmp_logflags; /* partial log flag return val */ |
| 6083 | |
| 6084 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 6085 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 6086 | &tmp_logflags, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6087 | logflags |= tmp_logflags; |
| 6088 | } |
| 6089 | |
| 6090 | del_cursor: |
| 6091 | if (cur) { |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 6092 | cur->bc_ino.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 6093 | xfs_btree_del_cursor(cur, error); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6094 | } |
| 6095 | |
| 6096 | if (logflags) |
| 6097 | xfs_trans_log_inode(tp, ip, logflags); |
| 6098 | return error; |
| 6099 | } |
| 6100 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6101 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6102 | static bool |
| 6103 | xfs_bmap_is_update_needed( |
| 6104 | struct xfs_bmbt_irec *bmap) |
| 6105 | { |
| 6106 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6107 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6108 | } |
| 6109 | |
| 6110 | /* Record a bmap intent. */ |
| 6111 | static int |
| 6112 | __xfs_bmap_add( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6113 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6114 | enum xfs_bmap_intent_type type, |
| 6115 | struct xfs_inode *ip, |
| 6116 | int whichfork, |
| 6117 | struct xfs_bmbt_irec *bmap) |
| 6118 | { |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6119 | struct xfs_bmap_intent *bi; |
| 6120 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6121 | trace_xfs_bmap_defer(tp->t_mountp, |
| 6122 | XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock), |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6123 | type, |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6124 | XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock), |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6125 | ip->i_ino, whichfork, |
| 6126 | bmap->br_startoff, |
| 6127 | bmap->br_blockcount, |
| 6128 | bmap->br_state); |
| 6129 | |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 6130 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6131 | INIT_LIST_HEAD(&bi->bi_list); |
| 6132 | bi->bi_type = type; |
| 6133 | bi->bi_owner = ip; |
| 6134 | bi->bi_whichfork = whichfork; |
| 6135 | bi->bi_bmap = *bmap; |
| 6136 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6137 | xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6138 | return 0; |
| 6139 | } |
| 6140 | |
| 6141 | /* Map an extent into a file. */ |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6142 | void |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6143 | xfs_bmap_map_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6144 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6145 | struct xfs_inode *ip, |
| 6146 | struct xfs_bmbt_irec *PREV) |
| 6147 | { |
| 6148 | if (!xfs_bmap_is_update_needed(PREV)) |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6149 | return; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6150 | |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6151 | __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, XFS_DATA_FORK, PREV); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | /* Unmap an extent out of a file. */ |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6155 | void |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6156 | xfs_bmap_unmap_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6157 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6158 | struct xfs_inode *ip, |
| 6159 | struct xfs_bmbt_irec *PREV) |
| 6160 | { |
| 6161 | if (!xfs_bmap_is_update_needed(PREV)) |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6162 | return; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6163 | |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6164 | __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, XFS_DATA_FORK, PREV); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6165 | } |
| 6166 | |
| 6167 | /* |
| 6168 | * Process one of the deferred bmap operations. We pass back the |
| 6169 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6170 | */ |
| 6171 | int |
| 6172 | xfs_bmap_finish_one( |
| 6173 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6174 | struct xfs_inode *ip, |
| 6175 | enum xfs_bmap_intent_type type, |
| 6176 | int whichfork, |
| 6177 | xfs_fileoff_t startoff, |
| 6178 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6179 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6180 | xfs_exntst_t state) |
| 6181 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6182 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6183 | |
Brian Foster | 3728379 | 2018-07-11 22:26:23 -0700 | [diff] [blame] | 6184 | ASSERT(tp->t_firstblock == NULLFSBLOCK); |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6185 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6186 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6187 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6188 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6189 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6190 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6191 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6192 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6193 | |
| 6194 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6195 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6196 | return -EIO; |
| 6197 | |
| 6198 | switch (type) { |
| 6199 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6200 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Brian Foster | ff3edf2 | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 6201 | startblock, 0); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6202 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6203 | break; |
| 6204 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6205 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 6206 | XFS_BMAPI_REMAP, 1); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6207 | break; |
| 6208 | default: |
| 6209 | ASSERT(0); |
| 6210 | error = -EFSCORRUPTED; |
| 6211 | } |
| 6212 | |
| 6213 | return error; |
| 6214 | } |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6215 | |
| 6216 | /* Check that an inode's extent does not have invalid flags or bad ranges. */ |
| 6217 | xfs_failaddr_t |
| 6218 | xfs_bmap_validate_extent( |
| 6219 | struct xfs_inode *ip, |
| 6220 | int whichfork, |
| 6221 | struct xfs_bmbt_irec *irec) |
| 6222 | { |
| 6223 | struct xfs_mount *mp = ip->i_mount; |
| 6224 | xfs_fsblock_t endfsb; |
| 6225 | bool isrt; |
| 6226 | |
| 6227 | isrt = XFS_IS_REALTIME_INODE(ip); |
| 6228 | endfsb = irec->br_startblock + irec->br_blockcount - 1; |
| 6229 | if (isrt) { |
| 6230 | if (!xfs_verify_rtbno(mp, irec->br_startblock)) |
| 6231 | return __this_address; |
| 6232 | if (!xfs_verify_rtbno(mp, endfsb)) |
| 6233 | return __this_address; |
| 6234 | } else { |
| 6235 | if (!xfs_verify_fsbno(mp, irec->br_startblock)) |
| 6236 | return __this_address; |
| 6237 | if (!xfs_verify_fsbno(mp, endfsb)) |
| 6238 | return __this_address; |
| 6239 | if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) != |
| 6240 | XFS_FSB_TO_AGNO(mp, endfsb)) |
| 6241 | return __this_address; |
| 6242 | } |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 6243 | if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK) |
| 6244 | return __this_address; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6245 | return NULL; |
| 6246 | } |