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); |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 1102 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, 0); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1103 | ip->i_afp->if_format = XFS_DINODE_FMT_EXTENTS; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1104 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1105 | logflags = 0; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1106 | switch (ip->i_df.if_format) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1107 | case XFS_DINODE_FMT_LOCAL: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1108 | error = xfs_bmap_add_attrfork_local(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1109 | break; |
| 1110 | case XFS_DINODE_FMT_EXTENTS: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1111 | error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1112 | break; |
| 1113 | case XFS_DINODE_FMT_BTREE: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1114 | error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1115 | break; |
| 1116 | default: |
| 1117 | error = 0; |
| 1118 | break; |
| 1119 | } |
| 1120 | if (logflags) |
| 1121 | xfs_trans_log_inode(tp, ip, logflags); |
| 1122 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1123 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1124 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1125 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1126 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1127 | |
| 1128 | spin_lock(&mp->m_sb_lock); |
| 1129 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1130 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1131 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1132 | } |
| 1133 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1134 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1135 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1136 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1137 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1138 | if (log_sb) |
| 1139 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1140 | } |
| 1141 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1142 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1143 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1144 | return error; |
| 1145 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1146 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1147 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1148 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1149 | return error; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
| 1153 | * Internal and external extent tree search functions. |
| 1154 | */ |
| 1155 | |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1156 | struct xfs_iread_state { |
| 1157 | struct xfs_iext_cursor icur; |
| 1158 | xfs_extnum_t loaded; |
| 1159 | }; |
| 1160 | |
| 1161 | /* Stuff every bmbt record from this block into the incore extent map. */ |
| 1162 | static int |
| 1163 | xfs_iread_bmbt_block( |
| 1164 | struct xfs_btree_cur *cur, |
| 1165 | int level, |
| 1166 | void *priv) |
| 1167 | { |
| 1168 | struct xfs_iread_state *ir = priv; |
| 1169 | struct xfs_mount *mp = cur->bc_mp; |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 1170 | struct xfs_inode *ip = cur->bc_ino.ip; |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1171 | struct xfs_btree_block *block; |
| 1172 | struct xfs_buf *bp; |
| 1173 | struct xfs_bmbt_rec *frp; |
| 1174 | xfs_extnum_t num_recs; |
| 1175 | xfs_extnum_t j; |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 1176 | int whichfork = cur->bc_ino.whichfork; |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1177 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1178 | |
| 1179 | block = xfs_btree_get_block(cur, level, &bp); |
| 1180 | |
| 1181 | /* Abort if we find more records than nextents. */ |
| 1182 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1183 | if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) { |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1184 | xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).", |
| 1185 | (unsigned long long)ip->i_ino); |
| 1186 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block, |
| 1187 | sizeof(*block), __this_address); |
| 1188 | return -EFSCORRUPTED; |
| 1189 | } |
| 1190 | |
| 1191 | /* Copy records into the incore cache. */ |
| 1192 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 1193 | for (j = 0; j < num_recs; j++, frp++, ir->loaded++) { |
| 1194 | struct xfs_bmbt_irec new; |
| 1195 | xfs_failaddr_t fa; |
| 1196 | |
| 1197 | xfs_bmbt_disk_get_all(frp, &new); |
| 1198 | fa = xfs_bmap_validate_extent(ip, whichfork, &new); |
| 1199 | if (fa) { |
| 1200 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
| 1201 | "xfs_iread_extents(2)", frp, |
| 1202 | sizeof(*frp), fa); |
| 1203 | return -EFSCORRUPTED; |
| 1204 | } |
| 1205 | xfs_iext_insert(ip, &ir->icur, &new, |
| 1206 | xfs_bmap_fork_to_state(whichfork)); |
| 1207 | trace_xfs_read_extent(ip, &ir->icur, |
| 1208 | xfs_bmap_fork_to_state(whichfork), _THIS_IP_); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1209 | xfs_iext_next(ifp, &ir->icur); |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1215 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1216 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1217 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1218 | int |
| 1219 | xfs_iread_extents( |
| 1220 | struct xfs_trans *tp, |
| 1221 | struct xfs_inode *ip, |
| 1222 | int whichfork) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1223 | { |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1224 | struct xfs_iread_state ir; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1225 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1226 | struct xfs_mount *mp = ip->i_mount; |
| 1227 | struct xfs_btree_cur *cur; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1228 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1229 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1230 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1231 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1232 | if (XFS_IS_CORRUPT(mp, ifp->if_format != XFS_DINODE_FMT_BTREE)) { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1233 | error = -EFSCORRUPTED; |
| 1234 | goto out; |
| 1235 | } |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1236 | |
| 1237 | ir.loaded = 0; |
| 1238 | xfs_iext_first(ifp, &ir.icur); |
| 1239 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 1240 | error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block, |
| 1241 | XFS_BTREE_VISIT_RECORDS, &ir); |
| 1242 | xfs_btree_del_cursor(cur, error); |
| 1243 | if (error) |
| 1244 | goto out; |
| 1245 | |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1246 | if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) { |
Darrick J. Wong | e992ae8 | 2019-10-28 16:12:35 -0700 | [diff] [blame] | 1247 | error = -EFSCORRUPTED; |
| 1248 | goto out; |
| 1249 | } |
| 1250 | ASSERT(ir.loaded == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1251 | |
| 1252 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1253 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1254 | out: |
| 1255 | xfs_iext_destroy(ifp); |
| 1256 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1257 | } |
| 1258 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1259 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1260 | * Returns the relative block number of the first unused block(s) in the given |
| 1261 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1262 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1263 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1264 | */ |
| 1265 | int /* error */ |
| 1266 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1267 | struct xfs_trans *tp, /* transaction pointer */ |
| 1268 | struct xfs_inode *ip, /* incore inode */ |
| 1269 | xfs_extlen_t len, /* size of hole to find */ |
| 1270 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1271 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1272 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1273 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1274 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1275 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1276 | xfs_fileoff_t lastaddr = 0; |
| 1277 | xfs_fileoff_t lowest, max; |
| 1278 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1279 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1280 | if (ifp->if_format == XFS_DINODE_FMT_LOCAL) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1281 | *first_unused = 0; |
| 1282 | return 0; |
| 1283 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1284 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1285 | ASSERT(xfs_ifork_has_extents(ifp)); |
| 1286 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1287 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1288 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1289 | if (error) |
| 1290 | return error; |
| 1291 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1292 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1293 | lowest = max = *first_unused; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1294 | for_each_xfs_iext(ifp, &icur, &got) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1295 | /* |
| 1296 | * See if the hole before this extent will work. |
| 1297 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1298 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1299 | got.br_startoff - max >= len) |
| 1300 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1301 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1302 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1303 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1304 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1305 | *first_unused = max; |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1310 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1311 | * last_block (input value) in the file. |
| 1312 | * This is not based on i_size, it is based on the extent records. |
| 1313 | * Returns 0 for local files, as they do not have extent records. |
| 1314 | */ |
| 1315 | int /* error */ |
| 1316 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1317 | struct xfs_trans *tp, /* transaction pointer */ |
| 1318 | struct xfs_inode *ip, /* incore inode */ |
| 1319 | xfs_fileoff_t *last_block, /* last block */ |
| 1320 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1321 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1322 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1323 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1324 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1325 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1326 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1327 | switch (ifp->if_format) { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1328 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1329 | *last_block = 0; |
| 1330 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1331 | case XFS_DINODE_FMT_BTREE: |
| 1332 | case XFS_DINODE_FMT_EXTENTS: |
| 1333 | break; |
| 1334 | default: |
Darrick J. Wong | a5155b8 | 2019-11-02 09:40:53 -0700 | [diff] [blame] | 1335 | ASSERT(0); |
Darrick J. Wong | c2414ad | 2019-10-28 16:12:34 -0700 | [diff] [blame] | 1336 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1337 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1338 | |
| 1339 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1340 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1341 | if (error) |
| 1342 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1343 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1344 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1345 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1346 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1347 | return 0; |
| 1348 | } |
| 1349 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1350 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1351 | xfs_bmap_last_extent( |
| 1352 | struct xfs_trans *tp, |
| 1353 | struct xfs_inode *ip, |
| 1354 | int whichfork, |
| 1355 | struct xfs_bmbt_irec *rec, |
| 1356 | int *is_empty) |
| 1357 | { |
| 1358 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1359 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1360 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1361 | |
| 1362 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1363 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1364 | if (error) |
| 1365 | return error; |
| 1366 | } |
| 1367 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1368 | xfs_iext_last(ifp, &icur); |
| 1369 | if (!xfs_iext_get_extent(ifp, &icur, rec)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1370 | *is_empty = 1; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1371 | else |
| 1372 | *is_empty = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1373 | return 0; |
| 1374 | } |
| 1375 | |
| 1376 | /* |
| 1377 | * Check the last inode extent to determine whether this allocation will result |
| 1378 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1379 | * blocks at the end of the file which do not start at the previous data block, |
| 1380 | * we will try to align the new blocks at stripe unit boundaries. |
| 1381 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1382 | * 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] | 1383 | * at, or past the EOF. |
| 1384 | */ |
| 1385 | STATIC int |
| 1386 | xfs_bmap_isaeof( |
| 1387 | struct xfs_bmalloca *bma, |
| 1388 | int whichfork) |
| 1389 | { |
| 1390 | struct xfs_bmbt_irec rec; |
| 1391 | int is_empty; |
| 1392 | int error; |
| 1393 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1394 | bma->aeof = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1395 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1396 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1397 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1398 | return error; |
| 1399 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1400 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1401 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1402 | return 0; |
| 1403 | } |
| 1404 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1405 | /* |
| 1406 | * Check if we are allocation or past the last extent, or at least into |
| 1407 | * the last delayed allocated extent. |
| 1408 | */ |
| 1409 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1410 | (bma->offset >= rec.br_startoff && |
| 1411 | isnullstartblock(rec.br_startblock)); |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1416 | * Returns the file-relative block number of the first block past eof in |
| 1417 | * the file. This is not based on i_size, it is based on the extent records. |
| 1418 | * Returns 0 for local files, as they do not have extent records. |
| 1419 | */ |
| 1420 | int |
| 1421 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1422 | struct xfs_inode *ip, |
| 1423 | xfs_fileoff_t *last_block, |
| 1424 | int whichfork) |
| 1425 | { |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1426 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1427 | struct xfs_bmbt_irec rec; |
| 1428 | int is_empty; |
| 1429 | int error; |
| 1430 | |
| 1431 | *last_block = 0; |
| 1432 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1433 | if (ifp->if_format == XFS_DINODE_FMT_LOCAL) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1434 | return 0; |
| 1435 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1436 | 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] | 1437 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1438 | |
| 1439 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1440 | if (error || is_empty) |
| 1441 | return error; |
| 1442 | |
| 1443 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * Returns whether the selected fork of the inode has exactly one |
| 1449 | * block or not. For the data fork we check this matches di_size, |
| 1450 | * implying the file's range is 0..bsize-1. |
| 1451 | */ |
| 1452 | int /* 1=>1 block, 0=>otherwise */ |
| 1453 | xfs_bmap_one_block( |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1454 | struct xfs_inode *ip, /* incore inode */ |
| 1455 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1456 | { |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1457 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1458 | int rval; /* return value */ |
| 1459 | struct xfs_bmbt_irec s; /* internal version of extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1460 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1461 | |
| 1462 | #ifndef DEBUG |
| 1463 | if (whichfork == XFS_DATA_FORK) |
| 1464 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1465 | #endif /* !DEBUG */ |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1466 | if (ifp->if_nextents != 1) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1467 | return 0; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 1468 | if (ifp->if_format != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1469 | return 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1470 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1471 | xfs_iext_first(ifp, &icur); |
| 1472 | xfs_iext_get_extent(ifp, &icur, &s); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1473 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1474 | if (rval && whichfork == XFS_DATA_FORK) |
| 1475 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1476 | return rval; |
| 1477 | } |
| 1478 | |
| 1479 | /* |
| 1480 | * Extent tree manipulation functions used during allocation. |
| 1481 | */ |
| 1482 | |
| 1483 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1484 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | */ |
| 1486 | STATIC int /* error */ |
| 1487 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1488 | struct xfs_bmalloca *bma, |
| 1489 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | { |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1491 | struct xfs_mount *mp = bma->ip->i_mount; |
| 1492 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1493 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | int i; /* temp state */ |
| 1496 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1497 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1498 | /* left is 0, right is 1, prev is 2 */ |
| 1499 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1500 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1501 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1502 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1503 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | int tmp_rval; /* partial logging flags */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1505 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1507 | ASSERT(whichfork != XFS_ATTR_FORK); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1508 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1509 | ASSERT(!bma->cur || |
Dave Chinner | 8ef5479 | 2020-03-10 17:54:38 -0700 | [diff] [blame] | 1510 | (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1511 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1512 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | #define LEFT r[0] |
| 1515 | #define RIGHT r[1] |
| 1516 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
| 1518 | /* |
| 1519 | * Set up a bunch of variables to make the tests simpler. |
| 1520 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1521 | xfs_iext_get_extent(ifp, &bma->icur, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1523 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1525 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1526 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1527 | da_old = startblockval(PREV.br_startblock); |
| 1528 | da_new = 0; |
| 1529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | /* |
| 1531 | * Set flags determining what part of the previous delayed allocation |
| 1532 | * extent is being replaced by a real allocation. |
| 1533 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1534 | if (PREV.br_startoff == new->br_startoff) |
| 1535 | state |= BMAP_LEFT_FILLING; |
| 1536 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1537 | state |= BMAP_RIGHT_FILLING; |
| 1538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | /* |
| 1540 | * Check and set flags if this segment has a left neighbor. |
| 1541 | * Don't set contiguous if the combined extent would be too large. |
| 1542 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1543 | if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1544 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1545 | if (isnullstartblock(LEFT.br_startblock)) |
| 1546 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1548 | |
| 1549 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1550 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1551 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1552 | LEFT.br_state == new->br_state && |
| 1553 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1554 | state |= BMAP_LEFT_CONTIG; |
| 1555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | /* |
| 1557 | * Check and set flags if this segment has a right neighbor. |
| 1558 | * Don't set contiguous if the combined extent would be too large. |
| 1559 | * Also check for all-three-contiguous being too large. |
| 1560 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1561 | if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1562 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1563 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1564 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1566 | |
| 1567 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1568 | new_endoff == RIGHT.br_startoff && |
| 1569 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1570 | new->br_state == RIGHT.br_state && |
| 1571 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1572 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1573 | BMAP_RIGHT_FILLING)) != |
| 1574 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1575 | BMAP_RIGHT_FILLING) || |
| 1576 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1577 | <= MAXEXTLEN)) |
| 1578 | state |= BMAP_RIGHT_CONTIG; |
| 1579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | error = 0; |
| 1581 | /* |
| 1582 | * Switch out based on the FILLING and CONTIG state bits. |
| 1583 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1584 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1585 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1586 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1587 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | /* |
| 1589 | * Filling in all of a previously delayed allocation extent. |
| 1590 | * The left and right neighbors are both contiguous with new. |
| 1591 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1592 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1593 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1594 | xfs_iext_remove(bma->ip, &bma->icur, state); |
| 1595 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1596 | xfs_iext_prev(ifp, &bma->icur); |
| 1597 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1598 | ifp->if_nextents--; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1599 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1600 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1602 | else { |
| 1603 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1604 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1605 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1607 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1608 | error = -EFSCORRUPTED; |
| 1609 | goto done; |
| 1610 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1611 | error = xfs_btree_delete(bma->cur, &i); |
| 1612 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1614 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1615 | error = -EFSCORRUPTED; |
| 1616 | goto done; |
| 1617 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1618 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1619 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1621 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1622 | error = -EFSCORRUPTED; |
| 1623 | goto done; |
| 1624 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1625 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1626 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | goto done; |
| 1628 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | break; |
| 1630 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1631 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | /* |
| 1633 | * Filling in all of a previously delayed allocation extent. |
| 1634 | * The left neighbor is contiguous, the right is not. |
| 1635 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1636 | old = LEFT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1637 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1638 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1639 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1640 | xfs_iext_prev(ifp, &bma->icur); |
| 1641 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1642 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1643 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | rval = XFS_ILOG_DEXT; |
| 1645 | else { |
| 1646 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1647 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1648 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1650 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1651 | error = -EFSCORRUPTED; |
| 1652 | goto done; |
| 1653 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1654 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1655 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | goto done; |
| 1657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | break; |
| 1659 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1660 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | /* |
| 1662 | * Filling in all of a previously delayed allocation extent. |
Dave Chinner | 9230a0b | 2018-11-19 22:50:08 -0800 | [diff] [blame] | 1663 | * The right neighbor is contiguous, the left is not. Take care |
| 1664 | * with delay -> unwritten extent allocation here because the |
| 1665 | * delalloc record we are overwriting is always written. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1667 | PREV.br_startblock = new->br_startblock; |
| 1668 | PREV.br_blockcount += RIGHT.br_blockcount; |
Dave Chinner | 9230a0b | 2018-11-19 22:50:08 -0800 | [diff] [blame] | 1669 | PREV.br_state = new->br_state; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1670 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1671 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1672 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1673 | xfs_iext_prev(ifp, &bma->icur); |
| 1674 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1675 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1676 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | rval = XFS_ILOG_DEXT; |
| 1678 | else { |
| 1679 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1680 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1681 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1683 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1684 | error = -EFSCORRUPTED; |
| 1685 | goto done; |
| 1686 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1687 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1688 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | goto done; |
| 1690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | break; |
| 1692 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1693 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | /* |
| 1695 | * Filling in all of a previously delayed allocation extent. |
| 1696 | * Neither the left nor right neighbors are contiguous with |
| 1697 | * the new one. |
| 1698 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1699 | PREV.br_startblock = new->br_startblock; |
| 1700 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1701 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1702 | ifp->if_nextents++; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1703 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1704 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1706 | else { |
| 1707 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1708 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1709 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1711 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 1712 | error = -EFSCORRUPTED; |
| 1713 | goto done; |
| 1714 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1715 | error = xfs_btree_insert(bma->cur, &i); |
| 1716 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1718 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1719 | error = -EFSCORRUPTED; |
| 1720 | goto done; |
| 1721 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | break; |
| 1724 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1725 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | /* |
| 1727 | * Filling in the first part of a previous delayed allocation. |
| 1728 | * The left neighbor is contiguous. |
| 1729 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1730 | old = LEFT; |
| 1731 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1732 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1733 | startblockval(PREV.br_startblock)); |
| 1734 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1735 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1736 | |
Christoph Hellwig | bf99971 | 2017-11-03 10:34:38 -0700 | [diff] [blame] | 1737 | PREV.br_blockcount = temp; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1738 | PREV.br_startoff += new->br_blockcount; |
| 1739 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1740 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1741 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1742 | xfs_iext_prev(ifp, &bma->icur); |
| 1743 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1744 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1745 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | rval = XFS_ILOG_DEXT; |
| 1747 | else { |
| 1748 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1749 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1750 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1752 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1753 | error = -EFSCORRUPTED; |
| 1754 | goto done; |
| 1755 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1756 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1757 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | goto done; |
| 1759 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | break; |
| 1761 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1762 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | /* |
| 1764 | * Filling in the first part of a previous delayed allocation. |
| 1765 | * The left neighbor is not contiguous. |
| 1766 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1767 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1768 | ifp->if_nextents++; |
| 1769 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1770 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1772 | else { |
| 1773 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1774 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1775 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1777 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 1778 | error = -EFSCORRUPTED; |
| 1779 | goto done; |
| 1780 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1781 | error = xfs_btree_insert(bma->cur, &i); |
| 1782 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1784 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1785 | error = -EFSCORRUPTED; |
| 1786 | goto done; |
| 1787 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1789 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1790 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1791 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1792 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | rval |= tmp_rval; |
| 1794 | if (error) |
| 1795 | goto done; |
| 1796 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1797 | |
| 1798 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1799 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1800 | startblockval(PREV.br_startblock) - |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 1801 | (bma->cur ? bma->cur->bc_ino.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1802 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1803 | PREV.br_startoff = new_endoff; |
| 1804 | PREV.br_blockcount = temp; |
| 1805 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1806 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1807 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1808 | xfs_iext_prev(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | break; |
| 1810 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1811 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | /* |
| 1813 | * Filling in the last part of a previous delayed allocation. |
| 1814 | * The right neighbor is contiguous with the new allocation. |
| 1815 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1816 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1817 | RIGHT.br_startoff = new->br_startoff; |
| 1818 | RIGHT.br_startblock = new->br_startblock; |
| 1819 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1820 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1821 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | rval = XFS_ILOG_DEXT; |
| 1823 | else { |
| 1824 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1825 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1826 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1828 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1829 | error = -EFSCORRUPTED; |
| 1830 | goto done; |
| 1831 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1832 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1833 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | goto done; |
| 1835 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1836 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1837 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1838 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1839 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1840 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1841 | PREV.br_blockcount = temp; |
| 1842 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1843 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1844 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1845 | xfs_iext_next(ifp, &bma->icur); |
| 1846 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | break; |
| 1848 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1849 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | /* |
| 1851 | * Filling in the last part of a previous delayed allocation. |
| 1852 | * The right neighbor is not contiguous. |
| 1853 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1854 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1855 | ifp->if_nextents++; |
| 1856 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1857 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1859 | else { |
| 1860 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1861 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1862 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1864 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 1865 | error = -EFSCORRUPTED; |
| 1866 | goto done; |
| 1867 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1868 | error = xfs_btree_insert(bma->cur, &i); |
| 1869 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1871 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1872 | error = -EFSCORRUPTED; |
| 1873 | goto done; |
| 1874 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1876 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1877 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1878 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1879 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | rval |= tmp_rval; |
| 1881 | if (error) |
| 1882 | goto done; |
| 1883 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1884 | |
| 1885 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1886 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1887 | startblockval(PREV.br_startblock) - |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 1888 | (bma->cur ? bma->cur->bc_ino.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1889 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1890 | PREV.br_startblock = nullstartblock(da_new); |
| 1891 | PREV.br_blockcount = temp; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1892 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1893 | xfs_iext_next(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | break; |
| 1895 | |
| 1896 | case 0: |
| 1897 | /* |
| 1898 | * Filling in the middle part of a previous delayed allocation. |
| 1899 | * Contiguity is impossible here. |
| 1900 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1901 | * |
| 1902 | * We start with a delayed allocation: |
| 1903 | * |
| 1904 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1905 | * PREV @ idx |
| 1906 | * |
| 1907 | * and we are allocating: |
| 1908 | * +rrrrrrrrrrrrrrrrr+ |
| 1909 | * new |
| 1910 | * |
| 1911 | * and we set it up for insertion as: |
| 1912 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1913 | * new |
| 1914 | * PREV @ idx LEFT RIGHT |
| 1915 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1917 | old = PREV; |
| 1918 | |
| 1919 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1920 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1921 | |
| 1922 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1923 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1924 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1925 | RIGHT.br_blockcount = |
| 1926 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1927 | RIGHT.br_startblock = |
| 1928 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1929 | RIGHT.br_blockcount)); |
| 1930 | |
| 1931 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1932 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1933 | PREV.br_startblock = |
| 1934 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1935 | PREV.br_blockcount)); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1936 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1937 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1938 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1939 | xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); |
| 1940 | xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 1941 | ifp->if_nextents++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1942 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1943 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1945 | else { |
| 1946 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1947 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1948 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1950 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 1951 | error = -EFSCORRUPTED; |
| 1952 | goto done; |
| 1953 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1954 | error = xfs_btree_insert(bma->cur, &i); |
| 1955 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 1957 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 1958 | error = -EFSCORRUPTED; |
| 1959 | goto done; |
| 1960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1962 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1963 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1964 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1965 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | rval |= tmp_rval; |
| 1967 | if (error) |
| 1968 | goto done; |
| 1969 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1970 | |
| 1971 | da_new = startblockval(PREV.br_startblock) + |
| 1972 | startblockval(RIGHT.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | break; |
| 1974 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1975 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1976 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1977 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1978 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1979 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1980 | case BMAP_LEFT_CONTIG: |
| 1981 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | /* |
| 1983 | * These cases are all impossible. |
| 1984 | */ |
| 1985 | ASSERT(0); |
| 1986 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1987 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 1988 | /* add reverse mapping unless caller opted out */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 1989 | if (!(bma->flags & XFS_BMAPI_NORMAP)) |
| 1990 | xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 1991 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1992 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1993 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1994 | int tmp_logflags; /* partial log flag return val */ |
| 1995 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1996 | ASSERT(bma->cur == NULL); |
| 1997 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1998 | &bma->cur, da_old > 0, &tmp_logflags, |
| 1999 | whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2000 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2001 | if (error) |
| 2002 | goto done; |
| 2003 | } |
| 2004 | |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 2005 | if (da_new != da_old) |
| 2006 | xfs_mod_delalloc(mp, (int64_t)da_new - da_old); |
| 2007 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2008 | if (bma->cur) { |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 2009 | da_new += bma->cur->bc_ino.allocated; |
| 2010 | bma->cur->bc_ino.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2013 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2014 | if (da_new != da_old) { |
| 2015 | ASSERT(state == 0 || da_new < da_old); |
| 2016 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2017 | false); |
| 2018 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2019 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2020 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2022 | if (whichfork != XFS_COW_FORK) |
| 2023 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | return error; |
| 2025 | #undef LEFT |
| 2026 | #undef RIGHT |
| 2027 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2031 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | */ |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 2033 | int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2035 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2037 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2038 | struct xfs_iext_cursor *icur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2040 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2041 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | int i; /* temp state */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 2046 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2049 | /* left is 0, right is 1, prev is 2 */ |
| 2050 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2051 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2052 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2053 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2055 | *logflagsp = 0; |
| 2056 | |
| 2057 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2058 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2059 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2060 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2061 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2062 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | #define LEFT r[0] |
| 2065 | #define RIGHT r[1] |
| 2066 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | /* |
| 2069 | * Set up a bunch of variables to make the tests simpler. |
| 2070 | */ |
| 2071 | error = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2072 | xfs_iext_get_extent(ifp, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2073 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2075 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2076 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | /* |
| 2079 | * Set flags determining what part of the previous oldext allocation |
| 2080 | * extent is being replaced by a newext allocation. |
| 2081 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2082 | if (PREV.br_startoff == new->br_startoff) |
| 2083 | state |= BMAP_LEFT_FILLING; |
| 2084 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2085 | state |= BMAP_RIGHT_FILLING; |
| 2086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | /* |
| 2088 | * Check and set flags if this segment has a left neighbor. |
| 2089 | * Don't set contiguous if the combined extent would be too large. |
| 2090 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2091 | if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2092 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2093 | if (isnullstartblock(LEFT.br_startblock)) |
| 2094 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2096 | |
| 2097 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2098 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2099 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2100 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2101 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2102 | state |= BMAP_LEFT_CONTIG; |
| 2103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | /* |
| 2105 | * Check and set flags if this segment has a right neighbor. |
| 2106 | * Don't set contiguous if the combined extent would be too large. |
| 2107 | * Also check for all-three-contiguous being too large. |
| 2108 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2109 | if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2110 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2111 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2112 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2114 | |
| 2115 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2116 | new_endoff == RIGHT.br_startoff && |
| 2117 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2118 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2119 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2120 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2121 | BMAP_RIGHT_FILLING)) != |
| 2122 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2123 | BMAP_RIGHT_FILLING) || |
| 2124 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2125 | <= MAXEXTLEN)) |
| 2126 | state |= BMAP_RIGHT_CONTIG; |
| 2127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | /* |
| 2129 | * Switch out based on the FILLING and CONTIG state bits. |
| 2130 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2131 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2132 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2133 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2134 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | /* |
| 2136 | * Setting all of a previous oldext extent to newext. |
| 2137 | * The left and right neighbors are both contiguous with new. |
| 2138 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2139 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2140 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2141 | xfs_iext_remove(ip, icur, state); |
| 2142 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2143 | xfs_iext_prev(ifp, icur); |
| 2144 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2145 | ifp->if_nextents -= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | if (cur == NULL) |
| 2147 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2148 | else { |
| 2149 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2150 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2151 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2153 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2154 | error = -EFSCORRUPTED; |
| 2155 | goto done; |
| 2156 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2157 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2159 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2160 | error = -EFSCORRUPTED; |
| 2161 | goto done; |
| 2162 | } |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2163 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2165 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2166 | error = -EFSCORRUPTED; |
| 2167 | goto done; |
| 2168 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2169 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2171 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2172 | error = -EFSCORRUPTED; |
| 2173 | goto done; |
| 2174 | } |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2175 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2177 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2178 | error = -EFSCORRUPTED; |
| 2179 | goto done; |
| 2180 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2181 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2182 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | goto done; |
| 2184 | } |
| 2185 | break; |
| 2186 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2187 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | /* |
| 2189 | * Setting all of a previous oldext extent to newext. |
| 2190 | * The left neighbor is contiguous, the right is not. |
| 2191 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2192 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2193 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2194 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2195 | xfs_iext_prev(ifp, icur); |
| 2196 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2197 | ifp->if_nextents--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | if (cur == NULL) |
| 2199 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2200 | else { |
| 2201 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2202 | error = xfs_bmbt_lookup_eq(cur, &PREV, &i); |
| 2203 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2205 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2206 | error = -EFSCORRUPTED; |
| 2207 | goto done; |
| 2208 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2209 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2211 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2212 | error = -EFSCORRUPTED; |
| 2213 | goto done; |
| 2214 | } |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2215 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2217 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2218 | error = -EFSCORRUPTED; |
| 2219 | goto done; |
| 2220 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2221 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2222 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | goto done; |
| 2224 | } |
| 2225 | break; |
| 2226 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2227 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | /* |
| 2229 | * Setting all of a previous oldext extent to newext. |
| 2230 | * The right neighbor is contiguous, the left is not. |
| 2231 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2232 | PREV.br_blockcount += RIGHT.br_blockcount; |
| 2233 | PREV.br_state = new->br_state; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2234 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2235 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2236 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2237 | xfs_iext_prev(ifp, icur); |
| 2238 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2239 | ifp->if_nextents--; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | if (cur == NULL) |
| 2242 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2243 | else { |
| 2244 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2245 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2246 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2248 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2249 | error = -EFSCORRUPTED; |
| 2250 | goto done; |
| 2251 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2252 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2254 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2255 | error = -EFSCORRUPTED; |
| 2256 | goto done; |
| 2257 | } |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2258 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2260 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2261 | error = -EFSCORRUPTED; |
| 2262 | goto done; |
| 2263 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2264 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2265 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | goto done; |
| 2267 | } |
| 2268 | break; |
| 2269 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2270 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | /* |
| 2272 | * Setting all of a previous oldext extent to newext. |
| 2273 | * Neither the left nor right neighbors are contiguous with |
| 2274 | * the new one. |
| 2275 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2276 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2277 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | if (cur == NULL) |
| 2280 | rval = XFS_ILOG_DEXT; |
| 2281 | else { |
| 2282 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2283 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2284 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2286 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2287 | error = -EFSCORRUPTED; |
| 2288 | goto done; |
| 2289 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2290 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2291 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | goto done; |
| 2293 | } |
| 2294 | break; |
| 2295 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2296 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | /* |
| 2298 | * Setting the first part of a previous oldext extent to newext. |
| 2299 | * The left neighbor is contiguous. |
| 2300 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2301 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2302 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2303 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2304 | PREV.br_startoff += new->br_blockcount; |
| 2305 | PREV.br_startblock += new->br_blockcount; |
| 2306 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2307 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2308 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2309 | xfs_iext_prev(ifp, icur); |
| 2310 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | if (cur == NULL) |
| 2313 | rval = XFS_ILOG_DEXT; |
| 2314 | else { |
| 2315 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2316 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2317 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2319 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2320 | error = -EFSCORRUPTED; |
| 2321 | goto done; |
| 2322 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2323 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2324 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2326 | error = xfs_btree_decrement(cur, 0, &i); |
| 2327 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2329 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2330 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | goto done; |
| 2332 | } |
| 2333 | break; |
| 2334 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2335 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | /* |
| 2337 | * Setting the first part of a previous oldext extent to newext. |
| 2338 | * The left neighbor is not contiguous. |
| 2339 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2340 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2341 | PREV.br_startoff += new->br_blockcount; |
| 2342 | PREV.br_startblock += new->br_blockcount; |
| 2343 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2344 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2345 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2346 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2347 | ifp->if_nextents++; |
| 2348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | if (cur == NULL) |
| 2350 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2351 | else { |
| 2352 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2353 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2354 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2356 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2357 | error = -EFSCORRUPTED; |
| 2358 | goto done; |
| 2359 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2360 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2361 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | goto done; |
| 2363 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2364 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2366 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2367 | error = -EFSCORRUPTED; |
| 2368 | goto done; |
| 2369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | } |
| 2371 | break; |
| 2372 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2373 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | /* |
| 2375 | * Setting the last part of a previous oldext extent to newext. |
| 2376 | * The right neighbor is contiguous with the new allocation. |
| 2377 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2378 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2379 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2380 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2381 | RIGHT.br_startoff = new->br_startoff; |
| 2382 | RIGHT.br_startblock = new->br_startblock; |
| 2383 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2384 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2385 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2386 | xfs_iext_next(ifp, icur); |
| 2387 | xfs_iext_update_extent(ip, state, icur, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | if (cur == NULL) |
| 2390 | rval = XFS_ILOG_DEXT; |
| 2391 | else { |
| 2392 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2393 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2394 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2396 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2397 | error = -EFSCORRUPTED; |
| 2398 | goto done; |
| 2399 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2400 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2401 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2403 | error = xfs_btree_increment(cur, 0, &i); |
| 2404 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2406 | error = xfs_bmbt_update(cur, &RIGHT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2407 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | goto done; |
| 2409 | } |
| 2410 | break; |
| 2411 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2412 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | /* |
| 2414 | * Setting the last part of a previous oldext extent to newext. |
| 2415 | * The right neighbor is not contiguous. |
| 2416 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2417 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2418 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2419 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2420 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2421 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2422 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2423 | ifp->if_nextents++; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | if (cur == NULL) |
| 2426 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2427 | else { |
| 2428 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2429 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2430 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2432 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2433 | error = -EFSCORRUPTED; |
| 2434 | goto done; |
| 2435 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2436 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2437 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2439 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2440 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2442 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 2443 | error = -EFSCORRUPTED; |
| 2444 | goto done; |
| 2445 | } |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2446 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2448 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2449 | error = -EFSCORRUPTED; |
| 2450 | goto done; |
| 2451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | } |
| 2453 | break; |
| 2454 | |
| 2455 | case 0: |
| 2456 | /* |
| 2457 | * Setting the middle part of a previous oldext extent to |
| 2458 | * newext. Contiguity is impossible here. |
| 2459 | * One extent becomes three extents. |
| 2460 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2461 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2462 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | r[0] = *new; |
| 2465 | r[1].br_startoff = new_endoff; |
| 2466 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2467 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2469 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2470 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2471 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2472 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2473 | xfs_iext_insert(ip, icur, &r[1], state); |
| 2474 | xfs_iext_insert(ip, icur, &r[0], state); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2475 | ifp->if_nextents += 2; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | if (cur == NULL) |
| 2478 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2479 | else { |
| 2480 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2481 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2482 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2484 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2485 | error = -EFSCORRUPTED; |
| 2486 | goto done; |
| 2487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2489 | error = xfs_bmbt_update(cur, &r[1]); |
| 2490 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | goto done; |
| 2492 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2494 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2496 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2497 | error = -EFSCORRUPTED; |
| 2498 | goto done; |
| 2499 | } |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2500 | /* |
| 2501 | * Reset the cursor to the position of the new extent |
| 2502 | * we are about to insert as we can't trust it after |
| 2503 | * the previous insert. |
| 2504 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2505 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2506 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2508 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 2509 | error = -EFSCORRUPTED; |
| 2510 | goto done; |
| 2511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2513 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2515 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2516 | error = -EFSCORRUPTED; |
| 2517 | goto done; |
| 2518 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | } |
| 2520 | break; |
| 2521 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2522 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2523 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2524 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2525 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2526 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2527 | case BMAP_LEFT_CONTIG: |
| 2528 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | /* |
| 2530 | * These cases are all impossible. |
| 2531 | */ |
| 2532 | ASSERT(0); |
| 2533 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2534 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2535 | /* update reverse mappings */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 2536 | xfs_rmap_convert_extent(mp, tp, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2537 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2538 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2539 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2540 | int tmp_logflags; /* partial log flag return val */ |
| 2541 | |
| 2542 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 2543 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 2544 | &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2545 | *logflagsp |= tmp_logflags; |
| 2546 | if (error) |
| 2547 | goto done; |
| 2548 | } |
| 2549 | |
| 2550 | /* clear out the allocated field, done with it now in any case. */ |
| 2551 | if (cur) { |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 2552 | cur->bc_ino.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2553 | *curp = cur; |
| 2554 | } |
| 2555 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2556 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2558 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | return error; |
| 2560 | #undef LEFT |
| 2561 | #undef RIGHT |
| 2562 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2566 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2568 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | xfs_bmap_add_extent_hole_delay( |
| 2570 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2571 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2572 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2573 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | { |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 2575 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2577 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2578 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2579 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2580 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2581 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2583 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2584 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | /* |
| 2587 | * Check and set flags if this segment has a left neighbor |
| 2588 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2589 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2590 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2591 | if (isnullstartblock(left.br_startblock)) |
| 2592 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | /* |
| 2596 | * Check and set flags if the current (right) segment exists. |
| 2597 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2598 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2599 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2600 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2601 | if (isnullstartblock(right.br_startblock)) |
| 2602 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | /* |
| 2606 | * Set contiguity flags on the left and right neighbors. |
| 2607 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2608 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2609 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2610 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2611 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2612 | state |= BMAP_LEFT_CONTIG; |
| 2613 | |
| 2614 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2615 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2616 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2617 | (!(state & BMAP_LEFT_CONTIG) || |
| 2618 | (left.br_blockcount + new->br_blockcount + |
| 2619 | right.br_blockcount <= MAXEXTLEN))) |
| 2620 | state |= BMAP_RIGHT_CONTIG; |
| 2621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | /* |
| 2623 | * Switch out based on the contiguity flags. |
| 2624 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2625 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2626 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | /* |
| 2628 | * New allocation is contiguous with delayed allocations |
| 2629 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2630 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | */ |
| 2632 | temp = left.br_blockcount + new->br_blockcount + |
| 2633 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2634 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2635 | oldlen = startblockval(left.br_startblock) + |
| 2636 | startblockval(new->br_startblock) + |
| 2637 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2638 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2639 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2640 | left.br_startblock = nullstartblock(newlen); |
| 2641 | left.br_blockcount = temp; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2642 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2643 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2644 | xfs_iext_prev(ifp, icur); |
| 2645 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | break; |
| 2647 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2648 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | /* |
| 2650 | * New allocation is contiguous with a delayed allocation |
| 2651 | * on the left. |
| 2652 | * Merge the new allocation with the left neighbor. |
| 2653 | */ |
| 2654 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2655 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2656 | oldlen = startblockval(left.br_startblock) + |
| 2657 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2658 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2659 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2660 | left.br_blockcount = temp; |
| 2661 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | 41d196f | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 2662 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2663 | xfs_iext_prev(ifp, icur); |
| 2664 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | break; |
| 2666 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2667 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | /* |
| 2669 | * New allocation is contiguous with a delayed allocation |
| 2670 | * on the right. |
| 2671 | * Merge the new allocation with the right neighbor. |
| 2672 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2674 | oldlen = startblockval(new->br_startblock) + |
| 2675 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2676 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2677 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2678 | right.br_startoff = new->br_startoff; |
| 2679 | right.br_startblock = nullstartblock(newlen); |
| 2680 | right.br_blockcount = temp; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2681 | xfs_iext_update_extent(ip, state, icur, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | break; |
| 2683 | |
| 2684 | case 0: |
| 2685 | /* |
| 2686 | * New allocation is not contiguous with another |
| 2687 | * delayed allocation. |
| 2688 | * Insert a new entry. |
| 2689 | */ |
| 2690 | oldlen = newlen = 0; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2691 | xfs_iext_insert(ip, icur, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | break; |
| 2693 | } |
| 2694 | if (oldlen != newlen) { |
| 2695 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2696 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2697 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | /* |
| 2699 | * Nothing to do for disk quota accounting here. |
| 2700 | */ |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 2701 | xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2706 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | */ |
| 2708 | STATIC int /* error */ |
| 2709 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2710 | struct xfs_trans *tp, |
| 2711 | struct xfs_inode *ip, |
| 2712 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2713 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2714 | struct xfs_btree_cur **curp, |
| 2715 | struct xfs_bmbt_irec *new, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2716 | int *logflagsp, |
| 2717 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2719 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2720 | struct xfs_mount *mp = ip->i_mount; |
| 2721 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2725 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2726 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2727 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2728 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2730 | ASSERT(!isnullstartblock(new->br_startblock)); |
Dave Chinner | 8ef5479 | 2020-03-10 17:54:38 -0700 | [diff] [blame] | 2731 | ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2732 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2733 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | /* |
| 2736 | * Check and set flags if this segment has a left neighbor. |
| 2737 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2738 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2739 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2740 | if (isnullstartblock(left.br_startblock)) |
| 2741 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | /* |
| 2745 | * Check and set flags if this segment has a current value. |
| 2746 | * Not true if we're inserting into the "hole" at eof. |
| 2747 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2748 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2749 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2750 | if (isnullstartblock(right.br_startblock)) |
| 2751 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | /* |
| 2755 | * We're inserting a real allocation between "left" and "right". |
| 2756 | * Set the contiguity flags. Don't let extents get too large. |
| 2757 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2758 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2759 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2760 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2761 | left.br_state == new->br_state && |
| 2762 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2763 | state |= BMAP_LEFT_CONTIG; |
| 2764 | |
| 2765 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2766 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2767 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2768 | new->br_state == right.br_state && |
| 2769 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2770 | (!(state & BMAP_LEFT_CONTIG) || |
| 2771 | left.br_blockcount + new->br_blockcount + |
| 2772 | right.br_blockcount <= MAXEXTLEN)) |
| 2773 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2775 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | /* |
| 2777 | * Select which case we're in here, and implement it. |
| 2778 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2779 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2780 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | /* |
| 2782 | * New allocation is contiguous with real allocations on the |
| 2783 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2784 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2786 | left.br_blockcount += new->br_blockcount + right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2787 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2788 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2789 | xfs_iext_prev(ifp, icur); |
| 2790 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2791 | ifp->if_nextents--; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2792 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2793 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2794 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2795 | } else { |
| 2796 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2797 | error = xfs_bmbt_lookup_eq(cur, &right, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2798 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2799 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2800 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2801 | error = -EFSCORRUPTED; |
| 2802 | goto done; |
| 2803 | } |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2804 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2805 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2806 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2807 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2808 | error = -EFSCORRUPTED; |
| 2809 | goto done; |
| 2810 | } |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2811 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2812 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2813 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2814 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2815 | error = -EFSCORRUPTED; |
| 2816 | goto done; |
| 2817 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2818 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2819 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2820 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2822 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2824 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | /* |
| 2826 | * New allocation is contiguous with a real allocation |
| 2827 | * on the left. |
| 2828 | * Merge the new allocation with the left neighbor. |
| 2829 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2830 | old = left; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2831 | left.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 1d2e008 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2832 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2833 | xfs_iext_prev(ifp, icur); |
| 2834 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2835 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2836 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2837 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2838 | } else { |
| 2839 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2840 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2841 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2842 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2843 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2844 | error = -EFSCORRUPTED; |
| 2845 | goto done; |
| 2846 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2847 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2848 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2849 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2851 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2853 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | /* |
| 2855 | * New allocation is contiguous with a real allocation |
| 2856 | * on the right. |
| 2857 | * Merge the new allocation with the right neighbor. |
| 2858 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2859 | old = right; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2860 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2861 | right.br_startoff = new->br_startoff; |
| 2862 | right.br_startblock = new->br_startblock; |
| 2863 | right.br_blockcount += new->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2864 | xfs_iext_update_extent(ip, state, icur, &right); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2865 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2866 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2867 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2868 | } else { |
| 2869 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2870 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2871 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2872 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2873 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2874 | error = -EFSCORRUPTED; |
| 2875 | goto done; |
| 2876 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2877 | error = xfs_bmbt_update(cur, &right); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2878 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2879 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2881 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | |
| 2883 | case 0: |
| 2884 | /* |
| 2885 | * New allocation is not contiguous with another |
| 2886 | * real allocation. |
| 2887 | * Insert a new entry. |
| 2888 | */ |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2889 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 2890 | ifp->if_nextents++; |
| 2891 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2892 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2893 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2894 | } else { |
| 2895 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2896 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2897 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2898 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2899 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 2900 | error = -EFSCORRUPTED; |
| 2901 | goto done; |
| 2902 | } |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2903 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2904 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2905 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 2906 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 2907 | error = -EFSCORRUPTED; |
| 2908 | goto done; |
| 2909 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2911 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2913 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2914 | /* add reverse mapping unless caller opted out */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 2915 | if (!(flags & XFS_BMAPI_NORMAP)) |
| 2916 | xfs_rmap_map_extent(tp, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2917 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2918 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2919 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2920 | int tmp_logflags; /* partial log flag return val */ |
| 2921 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2922 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 2923 | error = xfs_bmap_extents_to_btree(tp, ip, curp, 0, |
| 2924 | &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2925 | *logflagsp |= tmp_logflags; |
| 2926 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2927 | if (error) |
| 2928 | goto done; |
| 2929 | } |
| 2930 | |
| 2931 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2932 | if (cur) |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 2933 | cur->bc_ino.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2934 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2935 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2936 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2937 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2938 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | } |
| 2940 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2941 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2942 | * Functions used in the extent read, allocate and remove paths |
| 2943 | */ |
| 2944 | |
| 2945 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2946 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2947 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2948 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2949 | xfs_bmap_extsize_align( |
| 2950 | xfs_mount_t *mp, |
| 2951 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2952 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2953 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2954 | int rt, /* is this a realtime inode? */ |
| 2955 | int eof, /* is extent at end-of-file? */ |
| 2956 | int delay, /* creating delalloc extent? */ |
| 2957 | int convert, /* overwriting unwritten extent? */ |
| 2958 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2959 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2960 | { |
| 2961 | xfs_fileoff_t orig_off; /* original offset */ |
| 2962 | xfs_extlen_t orig_alen; /* original length */ |
| 2963 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2964 | xfs_fileoff_t nexto; /* next file offset */ |
| 2965 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2966 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2967 | xfs_extlen_t align_alen; /* temp for length */ |
| 2968 | xfs_extlen_t temp; /* temp for calculations */ |
| 2969 | |
| 2970 | if (convert) |
| 2971 | return 0; |
| 2972 | |
| 2973 | orig_off = align_off = *offp; |
| 2974 | orig_alen = align_alen = *lenp; |
| 2975 | orig_end = orig_off + orig_alen; |
| 2976 | |
| 2977 | /* |
| 2978 | * If this request overlaps an existing extent, then don't |
| 2979 | * attempt to perform any additional alignment. |
| 2980 | */ |
| 2981 | if (!delay && !eof && |
| 2982 | (orig_off >= gotp->br_startoff) && |
| 2983 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2984 | return 0; |
| 2985 | } |
| 2986 | |
| 2987 | /* |
| 2988 | * If the file offset is unaligned vs. the extent size |
| 2989 | * we need to align it. This will be possible unless |
| 2990 | * the file was previously written with a kernel that didn't |
| 2991 | * perform this alignment, or if a truncate shot us in the |
| 2992 | * foot. |
| 2993 | */ |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 2994 | div_u64_rem(orig_off, extsz, &temp); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2995 | if (temp) { |
| 2996 | align_alen += temp; |
| 2997 | align_off -= temp; |
| 2998 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2999 | |
| 3000 | /* Same adjustment for the end of the requested area. */ |
| 3001 | temp = (align_alen % extsz); |
| 3002 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3003 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3004 | |
| 3005 | /* |
| 3006 | * For large extent hint sizes, the aligned extent might be larger than |
| 3007 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 3008 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 3009 | * short allocation just fine, so it is safe to do this. We only want to |
| 3010 | * do it when we are forced to, though, because it means more allocation |
| 3011 | * operations are required. |
| 3012 | */ |
| 3013 | while (align_alen > MAXEXTLEN) |
| 3014 | align_alen -= extsz; |
| 3015 | ASSERT(align_alen <= MAXEXTLEN); |
| 3016 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3017 | /* |
| 3018 | * If the previous block overlaps with this proposed allocation |
| 3019 | * then move the start forward without adjusting the length. |
| 3020 | */ |
| 3021 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3022 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3023 | prevo = prevp->br_startoff; |
| 3024 | else |
| 3025 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3026 | } else |
| 3027 | prevo = 0; |
| 3028 | if (align_off != orig_off && align_off < prevo) |
| 3029 | align_off = prevo; |
| 3030 | /* |
| 3031 | * If the next block overlaps with this proposed allocation |
| 3032 | * then move the start back without adjusting the length, |
| 3033 | * but not before offset 0. |
| 3034 | * This may of course make the start overlap previous block, |
| 3035 | * and if we hit the offset 0 limit then the next block |
| 3036 | * can still overlap too. |
| 3037 | */ |
| 3038 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3039 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3040 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3041 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3042 | else |
| 3043 | nexto = gotp->br_startoff; |
| 3044 | } else |
| 3045 | nexto = NULLFILEOFF; |
| 3046 | if (!eof && |
| 3047 | align_off + align_alen != orig_end && |
| 3048 | align_off + align_alen > nexto) |
| 3049 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3050 | /* |
| 3051 | * If we're now overlapping the next or previous extent that |
| 3052 | * means we can't fit an extsz piece in this hole. Just move |
| 3053 | * the start forward to the first valid spot and set |
| 3054 | * the length so we hit the end. |
| 3055 | */ |
| 3056 | if (align_off != orig_off && align_off < prevo) |
| 3057 | align_off = prevo; |
| 3058 | if (align_off + align_alen != orig_end && |
| 3059 | align_off + align_alen > nexto && |
| 3060 | nexto != NULLFILEOFF) { |
| 3061 | ASSERT(nexto > prevo); |
| 3062 | align_alen = nexto - align_off; |
| 3063 | } |
| 3064 | |
| 3065 | /* |
| 3066 | * If realtime, and the result isn't a multiple of the realtime |
| 3067 | * extent size we need to remove blocks until it is. |
| 3068 | */ |
| 3069 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3070 | /* |
| 3071 | * We're not covering the original request, or |
| 3072 | * we won't be able to once we fix the length. |
| 3073 | */ |
| 3074 | if (orig_off < align_off || |
| 3075 | orig_end > align_off + align_alen || |
| 3076 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3077 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3078 | /* |
| 3079 | * Try to fix it by moving the start up. |
| 3080 | */ |
| 3081 | if (align_off + temp <= orig_off) { |
| 3082 | align_alen -= temp; |
| 3083 | align_off += temp; |
| 3084 | } |
| 3085 | /* |
| 3086 | * Try to fix it by moving the end in. |
| 3087 | */ |
| 3088 | else if (align_off + align_alen - temp >= orig_end) |
| 3089 | align_alen -= temp; |
| 3090 | /* |
| 3091 | * Set the start to the minimum then trim the length. |
| 3092 | */ |
| 3093 | else { |
| 3094 | align_alen -= orig_off - align_off; |
| 3095 | align_off = orig_off; |
| 3096 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3097 | } |
| 3098 | /* |
| 3099 | * Result doesn't cover the request, fail it. |
| 3100 | */ |
| 3101 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3102 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3103 | } else { |
| 3104 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3105 | /* see MAXEXTLEN handling above */ |
| 3106 | ASSERT(orig_end <= align_off + align_alen || |
| 3107 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3108 | } |
| 3109 | |
| 3110 | #ifdef DEBUG |
| 3111 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3112 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3113 | if (prevp->br_startoff != NULLFILEOFF) |
| 3114 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3115 | #endif |
| 3116 | |
| 3117 | *lenp = align_alen; |
| 3118 | *offp = align_off; |
| 3119 | return 0; |
| 3120 | } |
| 3121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3123 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3124 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3125 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3126 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | { |
| 3128 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3130 | xfs_mount_t *mp; /* mount point structure */ |
| 3131 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3132 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | |
| 3134 | #define ISVALID(x,y) \ |
| 3135 | (rt ? \ |
| 3136 | (x) < mp->m_sb.sb_rblocks : \ |
| 3137 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3138 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3139 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | mp = ap->ip->i_mount; |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3142 | nullfb = ap->tp->t_firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3143 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
Christoph Hellwig | c34d570 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 3144 | (ap->datatype & XFS_ALLOC_USERDATA); |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3145 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, |
| 3146 | ap->tp->t_firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | /* |
| 3148 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3149 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3151 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3152 | !isnullstartblock(ap->prev.br_startblock) && |
| 3153 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3154 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3155 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | /* |
| 3157 | * Adjust for the gap between prevp and us. |
| 3158 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3159 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3160 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3162 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3163 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | } |
| 3165 | /* |
| 3166 | * If not at eof, then compare the two neighbor blocks. |
| 3167 | * Figure out whether either one gives us a good starting point, |
| 3168 | * and pick the better one. |
| 3169 | */ |
| 3170 | else if (!ap->eof) { |
| 3171 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3172 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3173 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3174 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3175 | |
| 3176 | /* |
| 3177 | * If there's a previous (left) block, select a requested |
| 3178 | * start block based on it. |
| 3179 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3180 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3181 | !isnullstartblock(ap->prev.br_startblock) && |
| 3182 | (prevbno = ap->prev.br_startblock + |
| 3183 | ap->prev.br_blockcount) && |
| 3184 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | /* |
| 3186 | * Calculate gap to end of previous block. |
| 3187 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3188 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3189 | (ap->prev.br_startoff + |
| 3190 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | /* |
| 3192 | * Figure the startblock based on the previous block's |
| 3193 | * end and the gap size. |
| 3194 | * Heuristic! |
| 3195 | * If the gap is large relative to the piece we're |
| 3196 | * allocating, or using it gives us an invalid block |
| 3197 | * number, then just use the end of the previous block. |
| 3198 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3199 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3201 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | prevbno += adjust; |
| 3203 | else |
| 3204 | prevdiff += adjust; |
| 3205 | /* |
| 3206 | * If the firstblock forbids it, can't use it, |
| 3207 | * must use default. |
| 3208 | */ |
| 3209 | if (!rt && !nullfb && |
| 3210 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3211 | prevbno = NULLFSBLOCK; |
| 3212 | } |
| 3213 | /* |
| 3214 | * No previous block or can't follow it, just default. |
| 3215 | */ |
| 3216 | else |
| 3217 | prevbno = NULLFSBLOCK; |
| 3218 | /* |
| 3219 | * If there's a following (right) block, select a requested |
| 3220 | * start block based on it. |
| 3221 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3222 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | /* |
| 3224 | * Calculate gap to start of next block. |
| 3225 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3226 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | /* |
| 3228 | * Figure the startblock based on the next block's |
| 3229 | * start and the gap size. |
| 3230 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3231 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | /* |
| 3233 | * Heuristic! |
| 3234 | * If the gap is large relative to the piece we're |
| 3235 | * allocating, or using it gives us an invalid block |
| 3236 | * number, then just use the start of the next block |
| 3237 | * offset by our length. |
| 3238 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3239 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3241 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3242 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3243 | gotbno -= ap->length; |
| 3244 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | } else |
| 3246 | gotdiff += adjust; |
| 3247 | /* |
| 3248 | * If the firstblock forbids it, can't use it, |
| 3249 | * must use default. |
| 3250 | */ |
| 3251 | if (!rt && !nullfb && |
| 3252 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3253 | gotbno = NULLFSBLOCK; |
| 3254 | } |
| 3255 | /* |
| 3256 | * No next block, just default. |
| 3257 | */ |
| 3258 | else |
| 3259 | gotbno = NULLFSBLOCK; |
| 3260 | /* |
| 3261 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3262 | * 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] | 3263 | */ |
| 3264 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3265 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3267 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3268 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3269 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3271 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3272 | } |
| 3273 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3274 | static int |
| 3275 | xfs_bmap_longest_free_extent( |
| 3276 | struct xfs_trans *tp, |
| 3277 | xfs_agnumber_t ag, |
| 3278 | xfs_extlen_t *blen, |
| 3279 | int *notinit) |
| 3280 | { |
| 3281 | struct xfs_mount *mp = tp->t_mountp; |
| 3282 | struct xfs_perag *pag; |
| 3283 | xfs_extlen_t longest; |
| 3284 | int error = 0; |
| 3285 | |
| 3286 | pag = xfs_perag_get(mp, ag); |
| 3287 | if (!pag->pagf_init) { |
| 3288 | 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] | 3289 | if (error) { |
| 3290 | /* Couldn't lock the AGF, so skip this AG. */ |
| 3291 | if (error == -EAGAIN) { |
| 3292 | *notinit = 1; |
| 3293 | error = 0; |
| 3294 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3295 | goto out; |
| 3296 | } |
| 3297 | } |
| 3298 | |
Eric Sandeen | a1f6941 | 2018-04-06 10:09:42 -0700 | [diff] [blame] | 3299 | longest = xfs_alloc_longest_free_extent(pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3300 | xfs_alloc_min_freelist(mp, pag), |
| 3301 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3302 | if (*blen < longest) |
| 3303 | *blen = longest; |
| 3304 | |
| 3305 | out: |
| 3306 | xfs_perag_put(pag); |
| 3307 | return error; |
| 3308 | } |
| 3309 | |
| 3310 | static void |
| 3311 | xfs_bmap_select_minlen( |
| 3312 | struct xfs_bmalloca *ap, |
| 3313 | struct xfs_alloc_arg *args, |
| 3314 | xfs_extlen_t *blen, |
| 3315 | int notinit) |
| 3316 | { |
| 3317 | if (notinit || *blen < ap->minlen) { |
| 3318 | /* |
| 3319 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3320 | * there is space for this request. |
| 3321 | */ |
| 3322 | args->minlen = ap->minlen; |
| 3323 | } else if (*blen < args->maxlen) { |
| 3324 | /* |
| 3325 | * If the best seen length is less than the request length, |
| 3326 | * use the best as the minimum. |
| 3327 | */ |
| 3328 | args->minlen = *blen; |
| 3329 | } else { |
| 3330 | /* |
| 3331 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3332 | * as the minimum. |
| 3333 | */ |
| 3334 | args->minlen = args->maxlen; |
| 3335 | } |
| 3336 | } |
| 3337 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3338 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3339 | xfs_bmap_btalloc_nullfb( |
| 3340 | struct xfs_bmalloca *ap, |
| 3341 | struct xfs_alloc_arg *args, |
| 3342 | xfs_extlen_t *blen) |
| 3343 | { |
| 3344 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3345 | xfs_agnumber_t ag, startag; |
| 3346 | int notinit = 0; |
| 3347 | int error; |
| 3348 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3349 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3350 | args->total = ap->total; |
| 3351 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3352 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3353 | if (startag == NULLAGNUMBER) |
| 3354 | startag = ag = 0; |
| 3355 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3356 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3357 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3358 | ¬init); |
| 3359 | if (error) |
| 3360 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3361 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3362 | if (++ag == mp->m_sb.sb_agcount) |
| 3363 | ag = 0; |
| 3364 | if (ag == startag) |
| 3365 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3366 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3367 | |
| 3368 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3369 | return 0; |
| 3370 | } |
| 3371 | |
| 3372 | STATIC int |
| 3373 | xfs_bmap_btalloc_filestreams( |
| 3374 | struct xfs_bmalloca *ap, |
| 3375 | struct xfs_alloc_arg *args, |
| 3376 | xfs_extlen_t *blen) |
| 3377 | { |
| 3378 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3379 | xfs_agnumber_t ag; |
| 3380 | int notinit = 0; |
| 3381 | int error; |
| 3382 | |
| 3383 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3384 | args->total = ap->total; |
| 3385 | |
| 3386 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3387 | if (ag == NULLAGNUMBER) |
| 3388 | ag = 0; |
| 3389 | |
| 3390 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3391 | if (error) |
| 3392 | return error; |
| 3393 | |
| 3394 | if (*blen < args->maxlen) { |
| 3395 | error = xfs_filestream_new_ag(ap, &ag); |
| 3396 | if (error) |
| 3397 | return error; |
| 3398 | |
| 3399 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3400 | ¬init); |
| 3401 | if (error) |
| 3402 | return error; |
| 3403 | |
| 3404 | } |
| 3405 | |
| 3406 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3407 | |
| 3408 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3409 | * Set the failure fallback case to look in the selected AG as stream |
| 3410 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3411 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3412 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3413 | return 0; |
| 3414 | } |
| 3415 | |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3416 | /* Update all inode and quota accounting for the allocation we just did. */ |
| 3417 | static void |
| 3418 | xfs_bmap_btalloc_accounting( |
| 3419 | struct xfs_bmalloca *ap, |
| 3420 | struct xfs_alloc_arg *args) |
| 3421 | { |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3422 | if (ap->flags & XFS_BMAPI_COWFORK) { |
| 3423 | /* |
| 3424 | * COW fork blocks are in-core only and thus are treated as |
| 3425 | * in-core quota reservation (like delalloc blocks) even when |
| 3426 | * converted to real blocks. The quota reservation is not |
| 3427 | * accounted to disk until blocks are remapped to the data |
| 3428 | * fork. So if these blocks were previously delalloc, we |
| 3429 | * already have quota reservation and there's nothing to do |
| 3430 | * yet. |
| 3431 | */ |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3432 | if (ap->wasdel) { |
| 3433 | xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len); |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3434 | return; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3435 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3436 | |
| 3437 | /* |
| 3438 | * Otherwise, we've allocated blocks in a hole. The transaction |
| 3439 | * has acquired in-core quota reservation for this extent. |
| 3440 | * Rather than account these as real blocks, however, we reduce |
| 3441 | * the transaction quota reservation based on the allocation. |
| 3442 | * This essentially transfers the transaction quota reservation |
| 3443 | * to that of a delalloc extent. |
| 3444 | */ |
| 3445 | ap->ip->i_delayed_blks += args->len; |
| 3446 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS, |
| 3447 | -(long)args->len); |
| 3448 | return; |
| 3449 | } |
| 3450 | |
| 3451 | /* data/attr fork only */ |
| 3452 | ap->ip->i_d.di_nblocks += args->len; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3453 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3454 | if (ap->wasdel) { |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3455 | ap->ip->i_delayed_blks -= args->len; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3456 | xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len); |
| 3457 | } |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3458 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 3459 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT, |
| 3460 | args->len); |
| 3461 | } |
| 3462 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3463 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3464 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3465 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3466 | { |
| 3467 | xfs_mount_t *mp; /* mount point structure */ |
| 3468 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3469 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3470 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3471 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3472 | xfs_alloc_arg_t args; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3473 | xfs_fileoff_t orig_offset; |
| 3474 | xfs_extlen_t orig_length; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3475 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3476 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3477 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3478 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3479 | int tryagain; |
| 3480 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3481 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3482 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3483 | ASSERT(ap->length); |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3484 | orig_offset = ap->offset; |
| 3485 | orig_length = ap->length; |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3486 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3487 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3488 | |
| 3489 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3490 | stripe_align = 0; |
| 3491 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3492 | stripe_align = mp->m_swidth; |
| 3493 | else if (mp->m_dalign) |
| 3494 | stripe_align = mp->m_dalign; |
| 3495 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3496 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3497 | align = xfs_get_cowextsz_hint(ap->ip); |
Christoph Hellwig | c34d570 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 3498 | else if (ap->datatype & XFS_ALLOC_USERDATA) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3499 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3500 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3501 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3502 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3503 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3504 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3505 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3506 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3507 | |
| 3508 | |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3509 | nullfb = ap->tp->t_firstblock == NULLFSBLOCK; |
| 3510 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, |
| 3511 | ap->tp->t_firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3512 | if (nullfb) { |
Christoph Hellwig | c34d570 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 3513 | if ((ap->datatype & XFS_ALLOC_USERDATA) && |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3514 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3515 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3516 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3517 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3518 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3519 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3520 | } |
| 3521 | } else |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3522 | ap->blkno = ap->tp->t_firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3523 | |
| 3524 | xfs_bmap_adjacent(ap); |
| 3525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3527 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | * it's in the right allocation group. |
| 3529 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3530 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | ; |
| 3532 | else |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3533 | ap->blkno = ap->tp->t_firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | * Normal allocation, done through xfs_alloc_vextent. |
| 3536 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3537 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3538 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3539 | args.tp = ap->tp; |
| 3540 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3541 | args.fsbno = ap->blkno; |
Darrick J. Wong | 7280fed | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 3542 | args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3543 | |
| 3544 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 9bb54cb | 2018-06-07 07:54:02 -0700 | [diff] [blame] | 3545 | args.maxlen = min(ap->length, mp->m_ag_max_usable); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3546 | blen = 0; |
| 3547 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3548 | /* |
| 3549 | * Search for an allocation group with a single extent large |
| 3550 | * enough for the request. If one isn't found, then adjust |
| 3551 | * the minimum allocation size to the largest space found. |
| 3552 | */ |
Christoph Hellwig | c34d570 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 3553 | if ((ap->datatype & XFS_ALLOC_USERDATA) && |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3554 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3555 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3556 | else |
| 3557 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3558 | if (error) |
| 3559 | return error; |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3560 | } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3561 | if (xfs_inode_is_filestream(ap->ip)) |
| 3562 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3563 | else |
| 3564 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3565 | args.total = args.minlen = ap->minlen; |
| 3566 | } else { |
| 3567 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3568 | args.total = ap->total; |
| 3569 | args.minlen = ap->minlen; |
| 3570 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3571 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3572 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3573 | args.prod = align; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3574 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3575 | if (args.mod) |
| 3576 | args.mod = args.prod - args.mod; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3577 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3578 | args.prod = 1; |
| 3579 | args.mod = 0; |
| 3580 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3581 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3582 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3583 | if (args.mod) |
| 3584 | args.mod = args.prod - args.mod; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3585 | } |
| 3586 | /* |
Dave Chinner | 1c74357 | 2019-10-21 09:26:34 -0700 | [diff] [blame] | 3587 | * If we are not low on available data blocks, and the underlying |
| 3588 | * logical volume manager is a stripe, and the file offset is zero then |
| 3589 | * try to allocate data blocks on stripe unit boundary. NOTE: ap->aeof |
| 3590 | * is only set if the allocation length is >= the stripe unit and the |
| 3591 | * allocation offset is at the end of file. |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3592 | */ |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3593 | if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3594 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3595 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3596 | atype = args.type; |
| 3597 | isaligned = 1; |
| 3598 | /* |
Dave Chinner | 1c74357 | 2019-10-21 09:26:34 -0700 | [diff] [blame] | 3599 | * Adjust minlen to try and preserve alignment if we |
| 3600 | * can't guarantee an aligned maxlen extent. |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3601 | */ |
Dave Chinner | 1c74357 | 2019-10-21 09:26:34 -0700 | [diff] [blame] | 3602 | if (blen > args.alignment && |
| 3603 | blen <= args.maxlen + args.alignment) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3604 | args.minlen = blen - args.alignment; |
| 3605 | args.minalignslop = 0; |
| 3606 | } else { |
| 3607 | /* |
| 3608 | * First try an exact bno allocation. |
| 3609 | * If it fails then do a near or start bno |
| 3610 | * allocation with alignment turned on. |
| 3611 | */ |
| 3612 | atype = args.type; |
| 3613 | tryagain = 1; |
| 3614 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3615 | args.alignment = 1; |
| 3616 | /* |
| 3617 | * Compute the minlen+alignment for the |
| 3618 | * next case. Set slop so that the value |
| 3619 | * of minlen+alignment+slop doesn't go up |
| 3620 | * between the calls. |
| 3621 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3622 | if (blen > stripe_align && blen <= args.maxlen) |
| 3623 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3624 | else |
| 3625 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3626 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3627 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3628 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3629 | args.minlen - 1; |
| 3630 | else |
| 3631 | args.minalignslop = 0; |
| 3632 | } |
| 3633 | } else { |
| 3634 | args.alignment = 1; |
| 3635 | args.minalignslop = 0; |
| 3636 | } |
| 3637 | args.minleft = ap->minleft; |
| 3638 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3639 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3640 | args.datatype = ap->datatype; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3641 | |
| 3642 | error = xfs_alloc_vextent(&args); |
| 3643 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3644 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3645 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3646 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3647 | /* |
| 3648 | * Exact allocation failed. Now try with alignment |
| 3649 | * turned on. |
| 3650 | */ |
| 3651 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3652 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3653 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3654 | args.minlen = nextminlen; |
| 3655 | args.minalignslop = 0; |
| 3656 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | if ((error = xfs_alloc_vextent(&args))) |
| 3658 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3659 | } |
| 3660 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3661 | /* |
| 3662 | * allocation failed, so turn off alignment and |
| 3663 | * try again. |
| 3664 | */ |
| 3665 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3666 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3667 | args.alignment = 0; |
| 3668 | if ((error = xfs_alloc_vextent(&args))) |
| 3669 | return error; |
| 3670 | } |
| 3671 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3672 | args.minlen > ap->minlen) { |
| 3673 | args.minlen = ap->minlen; |
| 3674 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3675 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3676 | if ((error = xfs_alloc_vextent(&args))) |
| 3677 | return error; |
| 3678 | } |
| 3679 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3680 | args.fsbno = 0; |
| 3681 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3682 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3683 | if ((error = xfs_alloc_vextent(&args))) |
| 3684 | return error; |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3685 | ap->tp->t_flags |= XFS_TRANS_LOWMODE; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3686 | } |
| 3687 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3688 | /* |
| 3689 | * check the allocation happened at the same or higher AG than |
| 3690 | * the first block that was allocated. |
| 3691 | */ |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3692 | ASSERT(ap->tp->t_firstblock == NULLFSBLOCK || |
| 3693 | XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <= |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3694 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3695 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3696 | ap->blkno = args.fsbno; |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3697 | if (ap->tp->t_firstblock == NULLFSBLOCK) |
| 3698 | ap->tp->t_firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3699 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3700 | ap->length = args.len; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3701 | /* |
| 3702 | * If the extent size hint is active, we tried to round the |
| 3703 | * caller's allocation request offset down to extsz and the |
| 3704 | * length up to another extsz boundary. If we found a free |
| 3705 | * extent we mapped it in starting at this new offset. If the |
| 3706 | * newly mapped space isn't long enough to cover any of the |
| 3707 | * range of offsets that was originally requested, move the |
| 3708 | * mapping up so that we can fill as much of the caller's |
| 3709 | * original request as possible. Free space is apparently |
| 3710 | * very fragmented so we're unlikely to be able to satisfy the |
| 3711 | * hints anyway. |
| 3712 | */ |
| 3713 | if (ap->length <= orig_length) |
| 3714 | ap->offset = orig_offset; |
| 3715 | else if (ap->offset + ap->length < orig_offset + orig_length) |
| 3716 | ap->offset = orig_offset + orig_length - ap->length; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3717 | xfs_bmap_btalloc_accounting(ap, &args); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3718 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3719 | ap->blkno = NULLFSBLOCK; |
| 3720 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | } |
| 3722 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3723 | } |
| 3724 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3725 | /* Trim extent to fit a logical block range. */ |
| 3726 | void |
| 3727 | xfs_trim_extent( |
| 3728 | struct xfs_bmbt_irec *irec, |
| 3729 | xfs_fileoff_t bno, |
| 3730 | xfs_filblks_t len) |
| 3731 | { |
| 3732 | xfs_fileoff_t distance; |
| 3733 | xfs_fileoff_t end = bno + len; |
| 3734 | |
| 3735 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3736 | irec->br_startoff >= end) { |
| 3737 | irec->br_blockcount = 0; |
| 3738 | return; |
| 3739 | } |
| 3740 | |
| 3741 | if (irec->br_startoff < bno) { |
| 3742 | distance = bno - irec->br_startoff; |
| 3743 | if (isnullstartblock(irec->br_startblock)) |
| 3744 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3745 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3746 | irec->br_startblock != HOLESTARTBLOCK) |
| 3747 | irec->br_startblock += distance; |
| 3748 | irec->br_startoff += distance; |
| 3749 | irec->br_blockcount -= distance; |
| 3750 | } |
| 3751 | |
| 3752 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3753 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3754 | irec->br_blockcount -= distance; |
| 3755 | } |
| 3756 | } |
| 3757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3759 | * Trim the returned map to the required bounds |
| 3760 | */ |
| 3761 | STATIC void |
| 3762 | xfs_bmapi_trim_map( |
| 3763 | struct xfs_bmbt_irec *mval, |
| 3764 | struct xfs_bmbt_irec *got, |
| 3765 | xfs_fileoff_t *bno, |
| 3766 | xfs_filblks_t len, |
| 3767 | xfs_fileoff_t obno, |
| 3768 | xfs_fileoff_t end, |
| 3769 | int n, |
| 3770 | int flags) |
| 3771 | { |
| 3772 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3773 | got->br_startoff + got->br_blockcount <= obno) { |
| 3774 | *mval = *got; |
| 3775 | if (isnullstartblock(got->br_startblock)) |
| 3776 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3777 | return; |
| 3778 | } |
| 3779 | |
| 3780 | if (obno > *bno) |
| 3781 | *bno = obno; |
| 3782 | ASSERT((*bno >= obno) || (n == 0)); |
| 3783 | ASSERT(*bno < end); |
| 3784 | mval->br_startoff = *bno; |
| 3785 | if (isnullstartblock(got->br_startblock)) |
| 3786 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3787 | else |
| 3788 | mval->br_startblock = got->br_startblock + |
| 3789 | (*bno - got->br_startoff); |
| 3790 | /* |
| 3791 | * Return the minimum of what we got and what we asked for for |
| 3792 | * the length. We can use the len variable here because it is |
| 3793 | * modified below and we could have been there before coming |
| 3794 | * here if the first part of the allocation didn't overlap what |
| 3795 | * was asked for. |
| 3796 | */ |
| 3797 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3798 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3799 | mval->br_state = got->br_state; |
| 3800 | ASSERT(mval->br_blockcount <= len); |
| 3801 | return; |
| 3802 | } |
| 3803 | |
| 3804 | /* |
| 3805 | * Update and validate the extent map to return |
| 3806 | */ |
| 3807 | STATIC void |
| 3808 | xfs_bmapi_update_map( |
| 3809 | struct xfs_bmbt_irec **map, |
| 3810 | xfs_fileoff_t *bno, |
| 3811 | xfs_filblks_t *len, |
| 3812 | xfs_fileoff_t obno, |
| 3813 | xfs_fileoff_t end, |
| 3814 | int *n, |
| 3815 | int flags) |
| 3816 | { |
| 3817 | xfs_bmbt_irec_t *mval = *map; |
| 3818 | |
| 3819 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3820 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3821 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3822 | (mval->br_startoff < obno)); |
| 3823 | |
| 3824 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3825 | *len = end - *bno; |
| 3826 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3827 | /* update previous map with new information */ |
| 3828 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3829 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3830 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3831 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3832 | mval[-1].br_state = mval->br_state; |
| 3833 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3834 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3835 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3836 | mval->br_startblock == mval[-1].br_startblock + |
| 3837 | mval[-1].br_blockcount && |
Christoph Hellwig | c3a2f9f | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 3838 | mval[-1].br_state == mval->br_state) { |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3839 | ASSERT(mval->br_startoff == |
| 3840 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3841 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3842 | } else if (*n > 0 && |
| 3843 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3844 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3845 | mval->br_startoff == |
| 3846 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3847 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3848 | mval[-1].br_state = mval->br_state; |
| 3849 | } else if (!((*n == 0) && |
| 3850 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3851 | obno))) { |
| 3852 | mval++; |
| 3853 | (*n)++; |
| 3854 | } |
| 3855 | *map = mval; |
| 3856 | } |
| 3857 | |
| 3858 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3859 | * Map file blocks to filesystem blocks without allocation. |
| 3860 | */ |
| 3861 | int |
| 3862 | xfs_bmapi_read( |
| 3863 | struct xfs_inode *ip, |
| 3864 | xfs_fileoff_t bno, |
| 3865 | xfs_filblks_t len, |
| 3866 | struct xfs_bmbt_irec *mval, |
| 3867 | int *nmap, |
| 3868 | int flags) |
| 3869 | { |
| 3870 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 4b516ff | 2020-05-14 14:06:41 -0700 | [diff] [blame] | 3871 | int whichfork = xfs_bmapi_whichfork(flags); |
| 3872 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3873 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3874 | xfs_fileoff_t obno; |
| 3875 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3876 | struct xfs_iext_cursor icur; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3877 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3878 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3879 | int n = 0; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3880 | |
| 3881 | ASSERT(*nmap >= 1); |
Christoph Hellwig | 1a1c57b | 2020-05-14 14:06:40 -0700 | [diff] [blame] | 3882 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3883 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3884 | |
Christoph Hellwig | 4b516ff | 2020-05-14 14:06:41 -0700 | [diff] [blame] | 3885 | if (WARN_ON_ONCE(!ifp)) |
| 3886 | return -EFSCORRUPTED; |
| 3887 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 3888 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
| 3889 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3890 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3891 | |
| 3892 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3893 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3894 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3895 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3896 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3897 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3898 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3899 | if (error) |
| 3900 | return error; |
| 3901 | } |
| 3902 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3903 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3904 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3905 | end = bno + len; |
| 3906 | obno = bno; |
| 3907 | |
| 3908 | while (bno < end && n < *nmap) { |
| 3909 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3910 | if (eof) |
| 3911 | got.br_startoff = end; |
| 3912 | if (got.br_startoff > bno) { |
| 3913 | /* Reading in a hole. */ |
| 3914 | mval->br_startoff = bno; |
| 3915 | mval->br_startblock = HOLESTARTBLOCK; |
| 3916 | mval->br_blockcount = |
| 3917 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3918 | mval->br_state = XFS_EXT_NORM; |
| 3919 | bno += mval->br_blockcount; |
| 3920 | len -= mval->br_blockcount; |
| 3921 | mval++; |
| 3922 | n++; |
| 3923 | continue; |
| 3924 | } |
| 3925 | |
| 3926 | /* set up the extent map to return. */ |
| 3927 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3928 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3929 | |
| 3930 | /* If we're done, stop now. */ |
| 3931 | if (bno >= end || n >= *nmap) |
| 3932 | break; |
| 3933 | |
| 3934 | /* Else go on to the next record. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3935 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3936 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3937 | } |
| 3938 | *nmap = n; |
| 3939 | return 0; |
| 3940 | } |
| 3941 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3942 | /* |
| 3943 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3944 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3945 | * |
| 3946 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3947 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3948 | * to the extent record that was inserted to the inode fork. |
| 3949 | * |
| 3950 | * Note that the allocated extent may have been merged with contiguous extents |
| 3951 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3952 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3953 | * look up the updated record in the inode fork. |
| 3954 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3955 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3956 | xfs_bmapi_reserve_delalloc( |
| 3957 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3958 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3959 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3960 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3961 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3962 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3963 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3964 | int eof) |
| 3965 | { |
| 3966 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3967 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3968 | xfs_extlen_t alen; |
| 3969 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3970 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3971 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3972 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3973 | /* |
| 3974 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3975 | * tag the inode before we return. |
| 3976 | */ |
| 3977 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3978 | if (!eof) |
| 3979 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3980 | if (prealloc && alen >= len) |
| 3981 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3982 | |
| 3983 | /* Figure out the extent size, adjust alen */ |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3984 | if (whichfork == XFS_COW_FORK) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3985 | struct xfs_bmbt_irec prev; |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3986 | xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3987 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3988 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3989 | prev.br_startoff = NULLFILEOFF; |
| 3990 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3991 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3992 | 1, 0, &aoff, &alen); |
| 3993 | ASSERT(!error); |
| 3994 | } |
| 3995 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3996 | /* |
| 3997 | * Make a transaction-less quota reservation for delayed allocation |
| 3998 | * blocks. This number gets adjusted later. We return if we haven't |
| 3999 | * allocated blocks already inside this loop. |
| 4000 | */ |
| 4001 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4002 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4003 | if (error) |
| 4004 | return error; |
| 4005 | |
| 4006 | /* |
| 4007 | * Split changing sb for alen and indlen since they could be coming |
| 4008 | * from different places. |
| 4009 | */ |
| 4010 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4011 | ASSERT(indlen > 0); |
| 4012 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4013 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4014 | if (error) |
| 4015 | goto out_unreserve_quota; |
| 4016 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4017 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4018 | if (error) |
| 4019 | goto out_unreserve_blocks; |
| 4020 | |
| 4021 | |
| 4022 | ip->i_delayed_blks += alen; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 4023 | xfs_mod_delalloc(ip->i_mount, alen + indlen); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4024 | |
| 4025 | got->br_startoff = aoff; |
| 4026 | got->br_startblock = nullstartblock(indlen); |
| 4027 | got->br_blockcount = alen; |
| 4028 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4029 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4030 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4031 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4032 | /* |
| 4033 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 4034 | * preallocation can occur at the start or end of the extent, even when |
| 4035 | * prealloc == 0, so we must also check the aligned offset and length. |
| 4036 | */ |
| 4037 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4038 | xfs_inode_set_eofblocks_tag(ip); |
| 4039 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4040 | xfs_inode_set_cowblocks_tag(ip); |
| 4041 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4042 | return 0; |
| 4043 | |
| 4044 | out_unreserve_blocks: |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4045 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4046 | out_unreserve_quota: |
| 4047 | if (XFS_IS_QUOTA_ON(mp)) |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4048 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4049 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4050 | return error; |
| 4051 | } |
| 4052 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4053 | static int |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4054 | xfs_bmap_alloc_userdata( |
| 4055 | struct xfs_bmalloca *bma) |
| 4056 | { |
| 4057 | struct xfs_mount *mp = bma->ip->i_mount; |
| 4058 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
| 4059 | int error; |
| 4060 | |
| 4061 | /* |
| 4062 | * Set the data type being allocated. For the data fork, the first data |
| 4063 | * in the file is treated differently to all other allocations. For the |
| 4064 | * attribute fork, we only need to ensure the allocated range is not on |
| 4065 | * the busy list. |
| 4066 | */ |
| 4067 | bma->datatype = XFS_ALLOC_NOBUSY; |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4068 | if (whichfork == XFS_DATA_FORK) { |
Christoph Hellwig | c34d570 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 4069 | bma->datatype |= XFS_ALLOC_USERDATA; |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4070 | if (bma->offset == 0) |
| 4071 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4072 | |
| 4073 | if (mp->m_dalign && bma->length >= mp->m_dalign) { |
| 4074 | error = xfs_bmap_isaeof(bma, whichfork); |
| 4075 | if (error) |
| 4076 | return error; |
| 4077 | } |
| 4078 | |
| 4079 | if (XFS_IS_REALTIME_INODE(bma->ip)) |
| 4080 | return xfs_bmap_rtalloc(bma); |
| 4081 | } |
| 4082 | |
| 4083 | return xfs_bmap_btalloc(bma); |
| 4084 | } |
| 4085 | |
| 4086 | static int |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4087 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4088 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4089 | { |
| 4090 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4091 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4092 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4093 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4094 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4095 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4096 | ASSERT(bma->length > 0); |
| 4097 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4098 | /* |
| 4099 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4100 | * for in this bmap call but that wouldn't be as good. |
| 4101 | */ |
| 4102 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4103 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4104 | bma->offset = bma->got.br_startoff; |
Darrick J. Wong | f5be084 | 2019-11-06 08:53:54 -0800 | [diff] [blame] | 4105 | if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev)) |
| 4106 | bma->prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4107 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4108 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4109 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4110 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4111 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4114 | if (bma->flags & XFS_BMAPI_CONTIG) |
| 4115 | bma->minlen = bma->length; |
| 4116 | else |
| 4117 | bma->minlen = 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4118 | |
Christoph Hellwig | be6cacb | 2019-10-30 12:24:59 -0700 | [diff] [blame] | 4119 | if (bma->flags & XFS_BMAPI_METADATA) |
| 4120 | error = xfs_bmap_btalloc(bma); |
| 4121 | else |
| 4122 | error = xfs_bmap_alloc_userdata(bma); |
| 4123 | if (error || bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4124 | return error; |
| 4125 | |
Christoph Hellwig | fd638f1 | 2019-10-30 12:25:00 -0700 | [diff] [blame] | 4126 | if (bma->flags & XFS_BMAPI_ZERO) { |
| 4127 | error = xfs_zero_extent(bma->ip, bma->blkno, bma->length); |
| 4128 | if (error) |
| 4129 | return error; |
| 4130 | } |
| 4131 | |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 4132 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4133 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4134 | /* |
| 4135 | * Bump the number of extents we've allocated |
| 4136 | * in this call. |
| 4137 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4138 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4139 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4140 | if (bma->cur) |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 4141 | bma->cur->bc_ino.flags = |
Dave Chinner | 8ef5479 | 2020-03-10 17:54:38 -0700 | [diff] [blame] | 4142 | bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4143 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4144 | bma->got.br_startoff = bma->offset; |
| 4145 | bma->got.br_startblock = bma->blkno; |
| 4146 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4147 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4148 | |
Darrick J. Wong | a5949d3 | 2020-05-23 09:43:31 -0700 | [diff] [blame] | 4149 | if (bma->flags & XFS_BMAPI_PREALLOC) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4150 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4151 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4152 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4153 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4154 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4155 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4156 | whichfork, &bma->icur, &bma->cur, &bma->got, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4157 | &bma->logflags, bma->flags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4158 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4159 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4160 | if (error) |
| 4161 | return error; |
| 4162 | |
| 4163 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4164 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4165 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4166 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4167 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4168 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4169 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4170 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4171 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4172 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4173 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4174 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4175 | return 0; |
| 4176 | } |
| 4177 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4178 | STATIC int |
| 4179 | xfs_bmapi_convert_unwritten( |
| 4180 | struct xfs_bmalloca *bma, |
| 4181 | struct xfs_bmbt_irec *mval, |
| 4182 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4183 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4184 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4185 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4186 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4187 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4188 | int error; |
| 4189 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4190 | /* check if we need to do unwritten->real conversion */ |
| 4191 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4192 | (flags & XFS_BMAPI_PREALLOC)) |
| 4193 | return 0; |
| 4194 | |
| 4195 | /* check if we need to do real->unwritten conversion */ |
| 4196 | if (mval->br_state == XFS_EXT_NORM && |
| 4197 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4198 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4199 | return 0; |
| 4200 | |
| 4201 | /* |
| 4202 | * Modify (by adding) the state flag, if writing. |
| 4203 | */ |
| 4204 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4205 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4206 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4207 | bma->ip, whichfork); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4208 | } |
| 4209 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4210 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4211 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4212 | /* |
| 4213 | * Before insertion into the bmbt, zero the range being converted |
| 4214 | * if required. |
| 4215 | */ |
| 4216 | if (flags & XFS_BMAPI_ZERO) { |
| 4217 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4218 | mval->br_blockcount); |
| 4219 | if (error) |
| 4220 | return error; |
| 4221 | } |
| 4222 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4223 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4224 | &bma->icur, &bma->cur, mval, &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4225 | /* |
| 4226 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4227 | * path because the conversion might not have done so (e.g., if the |
| 4228 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4229 | * in the transaction for the sake of fsync(), even if nothing has |
| 4230 | * changed, because fsync() will not force the log for this transaction |
| 4231 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4232 | * |
| 4233 | * Note: If we're only converting cow fork extents, there aren't |
| 4234 | * 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] | 4235 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4236 | if (whichfork != XFS_COW_FORK) |
| 4237 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4238 | if (error) |
| 4239 | return error; |
| 4240 | |
| 4241 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4242 | * Update our extent pointer, given that |
| 4243 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4244 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4245 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4246 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4247 | |
| 4248 | /* |
| 4249 | * We may have combined previously unwritten space with written space, |
| 4250 | * so generate another request. |
| 4251 | */ |
| 4252 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4253 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4254 | return 0; |
| 4255 | } |
| 4256 | |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4257 | static inline xfs_extlen_t |
| 4258 | xfs_bmapi_minleft( |
| 4259 | struct xfs_trans *tp, |
| 4260 | struct xfs_inode *ip, |
| 4261 | int fork) |
| 4262 | { |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4263 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, fork); |
| 4264 | |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4265 | if (tp && tp->t_firstblock != NULLFSBLOCK) |
| 4266 | return 0; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4267 | if (ifp->if_format != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4268 | return 1; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4269 | return be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4270 | } |
| 4271 | |
| 4272 | /* |
| 4273 | * Log whatever the flags say, even if error. Otherwise we might miss detecting |
| 4274 | * a case where the data is changed, there's an error, and it's not logged so we |
| 4275 | * don't shutdown when we should. Don't bother logging extents/btree changes if |
| 4276 | * we converted to the other format. |
| 4277 | */ |
| 4278 | static void |
| 4279 | xfs_bmapi_finish( |
| 4280 | struct xfs_bmalloca *bma, |
| 4281 | int whichfork, |
| 4282 | int error) |
| 4283 | { |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4284 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
| 4285 | |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4286 | if ((bma->logflags & xfs_ilog_fext(whichfork)) && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4287 | ifp->if_format != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4288 | bma->logflags &= ~xfs_ilog_fext(whichfork); |
| 4289 | else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4290 | ifp->if_format != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4291 | bma->logflags &= ~xfs_ilog_fbroot(whichfork); |
| 4292 | |
| 4293 | if (bma->logflags) |
| 4294 | xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags); |
| 4295 | if (bma->cur) |
| 4296 | xfs_btree_del_cursor(bma->cur, error); |
| 4297 | } |
| 4298 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4299 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4300 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4301 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4302 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4303 | * locking problems. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4305 | int |
| 4306 | xfs_bmapi_write( |
| 4307 | struct xfs_trans *tp, /* transaction pointer */ |
| 4308 | struct xfs_inode *ip, /* incore inode */ |
| 4309 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4310 | xfs_filblks_t len, /* length to map in file */ |
| 4311 | int flags, /* XFS_BMAPI_... */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4312 | xfs_extlen_t total, /* total blocks needed */ |
| 4313 | struct xfs_bmbt_irec *mval, /* output: map values */ |
Brian Foster | 6e702a5 | 2018-07-11 22:26:12 -0700 | [diff] [blame] | 4314 | int *nmap) /* i/o: mval size/count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | { |
Darrick J. Wong | 4b0bce3 | 2019-03-19 08:16:22 -0700 | [diff] [blame] | 4316 | struct xfs_bmalloca bma = { |
| 4317 | .tp = tp, |
| 4318 | .ip = ip, |
| 4319 | .total = total, |
| 4320 | }; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4321 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4322 | int whichfork = xfs_bmapi_whichfork(flags); |
| 4323 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4324 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4325 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4326 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4327 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4328 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4331 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4332 | int orig_flags; /* original flags arg value */ |
| 4333 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4334 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4335 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4336 | |
| 4337 | orig_bno = bno; |
| 4338 | orig_len = len; |
| 4339 | orig_flags = flags; |
| 4340 | orig_mval = mval; |
| 4341 | orig_nmap = *nmap; |
| 4342 | #endif |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4345 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 4346 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4347 | ASSERT(len > 0); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4348 | ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4349 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4350 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4351 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4352 | /* zeroing is for currently only for data extents, not metadata */ |
| 4353 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4354 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4355 | /* |
| 4356 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4357 | * but it makes no sense to do both at once. This would result in |
| 4358 | * zeroing the unwritten extent twice, but it still being an |
| 4359 | * unwritten extent.... |
| 4360 | */ |
| 4361 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4362 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4363 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4364 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 4365 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4366 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4370 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4371 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4372 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4373 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4374 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4375 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4376 | if (error) |
| 4377 | goto error0; |
| 4378 | } |
| 4379 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4380 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4381 | eof = true; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4382 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4383 | bma.prev.br_startoff = NULLFILEOFF; |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4384 | bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4385 | |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4386 | n = 0; |
| 4387 | end = bno + len; |
| 4388 | obno = bno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4390 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4391 | |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4392 | /* in hole or beyond EOF? */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4393 | if (eof || bma.got.br_startoff > bno) { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4394 | /* |
| 4395 | * CoW fork conversions should /never/ hit EOF or |
| 4396 | * holes. There should always be something for us |
| 4397 | * to work on. |
| 4398 | */ |
| 4399 | ASSERT(!((flags & XFS_BMAPI_CONVERT) && |
| 4400 | (flags & XFS_BMAPI_COWFORK))); |
| 4401 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4402 | need_alloc = true; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4403 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4404 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4405 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4406 | |
| 4407 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | * First, deal with the hole before the allocated space |
| 4409 | * that we found, if any. |
| 4410 | */ |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 4411 | if (need_alloc || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4412 | bma.eof = eof; |
| 4413 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4414 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4415 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4416 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4417 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4418 | /* |
| 4419 | * There's a 32/64 bit type mismatch between the |
| 4420 | * allocation length request (which can be 64 bits in |
| 4421 | * length) and the bma length request, which is |
| 4422 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4423 | * check for 32-bit overflows and handle them here. |
| 4424 | */ |
| 4425 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4426 | bma.length = MAXEXTLEN; |
| 4427 | else |
| 4428 | bma.length = len; |
| 4429 | |
| 4430 | ASSERT(len > 0); |
| 4431 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4432 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | if (error) |
| 4434 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4435 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4436 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4437 | |
| 4438 | /* |
| 4439 | * If this is a CoW allocation, record the data in |
| 4440 | * the refcount btree for orphan recovery. |
| 4441 | */ |
Darrick J. Wong | 74b4c5d | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 4442 | if (whichfork == XFS_COW_FORK) |
| 4443 | xfs_refcount_alloc_cow_extent(tp, bma.blkno, |
| 4444 | bma.length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4446 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4447 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4448 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4449 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4451 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4452 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4453 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4454 | continue; |
| 4455 | if (error) |
| 4456 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4458 | /* update the extent map to return */ |
| 4459 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | /* |
| 4462 | * If we're done, stop now. Stop when we've allocated |
| 4463 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4464 | * the transaction may get too big. |
| 4465 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4466 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4468 | |
| 4469 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4470 | bma.prev = bma.got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4471 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4472 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4475 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 4476 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, |
| 4477 | whichfork); |
| 4478 | if (error) |
| 4479 | goto error0; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4480 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4481 | ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 4482 | ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork)); |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4483 | xfs_bmapi_finish(&bma, whichfork, 0); |
| 4484 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4485 | orig_nmap, *nmap); |
| 4486 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | error0: |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4488 | xfs_bmapi_finish(&bma, whichfork, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | return error; |
| 4490 | } |
| 4491 | |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4492 | /* |
| 4493 | * Convert an existing delalloc extent to real blocks based on file offset. This |
| 4494 | * attempts to allocate the entire delalloc extent and may require multiple |
| 4495 | * invocations to allocate the target offset if a large enough physical extent |
| 4496 | * is not available. |
| 4497 | */ |
| 4498 | int |
| 4499 | xfs_bmapi_convert_delalloc( |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4500 | struct xfs_inode *ip, |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4501 | int whichfork, |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 4502 | xfs_off_t offset, |
| 4503 | struct iomap *iomap, |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4504 | unsigned int *seq) |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4505 | { |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4506 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4507 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 4508 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4509 | struct xfs_bmalloca bma = { NULL }; |
Darrick J. Wong | af952ae | 2019-12-16 11:14:09 -0800 | [diff] [blame] | 4510 | uint16_t flags = 0; |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4511 | struct xfs_trans *tp; |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4512 | int error; |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4513 | |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 4514 | if (whichfork == XFS_COW_FORK) |
| 4515 | flags |= IOMAP_F_SHARED; |
| 4516 | |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4517 | /* |
| 4518 | * Space for the extent and indirect blocks was reserved when the |
| 4519 | * delalloc extent was created so there's no need to do so here. |
| 4520 | */ |
| 4521 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, |
| 4522 | XFS_TRANS_RESERVE, &tp); |
| 4523 | if (error) |
| 4524 | return error; |
| 4525 | |
| 4526 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 4527 | xfs_trans_ijoin(tp, ip, 0); |
| 4528 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4529 | if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) || |
| 4530 | bma.got.br_startoff > offset_fsb) { |
| 4531 | /* |
| 4532 | * No extent found in the range we are trying to convert. This |
| 4533 | * should only happen for the COW fork, where another thread |
| 4534 | * might have moved the extent to the data fork in the meantime. |
| 4535 | */ |
| 4536 | WARN_ON_ONCE(whichfork != XFS_COW_FORK); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4537 | error = -EAGAIN; |
| 4538 | goto out_trans_cancel; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4539 | } |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4540 | |
| 4541 | /* |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4542 | * If we find a real extent here we raced with another thread converting |
| 4543 | * the extent. Just return the real extent at this offset. |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4544 | */ |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4545 | if (!isnullstartblock(bma.got.br_startblock)) { |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 4546 | xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4547 | *seq = READ_ONCE(ifp->if_seq); |
| 4548 | goto out_trans_cancel; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | bma.tp = tp; |
| 4552 | bma.ip = ip; |
| 4553 | bma.wasdel = true; |
| 4554 | bma.offset = bma.got.br_startoff; |
| 4555 | bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4556 | bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); |
Darrick J. Wong | a5949d3 | 2020-05-23 09:43:31 -0700 | [diff] [blame] | 4557 | |
| 4558 | /* |
| 4559 | * When we're converting the delalloc reservations backing dirty pages |
| 4560 | * in the page cache, we must be careful about how we create the new |
| 4561 | * extents: |
| 4562 | * |
| 4563 | * New CoW fork extents are created unwritten, turned into real extents |
| 4564 | * when we're about to write the data to disk, and mapped into the data |
| 4565 | * fork after the write finishes. End of story. |
| 4566 | * |
| 4567 | * New data fork extents must be mapped in as unwritten and converted |
| 4568 | * to real extents after the write succeeds to avoid exposing stale |
| 4569 | * disk contents if we crash. |
| 4570 | */ |
| 4571 | bma.flags = XFS_BMAPI_PREALLOC; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4572 | if (whichfork == XFS_COW_FORK) |
Darrick J. Wong | a5949d3 | 2020-05-23 09:43:31 -0700 | [diff] [blame] | 4573 | bma.flags |= XFS_BMAPI_COWFORK; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4574 | |
| 4575 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
| 4576 | bma.prev.br_startoff = NULLFILEOFF; |
| 4577 | |
| 4578 | error = xfs_bmapi_allocate(&bma); |
| 4579 | if (error) |
| 4580 | goto out_finish; |
| 4581 | |
| 4582 | error = -ENOSPC; |
| 4583 | if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK)) |
| 4584 | goto out_finish; |
| 4585 | error = -EFSCORRUPTED; |
Christoph Hellwig | eb77b23 | 2019-09-03 08:13:13 -0700 | [diff] [blame] | 4586 | if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock))) |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4587 | goto out_finish; |
| 4588 | |
Christoph Hellwig | 125851a | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4589 | XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length)); |
| 4590 | XFS_STATS_INC(mp, xs_xstrat_quick); |
| 4591 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4592 | ASSERT(!isnullstartblock(bma.got.br_startblock)); |
Christoph Hellwig | 4e087a3 | 2019-10-17 13:12:06 -0700 | [diff] [blame] | 4593 | xfs_bmbt_to_iomap(ip, iomap, &bma.got, flags); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4594 | *seq = READ_ONCE(ifp->if_seq); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4595 | |
Darrick J. Wong | 74b4c5d | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 4596 | if (whichfork == XFS_COW_FORK) |
| 4597 | xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4598 | |
| 4599 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, |
| 4600 | whichfork); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4601 | if (error) |
| 4602 | goto out_finish; |
| 4603 | |
| 4604 | xfs_bmapi_finish(&bma, whichfork, 0); |
| 4605 | error = xfs_trans_commit(tp); |
| 4606 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 4607 | return error; |
| 4608 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4609 | out_finish: |
| 4610 | xfs_bmapi_finish(&bma, whichfork, error); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4611 | out_trans_cancel: |
| 4612 | xfs_trans_cancel(tp); |
| 4613 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4614 | return error; |
| 4615 | } |
| 4616 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4617 | int |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4618 | xfs_bmapi_remap( |
| 4619 | struct xfs_trans *tp, |
| 4620 | struct xfs_inode *ip, |
| 4621 | xfs_fileoff_t bno, |
| 4622 | xfs_filblks_t len, |
| 4623 | xfs_fsblock_t startblock, |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4624 | int flags) |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4625 | { |
| 4626 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4627 | struct xfs_ifork *ifp; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4628 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4629 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4630 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4631 | int whichfork = xfs_bmapi_whichfork(flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4632 | int logflags = 0, error; |
| 4633 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4634 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4635 | ASSERT(len > 0); |
| 4636 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4637 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4638 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC | |
| 4639 | XFS_BMAPI_NORMAP))); |
| 4640 | ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) != |
| 4641 | (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4642 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4643 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 4644 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4645 | return -EFSCORRUPTED; |
| 4646 | } |
| 4647 | |
| 4648 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4649 | return -EIO; |
| 4650 | |
| 4651 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4652 | error = xfs_iread_extents(tp, ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4653 | if (error) |
| 4654 | return error; |
| 4655 | } |
| 4656 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4657 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4658 | /* make sure we only reflink into a hole. */ |
| 4659 | ASSERT(got.br_startoff > bno); |
| 4660 | ASSERT(got.br_startoff - bno >= len); |
| 4661 | } |
| 4662 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4663 | ip->i_d.di_nblocks += len; |
| 4664 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4665 | |
| 4666 | if (ifp->if_flags & XFS_IFBROOT) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4667 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 4668 | cur->bc_ino.flags = 0; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | got.br_startoff = bno; |
| 4672 | got.br_startblock = startblock; |
| 4673 | got.br_blockcount = len; |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4674 | if (flags & XFS_BMAPI_PREALLOC) |
| 4675 | got.br_state = XFS_EXT_UNWRITTEN; |
| 4676 | else |
| 4677 | got.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4678 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4679 | error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4680 | &cur, &got, &logflags, flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4681 | if (error) |
| 4682 | goto error0; |
| 4683 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 4684 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4685 | |
| 4686 | error0: |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4687 | if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4688 | logflags &= ~XFS_ILOG_DEXT; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 4689 | else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4690 | logflags &= ~XFS_ILOG_DBROOT; |
| 4691 | |
| 4692 | if (logflags) |
| 4693 | xfs_trans_log_inode(tp, ip, logflags); |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 4694 | if (cur) |
| 4695 | xfs_btree_del_cursor(cur, error); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4696 | return error; |
| 4697 | } |
| 4698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4700 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4701 | * indlen reservation must be shared across the two new extents that are left |
| 4702 | * behind. |
| 4703 | * |
| 4704 | * Given the original reservation and the worst case indlen for the two new |
| 4705 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4706 | * reservation fairly across the two new extents. If necessary, steal available |
| 4707 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4708 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4709 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4710 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4711 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4712 | xfs_bmap_split_indlen( |
| 4713 | xfs_filblks_t ores, /* original res. */ |
| 4714 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4715 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4716 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4717 | { |
| 4718 | xfs_filblks_t len1 = *indlen1; |
| 4719 | xfs_filblks_t len2 = *indlen2; |
| 4720 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4721 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4722 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4723 | |
| 4724 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4725 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4726 | * indlen for both new extents. |
| 4727 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4728 | if (ores < nres && avail) |
| 4729 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4730 | ores += stolen; |
| 4731 | |
| 4732 | /* nothing else to do if we've satisfied the new reservation */ |
| 4733 | if (ores >= nres) |
| 4734 | return stolen; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4735 | |
| 4736 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4737 | * We can't meet the total required reservation for the two extents. |
| 4738 | * Calculate the percent of the overall shortage between both extents |
| 4739 | * and apply this percentage to each of the requested indlen values. |
| 4740 | * This distributes the shortage fairly and reduces the chances that one |
| 4741 | * of the two extents is left with nothing when extents are repeatedly |
| 4742 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4743 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4744 | resfactor = (ores * 100); |
| 4745 | do_div(resfactor, nres); |
| 4746 | len1 *= resfactor; |
| 4747 | do_div(len1, 100); |
| 4748 | len2 *= resfactor; |
| 4749 | do_div(len2, 100); |
| 4750 | ASSERT(len1 + len2 <= ores); |
| 4751 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4752 | |
| 4753 | /* |
| 4754 | * Hand out the remainder to each extent. If one of the two reservations |
| 4755 | * is zero, we want to make sure that one gets a block first. The loop |
| 4756 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4757 | * is zero. |
| 4758 | */ |
| 4759 | ores -= (len1 + len2); |
| 4760 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4761 | if (ores && !len2 && *indlen2) { |
| 4762 | len2++; |
| 4763 | ores--; |
| 4764 | } |
| 4765 | while (ores) { |
| 4766 | if (len1 < *indlen1) { |
| 4767 | len1++; |
| 4768 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4769 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4770 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4771 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4772 | if (len2 < *indlen2) { |
| 4773 | len2++; |
| 4774 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | |
| 4778 | *indlen1 = len1; |
| 4779 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4780 | |
| 4781 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4782 | } |
| 4783 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4784 | int |
| 4785 | xfs_bmap_del_extent_delay( |
| 4786 | struct xfs_inode *ip, |
| 4787 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4788 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4789 | struct xfs_bmbt_irec *got, |
| 4790 | struct xfs_bmbt_irec *del) |
| 4791 | { |
| 4792 | struct xfs_mount *mp = ip->i_mount; |
| 4793 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4794 | struct xfs_bmbt_irec new; |
| 4795 | int64_t da_old, da_new, da_diff = 0; |
| 4796 | xfs_fileoff_t del_endoff, got_endoff; |
| 4797 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4798 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4799 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4800 | bool isrt; |
| 4801 | |
| 4802 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4803 | |
| 4804 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4805 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4806 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4807 | da_old = startblockval(got->br_startblock); |
| 4808 | da_new = 0; |
| 4809 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4810 | ASSERT(del->br_blockcount > 0); |
| 4811 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4812 | ASSERT(got_endoff >= del_endoff); |
| 4813 | |
| 4814 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4815 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4816 | |
| 4817 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4818 | xfs_mod_frextents(mp, rtexts); |
| 4819 | } |
| 4820 | |
| 4821 | /* |
| 4822 | * Update the inode delalloc counter now and wait to update the |
| 4823 | * sb counters as we might have to borrow some blocks for the |
| 4824 | * indirect block accounting. |
| 4825 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4826 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4827 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4828 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4829 | if (error) |
| 4830 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4831 | ip->i_delayed_blks -= del->br_blockcount; |
| 4832 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4833 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4834 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4835 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4836 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4837 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4838 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4839 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4840 | /* |
| 4841 | * Matches the whole extent. Delete the entry. |
| 4842 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4843 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4844 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4845 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4846 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4847 | /* |
| 4848 | * Deleting the first part of the extent. |
| 4849 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4850 | got->br_startoff = del_endoff; |
| 4851 | got->br_blockcount -= del->br_blockcount; |
| 4852 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4853 | got->br_blockcount), da_old); |
| 4854 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4855 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4856 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4857 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4858 | /* |
| 4859 | * Deleting the last part of the extent. |
| 4860 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4861 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4862 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4863 | got->br_blockcount), da_old); |
| 4864 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4865 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4866 | break; |
| 4867 | case 0: |
| 4868 | /* |
| 4869 | * Deleting the middle of the extent. |
| 4870 | * |
| 4871 | * Distribute the original indlen reservation across the two new |
| 4872 | * extents. Steal blocks from the deleted extent if necessary. |
| 4873 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4874 | * Warn if either of the new indlen reservations is zero as this |
| 4875 | * can lead to delalloc problems. |
| 4876 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4877 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4878 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4879 | |
| 4880 | new.br_blockcount = got_endoff - del_endoff; |
| 4881 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4882 | |
| 4883 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4884 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4885 | del->br_blockcount); |
| 4886 | |
| 4887 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4888 | |
| 4889 | new.br_startoff = del_endoff; |
| 4890 | new.br_state = got->br_state; |
| 4891 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4892 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4893 | xfs_iext_update_extent(ip, state, icur, got); |
| 4894 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4895 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4896 | |
| 4897 | da_new = got_indlen + new_indlen - stolen; |
| 4898 | del->br_blockcount -= stolen; |
| 4899 | break; |
| 4900 | } |
| 4901 | |
| 4902 | ASSERT(da_old >= da_new); |
| 4903 | da_diff = da_old - da_new; |
| 4904 | if (!isrt) |
| 4905 | da_diff += del->br_blockcount; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 4906 | if (da_diff) { |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4907 | xfs_mod_fdblocks(mp, da_diff, false); |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 4908 | xfs_mod_delalloc(mp, -da_diff); |
| 4909 | } |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4910 | return error; |
| 4911 | } |
| 4912 | |
| 4913 | void |
| 4914 | xfs_bmap_del_extent_cow( |
| 4915 | struct xfs_inode *ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4916 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4917 | struct xfs_bmbt_irec *got, |
| 4918 | struct xfs_bmbt_irec *del) |
| 4919 | { |
| 4920 | struct xfs_mount *mp = ip->i_mount; |
| 4921 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4922 | struct xfs_bmbt_irec new; |
| 4923 | xfs_fileoff_t del_endoff, got_endoff; |
| 4924 | int state = BMAP_COWFORK; |
| 4925 | |
| 4926 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4927 | |
| 4928 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4929 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4930 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4931 | ASSERT(del->br_blockcount > 0); |
| 4932 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4933 | ASSERT(got_endoff >= del_endoff); |
| 4934 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4935 | |
| 4936 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4937 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4938 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4939 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4940 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4941 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4942 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4943 | /* |
| 4944 | * Matches the whole extent. Delete the entry. |
| 4945 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4946 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4947 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4948 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4949 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4950 | /* |
| 4951 | * Deleting the first part of the extent. |
| 4952 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4953 | got->br_startoff = del_endoff; |
| 4954 | got->br_blockcount -= del->br_blockcount; |
| 4955 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4956 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4957 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4958 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4959 | /* |
| 4960 | * Deleting the last part of the extent. |
| 4961 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4962 | got->br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4963 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4964 | break; |
| 4965 | case 0: |
| 4966 | /* |
| 4967 | * Deleting the middle of the extent. |
| 4968 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4969 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4970 | |
| 4971 | new.br_startoff = del_endoff; |
| 4972 | new.br_blockcount = got_endoff - del_endoff; |
| 4973 | new.br_state = got->br_state; |
| 4974 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4975 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4976 | xfs_iext_update_extent(ip, state, icur, got); |
| 4977 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4978 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4979 | break; |
| 4980 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 4981 | ip->i_delayed_blks -= del->br_blockcount; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4982 | } |
| 4983 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4984 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4985 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4986 | * after removing space. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4987 | */ |
| 4988 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4989 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4990 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4991 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4992 | struct xfs_iext_cursor *icur, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4993 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4994 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4995 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4996 | int whichfork, /* data or attr fork */ |
| 4997 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4998 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4999 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 5000 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5001 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5002 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 5003 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5004 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5005 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 5006 | int i; /* temp state */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 5007 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5008 | xfs_mount_t *mp; /* mount structure */ |
| 5009 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 5010 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 5011 | /* REFERENCED */ |
| 5012 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 5013 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5014 | struct xfs_bmbt_irec old; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5015 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5016 | mp = ip->i_mount; |
| 5017 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5018 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5019 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5020 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5021 | xfs_iext_get_extent(ifp, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5022 | ASSERT(got.br_startoff <= del->br_startoff); |
| 5023 | del_endoff = del->br_startoff + del->br_blockcount; |
| 5024 | got_endoff = got.br_startoff + got.br_blockcount; |
| 5025 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5026 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5027 | qfield = 0; |
| 5028 | error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5029 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 5030 | /* |
| 5031 | * If it's the case where the directory code is running with no block |
| 5032 | * reservation, and the deleted block is in the middle of its extent, |
| 5033 | * and the resulting insert of an extent would cause transformation to |
| 5034 | * btree format, then reject it. The calling code will then swap blocks |
| 5035 | * around instead. We have to do this now, rather than waiting for the |
| 5036 | * conversion to btree format, since the transaction will be dirty then. |
| 5037 | */ |
| 5038 | if (tp->t_blk_res == 0 && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5039 | ifp->if_format == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5040 | ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) && |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 5041 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 5042 | return -ENOSPC; |
| 5043 | |
| 5044 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5045 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 5046 | xfs_fsblock_t bno; |
| 5047 | xfs_filblks_t len; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5048 | xfs_extlen_t mod; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5049 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5050 | bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize, |
| 5051 | &mod); |
| 5052 | ASSERT(mod == 0); |
| 5053 | len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize, |
| 5054 | &mod); |
| 5055 | ASSERT(mod == 0); |
| 5056 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5057 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 5058 | if (error) |
| 5059 | goto done; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5060 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5061 | nblks = len * mp->m_sb.sb_rextsize; |
| 5062 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 5063 | } else { |
| 5064 | do_fx = 1; |
| 5065 | nblks = del->br_blockcount; |
| 5066 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 5067 | } |
| 5068 | |
| 5069 | del_endblock = del->br_startblock + del->br_blockcount; |
| 5070 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5071 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5072 | if (error) |
| 5073 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5074 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 5075 | error = -EFSCORRUPTED; |
| 5076 | goto done; |
| 5077 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5078 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 5079 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5080 | if (got.br_startoff == del->br_startoff) |
| 5081 | state |= BMAP_LEFT_FILLING; |
| 5082 | if (got_endoff == del_endoff) |
| 5083 | state |= BMAP_RIGHT_FILLING; |
| 5084 | |
| 5085 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 5086 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5087 | /* |
| 5088 | * Matches the whole extent. Delete the entry. |
| 5089 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5090 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5091 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5092 | ifp->if_nextents--; |
| 5093 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5094 | flags |= XFS_ILOG_CORE; |
| 5095 | if (!cur) { |
| 5096 | flags |= xfs_ilog_fext(whichfork); |
| 5097 | break; |
| 5098 | } |
| 5099 | if ((error = xfs_btree_delete(cur, &i))) |
| 5100 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5101 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 5102 | error = -EFSCORRUPTED; |
| 5103 | goto done; |
| 5104 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5105 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5106 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5107 | /* |
| 5108 | * Deleting the first part of the extent. |
| 5109 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5110 | got.br_startoff = del_endoff; |
| 5111 | got.br_startblock = del_endblock; |
| 5112 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5113 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5114 | if (!cur) { |
| 5115 | flags |= xfs_ilog_fext(whichfork); |
| 5116 | break; |
| 5117 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5118 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5119 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5120 | goto done; |
| 5121 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5122 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5123 | /* |
| 5124 | * Deleting the last part of the extent. |
| 5125 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5126 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5127 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5128 | if (!cur) { |
| 5129 | flags |= xfs_ilog_fext(whichfork); |
| 5130 | break; |
| 5131 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5132 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5133 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5134 | goto done; |
| 5135 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5136 | case 0: |
| 5137 | /* |
| 5138 | * Deleting the middle of the extent. |
| 5139 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5140 | old = got; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5141 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5142 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5143 | xfs_iext_update_extent(ip, state, icur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5144 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5145 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5146 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5147 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5148 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5149 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5150 | flags |= XFS_ILOG_CORE; |
| 5151 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5152 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5153 | if (error) |
| 5154 | goto done; |
| 5155 | error = xfs_btree_increment(cur, 0, &i); |
| 5156 | if (error) |
| 5157 | goto done; |
| 5158 | cur->bc_rec.b = new; |
| 5159 | error = xfs_btree_insert(cur, &i); |
| 5160 | if (error && error != -ENOSPC) |
| 5161 | goto done; |
| 5162 | /* |
| 5163 | * If get no-space back from btree insert, it tried a |
| 5164 | * split, and we have a zero block reservation. Fix up |
| 5165 | * our state and return the error. |
| 5166 | */ |
| 5167 | if (error == -ENOSPC) { |
| 5168 | /* |
| 5169 | * Reset the cursor, don't trust it after any |
| 5170 | * insert operation. |
| 5171 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5172 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5173 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5174 | goto done; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5175 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 5176 | error = -EFSCORRUPTED; |
| 5177 | goto done; |
| 5178 | } |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5179 | /* |
| 5180 | * Update the btree record back |
| 5181 | * to the original value. |
| 5182 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5183 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5184 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5185 | goto done; |
| 5186 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5187 | * Reset the extent record back |
| 5188 | * to the original value. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5189 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5190 | xfs_iext_update_extent(ip, state, icur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5191 | flags = 0; |
| 5192 | error = -ENOSPC; |
| 5193 | goto done; |
| 5194 | } |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5195 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 5196 | error = -EFSCORRUPTED; |
| 5197 | goto done; |
| 5198 | } |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5199 | } else |
| 5200 | flags |= xfs_ilog_fext(whichfork); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5201 | |
| 5202 | ifp->if_nextents++; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5203 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5204 | xfs_iext_insert(ip, icur, &new, state); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5205 | break; |
| 5206 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5207 | |
| 5208 | /* remove reverse mapping */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5209 | xfs_rmap_unmap_extent(tp, ip, whichfork, del); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5210 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5211 | /* |
| 5212 | * If we need to, add to list of extents to delete. |
| 5213 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5214 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5215 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
Darrick J. Wong | 74b4c5d | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 5216 | xfs_refcount_decrease_extent(tp, del); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5217 | } else { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5218 | __xfs_bmap_add_free(tp, del->br_startblock, |
Brian Foster | 4e52933 | 2018-05-10 09:35:42 -0700 | [diff] [blame] | 5219 | del->br_blockcount, NULL, |
| 5220 | (bflags & XFS_BMAPI_NODISCARD) || |
| 5221 | del->br_state == XFS_EXT_UNWRITTEN); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5222 | } |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5223 | } |
| 5224 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5225 | /* |
| 5226 | * Adjust inode # blocks in the file. |
| 5227 | */ |
| 5228 | if (nblks) |
| 5229 | ip->i_d.di_nblocks -= nblks; |
| 5230 | /* |
| 5231 | * Adjust quota data. |
| 5232 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5233 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5234 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5235 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5236 | done: |
| 5237 | *logflagsp = flags; |
| 5238 | return error; |
| 5239 | } |
| 5240 | |
| 5241 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | * Unmap (remove) blocks from a file. |
| 5243 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5244 | * that value. If not all extents in the block range can be removed then |
| 5245 | * *done is set. |
| 5246 | */ |
| 5247 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5248 | __xfs_bunmapi( |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5249 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5251 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5252 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | int flags, /* misc flags */ |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 5254 | xfs_extnum_t nexts) /* number of extents max */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | { |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5256 | struct xfs_btree_cur *cur; /* bmap btree cursor */ |
| 5257 | struct xfs_bmbt_irec del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | int error; /* error return value */ |
| 5259 | xfs_extnum_t extno; /* extent number in list */ |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5260 | struct xfs_bmbt_irec got; /* current extent record */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 5261 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5262 | int isrt; /* freeing in rt area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | int logflags; /* transaction logging flags */ |
| 5264 | xfs_extlen_t mod; /* rt extent offset */ |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 5265 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | int tmp_logflags; /* partial logging flags */ |
| 5267 | int wasdel; /* was a delayed alloc extent */ |
| 5268 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5270 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5271 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5272 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5273 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5274 | struct xfs_iext_cursor icur; |
| 5275 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5277 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5278 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5279 | whichfork = xfs_bmapi_whichfork(flags); |
| 5280 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5282 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5283 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5285 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5286 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5287 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | ASSERT(len > 0); |
| 5289 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5290 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5291 | /* |
| 5292 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5293 | * blowing out the transaction with a mix of EFIs and reflink |
| 5294 | * adjustments. |
| 5295 | */ |
Darrick J. Wong | 8c57b88 | 2017-12-10 18:03:53 -0800 | [diff] [blame] | 5296 | if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5297 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5298 | else |
| 5299 | max_len = len; |
| 5300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5302 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5303 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5304 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5305 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | return 0; |
| 5307 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5308 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5309 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5310 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5311 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5312 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5313 | *rlen = 0; |
| 5314 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5316 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | logflags = 0; |
| 5319 | if (ifp->if_flags & XFS_IFBROOT) { |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5320 | ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5321 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 5322 | cur->bc_ino.flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | } else |
| 5324 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5325 | |
| 5326 | if (isrt) { |
| 5327 | /* |
| 5328 | * Synchronize by locking the bitmap inode. |
| 5329 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5330 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5331 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5332 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5333 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | extno = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5337 | while (end != (xfs_fileoff_t)-1 && end >= start && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5338 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5340 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | * Just back up to the previous extent, if so. |
| 5342 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5343 | if (got.br_startoff > end && |
| 5344 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5345 | done = true; |
| 5346 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | } |
| 5348 | /* |
| 5349 | * Is the last block of this extent before the range |
| 5350 | * we're supposed to delete? If so, we're done. |
| 5351 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5352 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5354 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | break; |
| 5356 | /* |
| 5357 | * Then deal with the (possibly delayed) allocated space |
| 5358 | * we found. |
| 5359 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5361 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5362 | |
| 5363 | /* |
| 5364 | * Make sure we don't touch multiple AGF headers out of order |
| 5365 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5366 | */ |
Omar Sandoval | 69ffe59 | 2019-11-26 16:58:08 -0800 | [diff] [blame] | 5367 | if (!wasdel && !isrt) { |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5368 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5369 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5370 | break; |
| 5371 | prev_agno = agno; |
| 5372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | if (got.br_startoff < start) { |
| 5374 | del.br_startoff = start; |
| 5375 | del.br_blockcount -= start - got.br_startoff; |
| 5376 | if (!wasdel) |
| 5377 | del.br_startblock += start - got.br_startoff; |
| 5378 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5379 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5380 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5381 | |
| 5382 | /* How much can we safely unmap? */ |
| 5383 | if (max_len < del.br_blockcount) { |
| 5384 | del.br_startoff += del.br_blockcount - max_len; |
| 5385 | if (!wasdel) |
| 5386 | del.br_startblock += del.br_blockcount - max_len; |
| 5387 | del.br_blockcount = max_len; |
| 5388 | } |
| 5389 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5390 | if (!isrt) |
| 5391 | goto delete; |
| 5392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | sum = del.br_startblock + del.br_blockcount; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5394 | div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod); |
| 5395 | if (mod) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | /* |
| 5397 | * Realtime extent not lined up at the end. |
| 5398 | * The extent could have been split into written |
| 5399 | * and unwritten pieces, or we could just be |
| 5400 | * unmapping part of it. But we can't really |
| 5401 | * get rid of part of a realtime extent. |
| 5402 | */ |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 5403 | if (del.br_state == XFS_EXT_UNWRITTEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | /* |
| 5405 | * This piece is unwritten, or we're not |
| 5406 | * using unwritten extents. Skip over it. |
| 5407 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5408 | ASSERT(end >= mod); |
| 5409 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | del.br_blockcount : mod; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5411 | if (end < got.br_startoff && |
| 5412 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5413 | done = true; |
| 5414 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | } |
| 5416 | continue; |
| 5417 | } |
| 5418 | /* |
| 5419 | * It's written, turn it unwritten. |
| 5420 | * This is better than zeroing it. |
| 5421 | */ |
| 5422 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5423 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | /* |
| 5425 | * If this spans a realtime extent boundary, |
| 5426 | * chop it back to the start of the one we end at. |
| 5427 | */ |
| 5428 | if (del.br_blockcount > mod) { |
| 5429 | del.br_startoff += del.br_blockcount - mod; |
| 5430 | del.br_startblock += del.br_blockcount - mod; |
| 5431 | del.br_blockcount = mod; |
| 5432 | } |
| 5433 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5434 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5435 | whichfork, &icur, &cur, &del, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5436 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5437 | if (error) |
| 5438 | goto error0; |
| 5439 | goto nodelete; |
| 5440 | } |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5441 | div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod); |
| 5442 | if (mod) { |
Omar Sandoval | 0c4da70 | 2019-11-26 16:58:07 -0800 | [diff] [blame] | 5443 | xfs_extlen_t off = mp->m_sb.sb_rextsize - mod; |
| 5444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | /* |
| 5446 | * Realtime extent is lined up at the end but not |
| 5447 | * at the front. We'll get rid of full extents if |
| 5448 | * we can. |
| 5449 | */ |
Omar Sandoval | 0c4da70 | 2019-11-26 16:58:07 -0800 | [diff] [blame] | 5450 | if (del.br_blockcount > off) { |
| 5451 | del.br_blockcount -= off; |
| 5452 | del.br_startoff += off; |
| 5453 | del.br_startblock += off; |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 5454 | } else if (del.br_startoff == start && |
| 5455 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5456 | tp->t_blk_res == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | /* |
| 5458 | * Can't make it unwritten. There isn't |
| 5459 | * a full extent here so just skip it. |
| 5460 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5461 | ASSERT(end >= del.br_blockcount); |
| 5462 | end -= del.br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5463 | if (got.br_startoff > end && |
| 5464 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5465 | done = true; |
| 5466 | break; |
| 5467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | continue; |
| 5469 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5470 | struct xfs_bmbt_irec prev; |
Omar Sandoval | 0c4da70 | 2019-11-26 16:58:07 -0800 | [diff] [blame] | 5471 | xfs_fileoff_t unwrite_start; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | /* |
| 5474 | * This one is already unwritten. |
| 5475 | * It must have a written left neighbor. |
| 5476 | * Unwrite the killed part of that one and |
| 5477 | * try again. |
| 5478 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5479 | if (!xfs_iext_prev_extent(ifp, &icur, &prev)) |
| 5480 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5482 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | ASSERT(del.br_startblock == |
| 5484 | prev.br_startblock + prev.br_blockcount); |
Omar Sandoval | 0c4da70 | 2019-11-26 16:58:07 -0800 | [diff] [blame] | 5485 | unwrite_start = max3(start, |
| 5486 | del.br_startoff - mod, |
| 5487 | prev.br_startoff); |
| 5488 | mod = unwrite_start - prev.br_startoff; |
| 5489 | prev.br_startoff = unwrite_start; |
| 5490 | prev.br_startblock += mod; |
| 5491 | prev.br_blockcount -= mod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5493 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5494 | ip, whichfork, &icur, &cur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5495 | &prev, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | if (error) |
| 5497 | goto error0; |
| 5498 | goto nodelete; |
| 5499 | } else { |
| 5500 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5501 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5502 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5503 | ip, whichfork, &icur, &cur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5504 | &del, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | if (error) |
| 5506 | goto error0; |
| 5507 | goto nodelete; |
| 5508 | } |
| 5509 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5510 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5511 | delete: |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5512 | if (wasdel) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5513 | error = xfs_bmap_del_extent_delay(ip, whichfork, &icur, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5514 | &got, &del); |
| 5515 | } else { |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 5516 | error = xfs_bmap_del_extent_real(ip, tp, &icur, cur, |
| 5517 | &del, &tmp_logflags, whichfork, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5518 | flags); |
| 5519 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5520 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | if (error) |
| 5523 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5524 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5525 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5526 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | /* |
| 5529 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5531 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5532 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
| 5533 | (got.br_startoff > end && |
| 5534 | !xfs_iext_prev_extent(ifp, &icur, &got))) { |
| 5535 | done = true; |
| 5536 | break; |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5537 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5538 | extno++; |
| 5539 | } |
| 5540 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5541 | if (done || end == (xfs_fileoff_t)-1 || end < start) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5542 | *rlen = 0; |
| 5543 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5544 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | /* |
| 5547 | * Convert to a btree if necessary. |
| 5548 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5549 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 5551 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 5552 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | logflags |= tmp_logflags; |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 5554 | } else { |
| 5555 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | } |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 5558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | error0: |
| 5560 | /* |
| 5561 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5562 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5564 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5565 | ifp->if_format != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5566 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5567 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5568 | ifp->if_format != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5569 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | /* |
| 5571 | * Log inode even in the error case, if the transaction |
| 5572 | * is dirty we'll need to shut down the filesystem. |
| 5573 | */ |
| 5574 | if (logflags) |
| 5575 | xfs_trans_log_inode(tp, ip, logflags); |
| 5576 | if (cur) { |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 5577 | if (!error) |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 5578 | cur->bc_ino.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5579 | xfs_btree_del_cursor(cur, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | } |
| 5581 | return error; |
| 5582 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5583 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5584 | /* Unmap a range of a file. */ |
| 5585 | int |
| 5586 | xfs_bunmapi( |
| 5587 | xfs_trans_t *tp, |
| 5588 | struct xfs_inode *ip, |
| 5589 | xfs_fileoff_t bno, |
| 5590 | xfs_filblks_t len, |
| 5591 | int flags, |
| 5592 | xfs_extnum_t nexts, |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5593 | int *done) |
| 5594 | { |
| 5595 | int error; |
| 5596 | |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 5597 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts); |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5598 | *done = (len == 0); |
| 5599 | return error; |
| 5600 | } |
| 5601 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5602 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5603 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5604 | * extent that precedes the target hole of the shift. |
| 5605 | */ |
| 5606 | STATIC bool |
| 5607 | xfs_bmse_can_merge( |
| 5608 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5609 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5610 | xfs_fileoff_t shift) /* shift fsb */ |
| 5611 | { |
| 5612 | xfs_fileoff_t startoff; |
| 5613 | |
| 5614 | startoff = got->br_startoff - shift; |
| 5615 | |
| 5616 | /* |
| 5617 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5618 | * the preceding extent. |
| 5619 | */ |
| 5620 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5621 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5622 | (left->br_state != got->br_state) || |
| 5623 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5624 | return false; |
| 5625 | |
| 5626 | return true; |
| 5627 | } |
| 5628 | |
| 5629 | /* |
| 5630 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5631 | * hole in the file. If an extent shift would result in the extent being fully |
| 5632 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5633 | * the preceding extent rather than do the shift. |
| 5634 | * |
| 5635 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5636 | * with the provided extents via xfs_bmse_can_merge(). |
| 5637 | */ |
| 5638 | STATIC int |
| 5639 | xfs_bmse_merge( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5640 | struct xfs_trans *tp, |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5641 | struct xfs_inode *ip, |
| 5642 | int whichfork, |
| 5643 | xfs_fileoff_t shift, /* shift fsb */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5644 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5645 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5646 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5647 | struct xfs_btree_cur *cur, |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5648 | int *logflags) /* output */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5649 | { |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5650 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5651 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5652 | xfs_filblks_t blockcount; |
| 5653 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5654 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5655 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5656 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5657 | |
| 5658 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5659 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5660 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5661 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5662 | new = *left; |
| 5663 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5664 | |
| 5665 | /* |
| 5666 | * Update the on-disk extent count, the btree if necessary and log the |
| 5667 | * inode. |
| 5668 | */ |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5669 | ifp->if_nextents--; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5670 | *logflags |= XFS_ILOG_CORE; |
| 5671 | if (!cur) { |
| 5672 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5673 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5674 | } |
| 5675 | |
| 5676 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5677 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5678 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5679 | return error; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5680 | if (XFS_IS_CORRUPT(mp, i != 1)) |
| 5681 | return -EFSCORRUPTED; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5682 | |
| 5683 | error = xfs_btree_delete(cur, &i); |
| 5684 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5685 | return error; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5686 | if (XFS_IS_CORRUPT(mp, i != 1)) |
| 5687 | return -EFSCORRUPTED; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5688 | |
| 5689 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5690 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5691 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5692 | return error; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5693 | if (XFS_IS_CORRUPT(mp, i != 1)) |
| 5694 | return -EFSCORRUPTED; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5695 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5696 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5697 | if (error) |
| 5698 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5699 | |
Brian Foster | e20e174 | 2019-09-18 17:50:45 -0700 | [diff] [blame] | 5700 | /* change to extent format if required after extent removal */ |
| 5701 | error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork); |
| 5702 | if (error) |
| 5703 | return error; |
| 5704 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5705 | done: |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5706 | xfs_iext_remove(ip, icur, 0); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 5707 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5708 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5709 | &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5710 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5711 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5712 | xfs_rmap_unmap_extent(tp, ip, whichfork, got); |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5713 | memcpy(&new, got, sizeof(new)); |
| 5714 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5715 | xfs_rmap_map_extent(tp, ip, whichfork, &new); |
| 5716 | return 0; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5717 | } |
| 5718 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5719 | static int |
| 5720 | xfs_bmap_shift_update_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5721 | struct xfs_trans *tp, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5722 | struct xfs_inode *ip, |
| 5723 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5724 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5725 | struct xfs_bmbt_irec *got, |
| 5726 | struct xfs_btree_cur *cur, |
| 5727 | int *logflags, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5728 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5729 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5730 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5731 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5732 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5733 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5734 | *logflags |= XFS_ILOG_CORE; |
| 5735 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5736 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5737 | |
| 5738 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5739 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5740 | if (error) |
| 5741 | return error; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5742 | if (XFS_IS_CORRUPT(mp, i != 1)) |
| 5743 | return -EFSCORRUPTED; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5744 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5745 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5746 | if (error) |
| 5747 | return error; |
| 5748 | } else { |
| 5749 | *logflags |= XFS_ILOG_DEXT; |
| 5750 | } |
| 5751 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5752 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5753 | got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5754 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5755 | /* update reverse mapping */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5756 | xfs_rmap_unmap_extent(tp, ip, whichfork, &prev); |
| 5757 | xfs_rmap_map_extent(tp, ip, whichfork, got); |
| 5758 | return 0; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5759 | } |
| 5760 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5761 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5762 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5763 | struct xfs_trans *tp, |
| 5764 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5765 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5766 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 333f950 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5767 | bool *done) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5768 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5769 | int whichfork = XFS_DATA_FORK; |
| 5770 | struct xfs_mount *mp = ip->i_mount; |
| 5771 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5772 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5773 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5774 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5775 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5776 | int error = 0; |
| 5777 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5778 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5779 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 5780 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5781 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5782 | } |
| 5783 | |
| 5784 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5785 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5786 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5787 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5788 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5789 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5790 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5791 | if (error) |
| 5792 | return error; |
| 5793 | } |
| 5794 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5795 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5796 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 5797 | cur->bc_ino.flags = 0; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5798 | } |
| 5799 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5800 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5801 | *done = true; |
| 5802 | goto del_cursor; |
| 5803 | } |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5804 | if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) { |
| 5805 | error = -EFSCORRUPTED; |
| 5806 | goto del_cursor; |
| 5807 | } |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5808 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5809 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5810 | if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5811 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5812 | error = -EINVAL; |
| 5813 | goto del_cursor; |
| 5814 | } |
| 5815 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5816 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5817 | error = xfs_bmse_merge(tp, ip, whichfork, |
| 5818 | offset_shift_fsb, &icur, &got, &prev, |
| 5819 | cur, &logflags); |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5820 | if (error) |
| 5821 | goto del_cursor; |
| 5822 | goto done; |
| 5823 | } |
| 5824 | } else { |
| 5825 | if (got.br_startoff < offset_shift_fsb) { |
| 5826 | error = -EINVAL; |
| 5827 | goto del_cursor; |
| 5828 | } |
| 5829 | } |
| 5830 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5831 | error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got, |
| 5832 | cur, &logflags, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5833 | if (error) |
| 5834 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5835 | |
Christoph Hellwig | 4263036 | 2017-11-03 10:34:41 -0700 | [diff] [blame] | 5836 | done: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5837 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
| 5838 | *done = true; |
| 5839 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5840 | } |
| 5841 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5842 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5843 | del_cursor: |
| 5844 | if (cur) |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5845 | xfs_btree_del_cursor(cur, error); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5846 | if (logflags) |
| 5847 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5848 | return error; |
| 5849 | } |
| 5850 | |
Darrick J. Wong | f62cb48 | 2018-06-21 23:26:57 -0700 | [diff] [blame] | 5851 | /* Make sure we won't be right-shifting an extent past the maximum bound. */ |
| 5852 | int |
| 5853 | xfs_bmap_can_insert_extents( |
| 5854 | struct xfs_inode *ip, |
| 5855 | xfs_fileoff_t off, |
| 5856 | xfs_fileoff_t shift) |
| 5857 | { |
| 5858 | struct xfs_bmbt_irec got; |
| 5859 | int is_empty; |
| 5860 | int error = 0; |
| 5861 | |
| 5862 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5863 | |
| 5864 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 5865 | return -EIO; |
| 5866 | |
| 5867 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 5868 | error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty); |
| 5869 | if (!error && !is_empty && got.br_startoff >= off && |
| 5870 | ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff) |
| 5871 | error = -EINVAL; |
| 5872 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 5873 | |
| 5874 | return error; |
| 5875 | } |
| 5876 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5877 | int |
| 5878 | xfs_bmap_insert_extents( |
| 5879 | struct xfs_trans *tp, |
| 5880 | struct xfs_inode *ip, |
| 5881 | xfs_fileoff_t *next_fsb, |
| 5882 | xfs_fileoff_t offset_shift_fsb, |
| 5883 | bool *done, |
Brian Foster | 333f950 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5884 | xfs_fileoff_t stop_fsb) |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5885 | { |
| 5886 | int whichfork = XFS_DATA_FORK; |
| 5887 | struct xfs_mount *mp = ip->i_mount; |
| 5888 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5889 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5890 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5891 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5892 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5893 | int error = 0; |
| 5894 | int logflags = 0; |
| 5895 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5896 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 5897 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5898 | return -EFSCORRUPTED; |
| 5899 | } |
| 5900 | |
| 5901 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5902 | return -EIO; |
| 5903 | |
| 5904 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5905 | |
| 5906 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5907 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5908 | if (error) |
| 5909 | return error; |
| 5910 | } |
| 5911 | |
| 5912 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5913 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 5914 | cur->bc_ino.flags = 0; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5915 | } |
| 5916 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5917 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5918 | xfs_iext_last(ifp, &icur); |
| 5919 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5920 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5921 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5922 | goto del_cursor; |
| 5923 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5924 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5925 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5926 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5927 | goto del_cursor; |
| 5928 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5929 | } |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 5930 | if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) { |
| 5931 | error = -EFSCORRUPTED; |
| 5932 | goto del_cursor; |
| 5933 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5934 | |
Brian Foster | d0c2204 | 2019-12-11 13:18:38 -0800 | [diff] [blame] | 5935 | if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) { |
Darrick J. Wong | c2414ad | 2019-10-28 16:12:34 -0700 | [diff] [blame] | 5936 | error = -EFSCORRUPTED; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5937 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5938 | } |
| 5939 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5940 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5941 | if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5942 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5943 | error = -EINVAL; |
| 5944 | goto del_cursor; |
| 5945 | } |
| 5946 | |
| 5947 | /* |
| 5948 | * Unlike a left shift (which involves a hole punch), a right |
| 5949 | * shift does not modify extent neighbors in any way. We should |
| 5950 | * never find mergeable extents in this scenario. Check anyways |
| 5951 | * and warn if we encounter two extents that could be one. |
| 5952 | */ |
| 5953 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5954 | WARN_ON_ONCE(1); |
| 5955 | } |
| 5956 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5957 | error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got, |
| 5958 | cur, &logflags, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5959 | if (error) |
| 5960 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5961 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5962 | if (!xfs_iext_prev_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5963 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5964 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5965 | goto del_cursor; |
| 5966 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5967 | |
| 5968 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5969 | del_cursor: |
| 5970 | if (cur) |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5971 | xfs_btree_del_cursor(cur, error); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5972 | if (logflags) |
| 5973 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5974 | return error; |
| 5975 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5976 | |
| 5977 | /* |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5978 | * Splits an extent into two extents at split_fsb block such that it is the |
| 5979 | * first block of the current_ext. @ext is a target extent to be split. |
| 5980 | * @split_fsb is a block where the extents is split. If split_fsb lies in a |
| 5981 | * hole or the first block of extents, just return 0. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5982 | */ |
Brian Foster | b73df17 | 2020-02-26 09:43:15 -0800 | [diff] [blame] | 5983 | int |
| 5984 | xfs_bmap_split_extent( |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5985 | struct xfs_trans *tp, |
| 5986 | struct xfs_inode *ip, |
Brian Foster | 4b77a08 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5987 | xfs_fileoff_t split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5988 | { |
| 5989 | int whichfork = XFS_DATA_FORK; |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 5990 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5991 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5992 | struct xfs_bmbt_irec got; |
| 5993 | struct xfs_bmbt_irec new; /* split extent */ |
| 5994 | struct xfs_mount *mp = ip->i_mount; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5995 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5996 | struct xfs_iext_cursor icur; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5997 | int error = 0; |
| 5998 | int logflags = 0; |
| 5999 | int i = 0; |
| 6000 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 6001 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
Darrick J. Wong | a71895c | 2019-11-11 12:53:22 -0800 | [diff] [blame] | 6002 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6003 | return -EFSCORRUPTED; |
| 6004 | } |
| 6005 | |
| 6006 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 6007 | return -EIO; |
| 6008 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6009 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6010 | /* Read in all the extents */ |
| 6011 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6012 | if (error) |
| 6013 | return error; |
| 6014 | } |
| 6015 | |
| 6016 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 6017 | * 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] | 6018 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 6019 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 6020 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6021 | return 0; |
| 6022 | |
| 6023 | gotblkcnt = split_fsb - got.br_startoff; |
| 6024 | new.br_startoff = split_fsb; |
| 6025 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 6026 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 6027 | new.br_state = got.br_state; |
| 6028 | |
| 6029 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6030 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 6031 | cur->bc_ino.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 6032 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6033 | if (error) |
| 6034 | goto del_cursor; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 6035 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 6036 | error = -EFSCORRUPTED; |
| 6037 | goto del_cursor; |
| 6038 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6039 | } |
| 6040 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6041 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 6042 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, |
| 6043 | &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6044 | |
| 6045 | logflags = XFS_ILOG_CORE; |
| 6046 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 6047 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6048 | if (error) |
| 6049 | goto del_cursor; |
| 6050 | } else |
| 6051 | logflags |= XFS_ILOG_DEXT; |
| 6052 | |
| 6053 | /* Add new extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 6054 | xfs_iext_next(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 6055 | xfs_iext_insert(ip, &icur, &new, 0); |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 6056 | ifp->if_nextents++; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6057 | |
| 6058 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 6059 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6060 | if (error) |
| 6061 | goto del_cursor; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 6062 | if (XFS_IS_CORRUPT(mp, i != 0)) { |
| 6063 | error = -EFSCORRUPTED; |
| 6064 | goto del_cursor; |
| 6065 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6066 | error = xfs_btree_insert(cur, &i); |
| 6067 | if (error) |
| 6068 | goto del_cursor; |
Darrick J. Wong | f9e0370 | 2019-11-11 12:52:18 -0800 | [diff] [blame] | 6069 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
| 6070 | error = -EFSCORRUPTED; |
| 6071 | goto del_cursor; |
| 6072 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | /* |
| 6076 | * Convert to a btree if necessary. |
| 6077 | */ |
| 6078 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 6079 | int tmp_logflags; /* partial log flag return val */ |
| 6080 | |
| 6081 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 6082 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 6083 | &tmp_logflags, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6084 | logflags |= tmp_logflags; |
| 6085 | } |
| 6086 | |
| 6087 | del_cursor: |
| 6088 | if (cur) { |
Dave Chinner | 92219c2 | 2020-03-10 17:52:53 -0700 | [diff] [blame] | 6089 | cur->bc_ino.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 6090 | xfs_btree_del_cursor(cur, error); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6091 | } |
| 6092 | |
| 6093 | if (logflags) |
| 6094 | xfs_trans_log_inode(tp, ip, logflags); |
| 6095 | return error; |
| 6096 | } |
| 6097 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6098 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6099 | static bool |
| 6100 | xfs_bmap_is_update_needed( |
| 6101 | struct xfs_bmbt_irec *bmap) |
| 6102 | { |
| 6103 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6104 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6105 | } |
| 6106 | |
| 6107 | /* Record a bmap intent. */ |
| 6108 | static int |
| 6109 | __xfs_bmap_add( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6110 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6111 | enum xfs_bmap_intent_type type, |
| 6112 | struct xfs_inode *ip, |
| 6113 | int whichfork, |
| 6114 | struct xfs_bmbt_irec *bmap) |
| 6115 | { |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6116 | struct xfs_bmap_intent *bi; |
| 6117 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6118 | trace_xfs_bmap_defer(tp->t_mountp, |
| 6119 | XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock), |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6120 | type, |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6121 | XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock), |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6122 | ip->i_ino, whichfork, |
| 6123 | bmap->br_startoff, |
| 6124 | bmap->br_blockcount, |
| 6125 | bmap->br_state); |
| 6126 | |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 6127 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6128 | INIT_LIST_HEAD(&bi->bi_list); |
| 6129 | bi->bi_type = type; |
| 6130 | bi->bi_owner = ip; |
| 6131 | bi->bi_whichfork = whichfork; |
| 6132 | bi->bi_bmap = *bmap; |
| 6133 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6134 | 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] | 6135 | return 0; |
| 6136 | } |
| 6137 | |
| 6138 | /* Map an extent into a file. */ |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6139 | void |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6140 | xfs_bmap_map_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6141 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6142 | struct xfs_inode *ip, |
| 6143 | struct xfs_bmbt_irec *PREV) |
| 6144 | { |
| 6145 | if (!xfs_bmap_is_update_needed(PREV)) |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6146 | return; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6147 | |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6148 | __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] | 6149 | } |
| 6150 | |
| 6151 | /* Unmap an extent out of a file. */ |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6152 | void |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6153 | xfs_bmap_unmap_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6154 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6155 | struct xfs_inode *ip, |
| 6156 | struct xfs_bmbt_irec *PREV) |
| 6157 | { |
| 6158 | if (!xfs_bmap_is_update_needed(PREV)) |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6159 | return; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6160 | |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 6161 | __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] | 6162 | } |
| 6163 | |
| 6164 | /* |
| 6165 | * Process one of the deferred bmap operations. We pass back the |
| 6166 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6167 | */ |
| 6168 | int |
| 6169 | xfs_bmap_finish_one( |
| 6170 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6171 | struct xfs_inode *ip, |
| 6172 | enum xfs_bmap_intent_type type, |
| 6173 | int whichfork, |
| 6174 | xfs_fileoff_t startoff, |
| 6175 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6176 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6177 | xfs_exntst_t state) |
| 6178 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6179 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6180 | |
Brian Foster | 3728379 | 2018-07-11 22:26:23 -0700 | [diff] [blame] | 6181 | ASSERT(tp->t_firstblock == NULLFSBLOCK); |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6182 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6183 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6184 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6185 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6186 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6187 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6188 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6189 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6190 | |
| 6191 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6192 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6193 | return -EIO; |
| 6194 | |
| 6195 | switch (type) { |
| 6196 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6197 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Brian Foster | ff3edf2 | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 6198 | startblock, 0); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6199 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6200 | break; |
| 6201 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6202 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 6203 | XFS_BMAPI_REMAP, 1); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6204 | break; |
| 6205 | default: |
| 6206 | ASSERT(0); |
| 6207 | error = -EFSCORRUPTED; |
| 6208 | } |
| 6209 | |
| 6210 | return error; |
| 6211 | } |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6212 | |
| 6213 | /* Check that an inode's extent does not have invalid flags or bad ranges. */ |
| 6214 | xfs_failaddr_t |
| 6215 | xfs_bmap_validate_extent( |
| 6216 | struct xfs_inode *ip, |
| 6217 | int whichfork, |
| 6218 | struct xfs_bmbt_irec *irec) |
| 6219 | { |
| 6220 | struct xfs_mount *mp = ip->i_mount; |
| 6221 | xfs_fsblock_t endfsb; |
| 6222 | bool isrt; |
| 6223 | |
| 6224 | isrt = XFS_IS_REALTIME_INODE(ip); |
| 6225 | endfsb = irec->br_startblock + irec->br_blockcount - 1; |
| 6226 | if (isrt) { |
| 6227 | if (!xfs_verify_rtbno(mp, irec->br_startblock)) |
| 6228 | return __this_address; |
| 6229 | if (!xfs_verify_rtbno(mp, endfsb)) |
| 6230 | return __this_address; |
| 6231 | } else { |
| 6232 | if (!xfs_verify_fsbno(mp, irec->br_startblock)) |
| 6233 | return __this_address; |
| 6234 | if (!xfs_verify_fsbno(mp, endfsb)) |
| 6235 | return __this_address; |
| 6236 | if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) != |
| 6237 | XFS_FSB_TO_AGNO(mp, endfsb)) |
| 6238 | return __this_address; |
| 6239 | } |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 6240 | if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK) |
| 6241 | return __this_address; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6242 | return NULL; |
| 6243 | } |