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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 40 | |
| 41 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 42 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | */ |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 46 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 47 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 49 | void |
| 50 | xfs_bmap_compute_maxlevels( |
| 51 | xfs_mount_t *mp, /* file system mount structure */ |
| 52 | int whichfork) /* data or attr fork */ |
| 53 | { |
| 54 | int level; /* btree level */ |
| 55 | uint maxblocks; /* max blocks at this level */ |
| 56 | uint maxleafents; /* max leaf entries possible */ |
| 57 | int maxrootrecs; /* max records in root block */ |
| 58 | int minleafrecs; /* min records in leaf block */ |
| 59 | int minnoderecs; /* min records in node block */ |
| 60 | int sz; /* root block size */ |
| 61 | |
| 62 | /* |
| 63 | * The maximum number of extents in a file, hence the maximum |
| 64 | * number of leaf entries, is controlled by the type of di_nextents |
| 65 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 66 | * (a signed 16-bit number, xfs_aextnum_t). |
| 67 | * |
| 68 | * Note that we can no longer assume that if we are in ATTR1 that |
| 69 | * the fork offset of all the inodes will be |
| 70 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 71 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 72 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 73 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 74 | * of a minimum size available. |
| 75 | */ |
| 76 | if (whichfork == XFS_DATA_FORK) { |
| 77 | maxleafents = MAXEXTNUM; |
| 78 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 79 | } else { |
| 80 | maxleafents = MAXAEXTNUM; |
| 81 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 82 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 83 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 84 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 85 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 86 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 87 | for (level = 1; maxblocks > 1; level++) { |
| 88 | if (maxblocks <= maxrootrecs) |
| 89 | maxblocks = 1; |
| 90 | else |
| 91 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 92 | } |
| 93 | mp->m_bm_maxlevels[whichfork] = level; |
| 94 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 96 | STATIC int /* error */ |
| 97 | xfs_bmbt_lookup_eq( |
| 98 | struct xfs_btree_cur *cur, |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 99 | struct xfs_bmbt_irec *irec, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 100 | int *stat) /* success/failure */ |
| 101 | { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 102 | cur->bc_rec.b = *irec; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 103 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 104 | } |
| 105 | |
| 106 | STATIC int /* error */ |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 107 | xfs_bmbt_lookup_first( |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 108 | struct xfs_btree_cur *cur, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 109 | int *stat) /* success/failure */ |
| 110 | { |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 111 | cur->bc_rec.b.br_startoff = 0; |
| 112 | cur->bc_rec.b.br_startblock = 0; |
| 113 | cur->bc_rec.b.br_blockcount = 0; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 114 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 115 | } |
| 116 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 117 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 118 | * Check if the inode needs to be converted to btree format. |
| 119 | */ |
| 120 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 121 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 122 | return whichfork != XFS_COW_FORK && |
| 123 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 124 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 125 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Check if the inode should be converted to extent format. |
| 130 | */ |
| 131 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 132 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 133 | return whichfork != XFS_COW_FORK && |
| 134 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 135 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 136 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 137 | } |
| 138 | |
| 139 | /* |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 140 | * 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] | 141 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 142 | */ |
| 143 | STATIC int |
| 144 | xfs_bmbt_update( |
| 145 | struct xfs_btree_cur *cur, |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 146 | struct xfs_bmbt_irec *irec) |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 147 | { |
| 148 | union xfs_btree_rec rec; |
| 149 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 150 | xfs_bmbt_disk_set_all(&rec.bmbt, irec); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 151 | return xfs_btree_update(cur, &rec); |
| 152 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 155 | * Compute the worst-case number of indirect blocks that will be used |
| 156 | * for ip's delayed extent of length "len". |
| 157 | */ |
| 158 | STATIC xfs_filblks_t |
| 159 | xfs_bmap_worst_indlen( |
| 160 | xfs_inode_t *ip, /* incore inode pointer */ |
| 161 | xfs_filblks_t len) /* delayed extent length */ |
| 162 | { |
| 163 | int level; /* btree level number */ |
| 164 | int maxrecs; /* maximum record count at this level */ |
| 165 | xfs_mount_t *mp; /* mount structure */ |
| 166 | xfs_filblks_t rval; /* return value */ |
| 167 | |
| 168 | mp = ip->i_mount; |
| 169 | maxrecs = mp->m_bmap_dmxr[0]; |
| 170 | for (level = 0, rval = 0; |
| 171 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 172 | level++) { |
| 173 | len += maxrecs - 1; |
| 174 | do_div(len, maxrecs); |
| 175 | rval += len; |
Darrick J. Wong | 5e5c943 | 2017-09-18 09:41:17 -0700 | [diff] [blame] | 176 | if (len == 1) |
| 177 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 178 | level - 1; |
| 179 | if (level == 0) |
| 180 | maxrecs = mp->m_bmap_dmxr[1]; |
| 181 | } |
| 182 | return rval; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Calculate the default attribute fork offset for newly created inodes. |
| 187 | */ |
| 188 | uint |
| 189 | xfs_default_attroffset( |
| 190 | struct xfs_inode *ip) |
| 191 | { |
| 192 | struct xfs_mount *mp = ip->i_mount; |
| 193 | uint offset; |
| 194 | |
| 195 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 196 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 197 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 198 | } else { |
| 199 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 200 | } |
| 201 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 202 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 203 | return offset; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Helper routine to reset inode di_forkoff field when switching |
| 208 | * attribute fork from local to extent format - we reset it where |
| 209 | * possible to make space available for inline data fork extents. |
| 210 | */ |
| 211 | STATIC void |
| 212 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 213 | xfs_inode_t *ip, |
| 214 | int whichfork) |
| 215 | { |
| 216 | if (whichfork == XFS_ATTR_FORK && |
| 217 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 218 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 219 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 220 | |
| 221 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 222 | ip->i_d.di_forkoff = dfl_forkoff; |
| 223 | } |
| 224 | } |
| 225 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 226 | #ifdef DEBUG |
| 227 | STATIC struct xfs_buf * |
| 228 | xfs_bmap_get_bp( |
| 229 | struct xfs_btree_cur *cur, |
| 230 | xfs_fsblock_t bno) |
| 231 | { |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame] | 232 | struct xfs_log_item *lip; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 233 | int i; |
| 234 | |
| 235 | if (!cur) |
| 236 | return NULL; |
| 237 | |
| 238 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 239 | if (!cur->bc_bufs[i]) |
| 240 | break; |
| 241 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 242 | return cur->bc_bufs[i]; |
| 243 | } |
| 244 | |
| 245 | /* 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] | 246 | list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) { |
| 247 | struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip; |
| 248 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 249 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 250 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 251 | return bip->bli_buf; |
| 252 | } |
| 253 | |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | STATIC void |
| 258 | xfs_check_block( |
| 259 | struct xfs_btree_block *block, |
| 260 | xfs_mount_t *mp, |
| 261 | int root, |
| 262 | short sz) |
| 263 | { |
| 264 | int i, j, dmxr; |
| 265 | __be64 *pp, *thispa; /* pointer to block address */ |
| 266 | xfs_bmbt_key_t *prevp, *keyp; |
| 267 | |
| 268 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 269 | |
| 270 | prevp = NULL; |
| 271 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 272 | dmxr = mp->m_bmap_dmxr[0]; |
| 273 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 274 | |
| 275 | if (prevp) { |
| 276 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 277 | be64_to_cpu(keyp->br_startoff)); |
| 278 | } |
| 279 | prevp = keyp; |
| 280 | |
| 281 | /* |
| 282 | * Compare the block numbers to see if there are dups. |
| 283 | */ |
| 284 | if (root) |
| 285 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 286 | else |
| 287 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 288 | |
| 289 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 290 | if (root) |
| 291 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 292 | else |
| 293 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 294 | if (*thispa == *pp) { |
| 295 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 296 | __func__, j, i, |
| 297 | (unsigned long long)be64_to_cpu(*thispa)); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 298 | xfs_err(mp, "%s: ptrs are equal in node\n", |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 299 | __func__); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 300 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * 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] | 308 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 309 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 310 | * them. The btree record ordering checks will still be done, so for such large |
| 311 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 312 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 313 | STATIC void |
| 314 | xfs_bmap_check_leaf_extents( |
| 315 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 316 | xfs_inode_t *ip, /* incore inode pointer */ |
| 317 | int whichfork) /* data or attr fork */ |
| 318 | { |
| 319 | struct xfs_btree_block *block; /* current btree block */ |
| 320 | xfs_fsblock_t bno; /* block # of "block" */ |
| 321 | xfs_buf_t *bp; /* buffer for "block" */ |
| 322 | int error; /* error return value */ |
| 323 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 324 | struct xfs_ifork *ifp; /* fork structure */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 325 | int level; /* btree level, for checking */ |
| 326 | xfs_mount_t *mp; /* file system mount structure */ |
| 327 | __be64 *pp; /* pointer to block address */ |
| 328 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 329 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 330 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 331 | int bp_release = 0; |
| 332 | |
| 333 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 334 | return; |
| 335 | } |
| 336 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 337 | /* skip large extent count inodes */ |
| 338 | if (ip->i_d.di_nextents > 10000) |
| 339 | return; |
| 340 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 341 | bno = NULLFSBLOCK; |
| 342 | mp = ip->i_mount; |
| 343 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 344 | block = ifp->if_broot; |
| 345 | /* |
| 346 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 347 | */ |
| 348 | level = be16_to_cpu(block->bb_level); |
| 349 | ASSERT(level > 0); |
| 350 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 351 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 352 | bno = be64_to_cpu(*pp); |
| 353 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 354 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 355 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 356 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 357 | |
| 358 | /* |
| 359 | * Go down the tree until leaf level is reached, following the first |
| 360 | * pointer (leftmost) at each level. |
| 361 | */ |
| 362 | while (level-- > 0) { |
| 363 | /* See if buf is in cur first */ |
| 364 | bp_release = 0; |
| 365 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 366 | if (!bp) { |
| 367 | bp_release = 1; |
Eric Sandeen | f5b999c | 2019-06-12 09:00:00 -0700 | [diff] [blame] | 368 | error = xfs_btree_read_bufl(mp, NULL, bno, &bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 369 | XFS_BMAP_BTREE_REF, |
| 370 | &xfs_bmbt_buf_ops); |
| 371 | if (error) |
| 372 | goto error_norelse; |
| 373 | } |
| 374 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 375 | if (level == 0) |
| 376 | break; |
| 377 | |
| 378 | /* |
| 379 | * Check this block for basic sanity (increasing keys and |
| 380 | * no duplicate blocks). |
| 381 | */ |
| 382 | |
| 383 | xfs_check_block(block, mp, 0, 0); |
| 384 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 385 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 386 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 387 | xfs_verify_fsbno(mp, bno), error0); |
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 | |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 556 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, 0); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 557 | new->xefi_startblock = bno; |
| 558 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 559 | if (oinfo) |
| 560 | new->xefi_oinfo = *oinfo; |
| 561 | else |
Darrick J. Wong | 7280fed | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 562 | new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE; |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 563 | new->xefi_skip_discard = skip_discard; |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 564 | trace_xfs_bmap_free_defer(tp->t_mountp, |
| 565 | XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0, |
| 566 | XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len); |
| 567 | xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Inode fork format manipulation functions |
| 572 | */ |
| 573 | |
| 574 | /* |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 575 | * Convert the inode format to extent format if it currently is in btree format, |
| 576 | * but the extent list is small enough that it fits into the extent format. |
| 577 | * |
| 578 | * Since the extents are already in-core, all we have to do is give up the space |
| 579 | * for the btree root and pitch the leaf block. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 580 | */ |
| 581 | STATIC int /* error */ |
| 582 | xfs_bmap_btree_to_extents( |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 583 | struct xfs_trans *tp, /* transaction pointer */ |
| 584 | struct xfs_inode *ip, /* incore inode pointer */ |
| 585 | struct xfs_btree_cur *cur, /* btree cursor */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 586 | int *logflagsp, /* inode logging flags */ |
| 587 | int whichfork) /* data or attr fork */ |
| 588 | { |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 589 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 590 | struct xfs_mount *mp = ip->i_mount; |
| 591 | struct xfs_btree_block *rblock = ifp->if_broot; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 592 | struct xfs_btree_block *cblock;/* child btree block */ |
| 593 | xfs_fsblock_t cbno; /* child block number */ |
| 594 | xfs_buf_t *cbp; /* child block's buffer */ |
| 595 | int error; /* error return value */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 596 | __be64 *pp; /* ptr to block address */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 597 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 598 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 599 | /* check if we actually need the extent format first: */ |
| 600 | if (!xfs_bmap_wants_extents(ip, whichfork)) |
| 601 | return 0; |
| 602 | |
| 603 | ASSERT(cur); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 604 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 605 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 606 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 607 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 608 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 609 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 610 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 611 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 612 | cbno = be64_to_cpu(*pp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 613 | #ifdef DEBUG |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 614 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, |
| 615 | xfs_btree_check_lptr(cur, cbno, 1)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 616 | #endif |
Eric Sandeen | f5b999c | 2019-06-12 09:00:00 -0700 | [diff] [blame] | 617 | 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] | 618 | &xfs_bmbt_buf_ops); |
| 619 | if (error) |
| 620 | return error; |
| 621 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 622 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 623 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 624 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 625 | xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 626 | ip->i_d.di_nblocks--; |
| 627 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 628 | xfs_trans_binval(tp, cbp); |
| 629 | if (cur->bc_bufs[0] == cbp) |
| 630 | cur->bc_bufs[0] = NULL; |
| 631 | xfs_iroot_realloc(ip, -1, whichfork); |
| 632 | ASSERT(ifp->if_broot == NULL); |
| 633 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 634 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 635 | *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * Convert an extents-format file into a btree-format file. |
| 641 | * The new file will have a root block (in the inode) and a single child block. |
| 642 | */ |
| 643 | STATIC int /* error */ |
| 644 | xfs_bmap_extents_to_btree( |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 645 | struct xfs_trans *tp, /* transaction pointer */ |
| 646 | struct xfs_inode *ip, /* incore inode pointer */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 647 | struct xfs_btree_cur **curp, /* cursor returned to caller */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 648 | int wasdel, /* converting a delayed alloc */ |
| 649 | int *logflagsp, /* inode logging flags */ |
| 650 | int whichfork) /* data or attr fork */ |
| 651 | { |
| 652 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 653 | struct xfs_buf *abp; /* buffer for ablock */ |
| 654 | struct xfs_alloc_arg args; /* allocation arguments */ |
| 655 | struct xfs_bmbt_rec *arp; /* child record pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 656 | struct xfs_btree_block *block; /* btree root block */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 657 | struct xfs_btree_cur *cur; /* bmap btree cursor */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 658 | int error; /* error return value */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 659 | struct xfs_ifork *ifp; /* inode fork pointer */ |
| 660 | struct xfs_bmbt_key *kp; /* root block key pointer */ |
| 661 | struct xfs_mount *mp; /* mount structure */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 662 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 663 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 664 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 665 | xfs_extnum_t cnt = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 666 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 667 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 668 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 669 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 670 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 671 | |
| 672 | /* |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 673 | * Make space in the inode incore. This needs to be undone if we fail |
| 674 | * to expand the root. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 675 | */ |
| 676 | xfs_iroot_realloc(ip, 1, whichfork); |
| 677 | ifp->if_flags |= XFS_IFBROOT; |
| 678 | |
| 679 | /* |
| 680 | * Fill in the root. |
| 681 | */ |
| 682 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 683 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 684 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 685 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 686 | /* |
| 687 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 688 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 689 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 690 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 691 | /* |
| 692 | * Convert to a btree with two levels, one record in root. |
| 693 | */ |
| 694 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 695 | memset(&args, 0, sizeof(args)); |
| 696 | args.tp = tp; |
| 697 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 698 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 699 | if (tp->t_firstblock == NULLFSBLOCK) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 700 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 701 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 702 | } else if (tp->t_flags & XFS_TRANS_LOWMODE) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 703 | args.type = XFS_ALLOCTYPE_START_BNO; |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 704 | args.fsbno = tp->t_firstblock; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 705 | } else { |
| 706 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 707 | args.fsbno = tp->t_firstblock; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 708 | } |
| 709 | args.minlen = args.maxlen = args.prod = 1; |
| 710 | args.wasdel = wasdel; |
| 711 | *logflagsp = 0; |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 712 | error = xfs_alloc_vextent(&args); |
| 713 | if (error) |
| 714 | goto out_root_realloc; |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 715 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 716 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 717 | error = -ENOSPC; |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 718 | goto out_root_realloc; |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 719 | } |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 720 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 721 | /* |
| 722 | * Allocation can't fail, the space was reserved. |
| 723 | */ |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 724 | ASSERT(tp->t_firstblock == NULLFSBLOCK || |
| 725 | args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock)); |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 726 | tp->t_firstblock = args.fsbno; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 727 | cur->bc_private.b.allocated++; |
| 728 | ip->i_d.di_nblocks++; |
| 729 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
Eric Sandeen | f5b999c | 2019-06-12 09:00:00 -0700 | [diff] [blame] | 730 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno); |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 731 | if (!abp) { |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 732 | error = -EFSCORRUPTED; |
| 733 | goto out_unreserve_dquot; |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 734 | } |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 735 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 736 | /* |
| 737 | * Fill in the child block. |
| 738 | */ |
| 739 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 740 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 741 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 742 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 743 | XFS_BTREE_LONG_PTRS); |
| 744 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 745 | for_each_xfs_iext(ifp, &icur, &rec) { |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 746 | if (isnullstartblock(rec.br_startblock)) |
| 747 | continue; |
| 748 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt); |
| 749 | xfs_bmbt_disk_set_all(arp, &rec); |
| 750 | cnt++; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 751 | } |
| 752 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 753 | xfs_btree_set_numrecs(ablock, cnt); |
| 754 | |
| 755 | /* |
| 756 | * Fill in the root key and pointer. |
| 757 | */ |
| 758 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 759 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 760 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 761 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 762 | be16_to_cpu(block->bb_level))); |
| 763 | *pp = cpu_to_be64(args.fsbno); |
| 764 | |
| 765 | /* |
| 766 | * Do all this logging at the end so that |
| 767 | * the root is at the right level. |
| 768 | */ |
| 769 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 770 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 771 | ASSERT(*curp == NULL); |
| 772 | *curp = cur; |
| 773 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 774 | return 0; |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 775 | |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 776 | out_unreserve_dquot: |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 777 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 778 | out_root_realloc: |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 779 | xfs_iroot_realloc(ip, -1, whichfork); |
| 780 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 781 | ASSERT(ifp->if_broot == NULL); |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 782 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 783 | |
| 784 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Convert a local file to an extents file. |
| 789 | * This code is out of bounds for data forks of regular files, |
| 790 | * since the file data needs to get logged so things will stay consistent. |
| 791 | * (The bmap-level manipulations are ok, though). |
| 792 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 793 | void |
| 794 | xfs_bmap_local_to_extents_empty( |
| 795 | struct xfs_inode *ip, |
| 796 | int whichfork) |
| 797 | { |
| 798 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 799 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 800 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 801 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 802 | ASSERT(ifp->if_bytes == 0); |
| 803 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 804 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 805 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 806 | ifp->if_flags &= ~XFS_IFINLINE; |
| 807 | ifp->if_flags |= XFS_IFEXTENTS; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 808 | ifp->if_u1.if_root = NULL; |
| 809 | ifp->if_height = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 810 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 811 | } |
| 812 | |
| 813 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 814 | STATIC int /* error */ |
| 815 | xfs_bmap_local_to_extents( |
| 816 | xfs_trans_t *tp, /* transaction pointer */ |
| 817 | xfs_inode_t *ip, /* incore inode pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 818 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 819 | int *logflagsp, /* inode logging flags */ |
| 820 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 821 | void (*init_fn)(struct xfs_trans *tp, |
| 822 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 823 | struct xfs_inode *ip, |
| 824 | struct xfs_ifork *ifp)) |
| 825 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 826 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 827 | int flags; /* logging flags returned */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 828 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 829 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 830 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 831 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 832 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 833 | |
| 834 | /* |
| 835 | * We don't want to deal with the case of keeping inode data inline yet. |
| 836 | * So sending the data fork of a regular inode is invalid. |
| 837 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 838 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 839 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 840 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 841 | |
| 842 | if (!ifp->if_bytes) { |
| 843 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 844 | flags = XFS_ILOG_CORE; |
| 845 | goto done; |
| 846 | } |
| 847 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 848 | flags = 0; |
| 849 | error = 0; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 850 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 851 | memset(&args, 0, sizeof(args)); |
| 852 | args.tp = tp; |
| 853 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 854 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 855 | /* |
| 856 | * Allocate a block. We know we need only one, since the |
| 857 | * file currently fits in an inode. |
| 858 | */ |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 859 | if (tp->t_firstblock == NULLFSBLOCK) { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 860 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 861 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 862 | } else { |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 863 | args.fsbno = tp->t_firstblock; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 864 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 865 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 866 | args.total = total; |
| 867 | args.minlen = args.maxlen = args.prod = 1; |
| 868 | error = xfs_alloc_vextent(&args); |
| 869 | if (error) |
| 870 | goto done; |
| 871 | |
| 872 | /* Can't fail, the space was reserved. */ |
| 873 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 874 | ASSERT(args.len == 1); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 875 | tp->t_firstblock = args.fsbno; |
Eric Sandeen | f5b999c | 2019-06-12 09:00:00 -0700 | [diff] [blame] | 876 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 877 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 878 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 879 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 880 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 881 | * The callout is responsible for logging because the remote format |
| 882 | * might differ from the local format and thus we don't know how much to |
| 883 | * log here. Note that init_fn must also set the buffer log item type |
| 884 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 885 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 886 | init_fn(tp, bp, ip, ifp); |
| 887 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 888 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 889 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 890 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 891 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 892 | |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 893 | ifp->if_u1.if_root = NULL; |
| 894 | ifp->if_height = 0; |
| 895 | |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 896 | rec.br_startoff = 0; |
| 897 | rec.br_startblock = args.fsbno; |
| 898 | rec.br_blockcount = 1; |
| 899 | rec.br_state = XFS_EXT_NORM; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 900 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 901 | xfs_iext_insert(ip, &icur, &rec, 0); |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 902 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 903 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 904 | ip->i_d.di_nblocks = 1; |
| 905 | xfs_trans_mod_dquot_byino(tp, ip, |
| 906 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 907 | flags |= xfs_ilog_fext(whichfork); |
| 908 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 909 | done: |
| 910 | *logflagsp = flags; |
| 911 | return error; |
| 912 | } |
| 913 | |
| 914 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 916 | */ |
| 917 | STATIC int /* error */ |
| 918 | xfs_bmap_add_attrfork_btree( |
| 919 | xfs_trans_t *tp, /* transaction pointer */ |
| 920 | xfs_inode_t *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | int *flags) /* inode logging flags */ |
| 922 | { |
| 923 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 924 | int error; /* error return value */ |
| 925 | xfs_mount_t *mp; /* file system mount struct */ |
| 926 | int stat; /* newroot status */ |
| 927 | |
| 928 | mp = ip->i_mount; |
| 929 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 930 | *flags |= XFS_ILOG_DBROOT; |
| 931 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 932 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 933 | error = xfs_bmbt_lookup_first(cur, &stat); |
| 934 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 936 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 937 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 938 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | goto error0; |
| 940 | if (stat == 0) { |
| 941 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 942 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | cur->bc_private.b.allocated = 0; |
| 945 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 946 | } |
| 947 | return 0; |
| 948 | error0: |
| 949 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 950 | return error; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 955 | */ |
| 956 | STATIC int /* error */ |
| 957 | xfs_bmap_add_attrfork_extents( |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 958 | struct xfs_trans *tp, /* transaction pointer */ |
| 959 | struct xfs_inode *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | int *flags) /* inode logging flags */ |
| 961 | { |
| 962 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 963 | int error; /* error return value */ |
| 964 | |
| 965 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 966 | return 0; |
| 967 | cur = NULL; |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 968 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags, |
| 969 | XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | if (cur) { |
| 971 | cur->bc_private.b.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 972 | xfs_btree_del_cursor(cur, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | } |
| 974 | return error; |
| 975 | } |
| 976 | |
| 977 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 978 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 979 | * different data fork content type needs a different callout to do the |
| 980 | * conversion. Some are basic and only require special block initialisation |
| 981 | * callouts for the data formating, others (directories) are so specialised they |
| 982 | * handle everything themselves. |
| 983 | * |
| 984 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 985 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 986 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | */ |
| 988 | STATIC int /* error */ |
| 989 | xfs_bmap_add_attrfork_local( |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 990 | struct xfs_trans *tp, /* transaction pointer */ |
| 991 | struct xfs_inode *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | int *flags) /* inode logging flags */ |
| 993 | { |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 994 | struct xfs_da_args dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 997 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 998 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 999 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1001 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | dargs.dp = ip; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1003 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | dargs.whichfork = XFS_DATA_FORK; |
| 1005 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1006 | return xfs_dir2_sf_to_block(&dargs); |
| 1007 | } |
| 1008 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1009 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1010 | return xfs_bmap_local_to_extents(tp, ip, 1, flags, |
| 1011 | XFS_DATA_FORK, |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1012 | xfs_symlink_local_to_remote); |
| 1013 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1014 | /* should only be called for types that support local format data */ |
| 1015 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1016 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 1019 | /* Set an inode attr fork off based on the format */ |
| 1020 | int |
| 1021 | xfs_bmap_set_attrforkoff( |
| 1022 | struct xfs_inode *ip, |
| 1023 | int size, |
| 1024 | int *version) |
| 1025 | { |
| 1026 | switch (ip->i_d.di_format) { |
| 1027 | case XFS_DINODE_FMT_DEV: |
| 1028 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1029 | break; |
| 1030 | case XFS_DINODE_FMT_LOCAL: |
| 1031 | case XFS_DINODE_FMT_EXTENTS: |
| 1032 | case XFS_DINODE_FMT_BTREE: |
| 1033 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1034 | if (!ip->i_d.di_forkoff) |
| 1035 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1036 | else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version) |
| 1037 | *version = 2; |
| 1038 | break; |
| 1039 | default: |
| 1040 | ASSERT(0); |
| 1041 | return -EINVAL; |
| 1042 | } |
| 1043 | |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1048 | * Convert inode from non-attributed to attributed. |
| 1049 | * Must not be in a transaction, ip must not be locked. |
| 1050 | */ |
| 1051 | int /* error code */ |
| 1052 | xfs_bmap_add_attrfork( |
| 1053 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1054 | int size, /* space new attribute needs */ |
| 1055 | int rsvd) /* xact may use reserved blks */ |
| 1056 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1057 | xfs_mount_t *mp; /* mount structure */ |
| 1058 | xfs_trans_t *tp; /* transaction pointer */ |
| 1059 | int blks; /* space reservation */ |
| 1060 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1061 | int logflags; /* logging flags */ |
| 1062 | int error; /* error return value */ |
| 1063 | |
| 1064 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1065 | |
| 1066 | mp = ip->i_mount; |
| 1067 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1068 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1069 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1070 | |
| 1071 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1072 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1073 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1074 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1075 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1076 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1077 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1078 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1079 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1080 | if (error) |
| 1081 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1082 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1083 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1084 | if (ip->i_d.di_anextents != 0) { |
| 1085 | error = -EFSCORRUPTED; |
| 1086 | goto trans_cancel; |
| 1087 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1088 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1089 | /* |
| 1090 | * For inodes coming from pre-6.2 filesystems. |
| 1091 | */ |
| 1092 | ASSERT(ip->i_d.di_aformat == 0); |
| 1093 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1094 | } |
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); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1103 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1104 | logflags = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1105 | switch (ip->i_d.di_format) { |
| 1106 | case XFS_DINODE_FMT_LOCAL: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1107 | error = xfs_bmap_add_attrfork_local(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1108 | break; |
| 1109 | case XFS_DINODE_FMT_EXTENTS: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1110 | error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1111 | break; |
| 1112 | case XFS_DINODE_FMT_BTREE: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1113 | error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1114 | break; |
| 1115 | default: |
| 1116 | error = 0; |
| 1117 | break; |
| 1118 | } |
| 1119 | if (logflags) |
| 1120 | xfs_trans_log_inode(tp, ip, logflags); |
| 1121 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1122 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1123 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1124 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1125 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1126 | |
| 1127 | spin_lock(&mp->m_sb_lock); |
| 1128 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1129 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1130 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1131 | } |
| 1132 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1133 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1134 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1135 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1136 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1137 | if (log_sb) |
| 1138 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1139 | } |
| 1140 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1141 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1142 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1143 | return error; |
| 1144 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1145 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1146 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1147 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1148 | return error; |
| 1149 | } |
| 1150 | |
| 1151 | /* |
| 1152 | * Internal and external extent tree search functions. |
| 1153 | */ |
| 1154 | |
| 1155 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1156 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1157 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1158 | int |
| 1159 | xfs_iread_extents( |
| 1160 | struct xfs_trans *tp, |
| 1161 | struct xfs_inode *ip, |
| 1162 | int whichfork) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1163 | { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1164 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | e8e0e17 | 2017-10-19 11:06:29 -0700 | [diff] [blame] | 1165 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1166 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1167 | xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
| 1168 | struct xfs_btree_block *block = ifp->if_broot; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1169 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1170 | struct xfs_bmbt_irec new; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1171 | xfs_fsblock_t bno; |
| 1172 | struct xfs_buf *bp; |
| 1173 | xfs_extnum_t i, j; |
| 1174 | int level; |
| 1175 | __be64 *pp; |
| 1176 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1177 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1178 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1179 | |
| 1180 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 1181 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 1182 | return -EFSCORRUPTED; |
| 1183 | } |
| 1184 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1185 | /* |
| 1186 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1187 | */ |
| 1188 | level = be16_to_cpu(block->bb_level); |
Brian Foster | 6958d11 | 2019-03-17 15:21:49 -0700 | [diff] [blame] | 1189 | if (unlikely(level == 0)) { |
| 1190 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 1191 | return -EFSCORRUPTED; |
| 1192 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1193 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1194 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1195 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1196 | /* |
| 1197 | * Go down the tree until leaf level is reached, following the first |
| 1198 | * pointer (leftmost) at each level. |
| 1199 | */ |
| 1200 | while (level-- > 0) { |
Eric Sandeen | f5b999c | 2019-06-12 09:00:00 -0700 | [diff] [blame] | 1201 | error = xfs_btree_read_bufl(mp, tp, bno, &bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1202 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1203 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1204 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1205 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1206 | if (level == 0) |
| 1207 | break; |
| 1208 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1209 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1210 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 1211 | xfs_verify_fsbno(mp, bno), out_brelse); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1212 | xfs_trans_brelse(tp, bp); |
| 1213 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1214 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1215 | /* |
| 1216 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1217 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1218 | i = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1219 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1220 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1221 | /* |
| 1222 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1223 | */ |
| 1224 | for (;;) { |
| 1225 | xfs_bmbt_rec_t *frp; |
| 1226 | xfs_fsblock_t nextbno; |
| 1227 | xfs_extnum_t num_recs; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1228 | |
| 1229 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1230 | if (unlikely(i + num_recs > nextents)) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1231 | xfs_warn(ip->i_mount, |
| 1232 | "corrupt dinode %Lu, (btree extents).", |
| 1233 | (unsigned long long) ip->i_ino); |
Darrick J. Wong | 90a58f9 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1234 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
| 1235 | __func__, block, sizeof(*block), |
| 1236 | __this_address); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1237 | error = -EFSCORRUPTED; |
| 1238 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1239 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1240 | /* |
| 1241 | * Read-ahead the next leaf block, if any. |
| 1242 | */ |
| 1243 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1244 | if (nextbno != NULLFSBLOCK) |
| 1245 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1246 | &xfs_bmbt_buf_ops); |
| 1247 | /* |
| 1248 | * Copy records into the extent records. |
| 1249 | */ |
| 1250 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1251 | for (j = 0; j < num_recs; j++, frp++, i++) { |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1252 | xfs_failaddr_t fa; |
| 1253 | |
Christoph Hellwig | dac9c9b | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1254 | xfs_bmbt_disk_get_all(frp, &new); |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1255 | fa = xfs_bmap_validate_extent(ip, whichfork, &new); |
| 1256 | if (fa) { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1257 | error = -EFSCORRUPTED; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1258 | xfs_inode_verifier_error(ip, error, |
| 1259 | "xfs_iread_extents(2)", |
| 1260 | frp, sizeof(*frp), fa); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1261 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1262 | } |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1263 | xfs_iext_insert(ip, &icur, &new, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1264 | trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); |
| 1265 | xfs_iext_next(ifp, &icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1266 | } |
| 1267 | xfs_trans_brelse(tp, bp); |
| 1268 | bno = nextbno; |
| 1269 | /* |
| 1270 | * If we've reached the end, stop. |
| 1271 | */ |
| 1272 | if (bno == NULLFSBLOCK) |
| 1273 | break; |
Eric Sandeen | f5b999c | 2019-06-12 09:00:00 -0700 | [diff] [blame] | 1274 | error = xfs_btree_read_bufl(mp, tp, bno, &bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1275 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1276 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1277 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1278 | block = XFS_BUF_TO_BLOCK(bp); |
| 1279 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1280 | |
| 1281 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) { |
| 1282 | error = -EFSCORRUPTED; |
| 1283 | goto out; |
| 1284 | } |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1285 | ASSERT(i == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1286 | |
| 1287 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1288 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1289 | |
| 1290 | out_brelse: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1291 | xfs_trans_brelse(tp, bp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1292 | out: |
| 1293 | xfs_iext_destroy(ifp); |
| 1294 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1295 | } |
| 1296 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1297 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1298 | * Returns the relative block number of the first unused block(s) in the given |
| 1299 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1300 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1301 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1302 | */ |
| 1303 | int /* error */ |
| 1304 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1305 | struct xfs_trans *tp, /* transaction pointer */ |
| 1306 | struct xfs_inode *ip, /* incore inode */ |
| 1307 | xfs_extlen_t len, /* size of hole to find */ |
| 1308 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1309 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1310 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1311 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1312 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1313 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1314 | xfs_fileoff_t lastaddr = 0; |
| 1315 | xfs_fileoff_t lowest, max; |
| 1316 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1317 | |
| 1318 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1319 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1320 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1321 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1322 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1323 | *first_unused = 0; |
| 1324 | return 0; |
| 1325 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1326 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1327 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1328 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1329 | if (error) |
| 1330 | return error; |
| 1331 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1332 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1333 | lowest = max = *first_unused; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1334 | for_each_xfs_iext(ifp, &icur, &got) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1335 | /* |
| 1336 | * See if the hole before this extent will work. |
| 1337 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1338 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1339 | got.br_startoff - max >= len) |
| 1340 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1341 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1342 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1343 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1344 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1345 | *first_unused = max; |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1350 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1351 | * last_block (input value) in the file. |
| 1352 | * This is not based on i_size, it is based on the extent records. |
| 1353 | * Returns 0 for local files, as they do not have extent records. |
| 1354 | */ |
| 1355 | int /* error */ |
| 1356 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1357 | struct xfs_trans *tp, /* transaction pointer */ |
| 1358 | struct xfs_inode *ip, /* incore inode */ |
| 1359 | xfs_fileoff_t *last_block, /* last block */ |
| 1360 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1361 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1362 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1363 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1364 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1365 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1366 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1367 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1368 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1369 | *last_block = 0; |
| 1370 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1371 | case XFS_DINODE_FMT_BTREE: |
| 1372 | case XFS_DINODE_FMT_EXTENTS: |
| 1373 | break; |
| 1374 | default: |
| 1375 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1376 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1377 | |
| 1378 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1379 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1380 | if (error) |
| 1381 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1382 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1383 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1384 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1385 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1386 | return 0; |
| 1387 | } |
| 1388 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1389 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1390 | xfs_bmap_last_extent( |
| 1391 | struct xfs_trans *tp, |
| 1392 | struct xfs_inode *ip, |
| 1393 | int whichfork, |
| 1394 | struct xfs_bmbt_irec *rec, |
| 1395 | int *is_empty) |
| 1396 | { |
| 1397 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1398 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1399 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1400 | |
| 1401 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1402 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1403 | if (error) |
| 1404 | return error; |
| 1405 | } |
| 1406 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1407 | xfs_iext_last(ifp, &icur); |
| 1408 | if (!xfs_iext_get_extent(ifp, &icur, rec)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1409 | *is_empty = 1; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1410 | else |
| 1411 | *is_empty = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | * Check the last inode extent to determine whether this allocation will result |
| 1417 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1418 | * blocks at the end of the file which do not start at the previous data block, |
| 1419 | * we will try to align the new blocks at stripe unit boundaries. |
| 1420 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1421 | * 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] | 1422 | * at, or past the EOF. |
| 1423 | */ |
| 1424 | STATIC int |
| 1425 | xfs_bmap_isaeof( |
| 1426 | struct xfs_bmalloca *bma, |
| 1427 | int whichfork) |
| 1428 | { |
| 1429 | struct xfs_bmbt_irec rec; |
| 1430 | int is_empty; |
| 1431 | int error; |
| 1432 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1433 | bma->aeof = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1434 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1435 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1436 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1437 | return error; |
| 1438 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1439 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1440 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1441 | return 0; |
| 1442 | } |
| 1443 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1444 | /* |
| 1445 | * Check if we are allocation or past the last extent, or at least into |
| 1446 | * the last delayed allocated extent. |
| 1447 | */ |
| 1448 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1449 | (bma->offset >= rec.br_startoff && |
| 1450 | isnullstartblock(rec.br_startblock)); |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
| 1454 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1455 | * Returns the file-relative block number of the first block past eof in |
| 1456 | * the file. This is not based on i_size, it is based on the extent records. |
| 1457 | * Returns 0 for local files, as they do not have extent records. |
| 1458 | */ |
| 1459 | int |
| 1460 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1461 | struct xfs_inode *ip, |
| 1462 | xfs_fileoff_t *last_block, |
| 1463 | int whichfork) |
| 1464 | { |
| 1465 | struct xfs_bmbt_irec rec; |
| 1466 | int is_empty; |
| 1467 | int error; |
| 1468 | |
| 1469 | *last_block = 0; |
| 1470 | |
| 1471 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1472 | return 0; |
| 1473 | |
| 1474 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1475 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1476 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1477 | |
| 1478 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1479 | if (error || is_empty) |
| 1480 | return error; |
| 1481 | |
| 1482 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * Returns whether the selected fork of the inode has exactly one |
| 1488 | * block or not. For the data fork we check this matches di_size, |
| 1489 | * implying the file's range is 0..bsize-1. |
| 1490 | */ |
| 1491 | int /* 1=>1 block, 0=>otherwise */ |
| 1492 | xfs_bmap_one_block( |
| 1493 | xfs_inode_t *ip, /* incore inode */ |
| 1494 | int whichfork) /* data or attr fork */ |
| 1495 | { |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 1496 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1497 | int rval; /* return value */ |
| 1498 | xfs_bmbt_irec_t s; /* internal version of extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1499 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1500 | |
| 1501 | #ifndef DEBUG |
| 1502 | if (whichfork == XFS_DATA_FORK) |
| 1503 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1504 | #endif /* !DEBUG */ |
| 1505 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1506 | return 0; |
| 1507 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1508 | return 0; |
| 1509 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1510 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1511 | xfs_iext_first(ifp, &icur); |
| 1512 | xfs_iext_get_extent(ifp, &icur, &s); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1513 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1514 | if (rval && whichfork == XFS_DATA_FORK) |
| 1515 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1516 | return rval; |
| 1517 | } |
| 1518 | |
| 1519 | /* |
| 1520 | * Extent tree manipulation functions used during allocation. |
| 1521 | */ |
| 1522 | |
| 1523 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1524 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | */ |
| 1526 | STATIC int /* error */ |
| 1527 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1528 | struct xfs_bmalloca *bma, |
| 1529 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1531 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | int i; /* temp state */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 1534 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1536 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1537 | /* left is 0, right is 1, prev is 2 */ |
| 1538 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1539 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1540 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1541 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1542 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1544 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1545 | xfs_extnum_t *nextents; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1546 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1548 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1549 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1550 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1551 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1552 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1553 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1554 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1555 | ASSERT(!bma->cur || |
| 1556 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1557 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1558 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | #define LEFT r[0] |
| 1561 | #define RIGHT r[1] |
| 1562 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | |
| 1564 | /* |
| 1565 | * Set up a bunch of variables to make the tests simpler. |
| 1566 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1567 | xfs_iext_get_extent(ifp, &bma->icur, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1569 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1571 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1572 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1573 | da_old = startblockval(PREV.br_startblock); |
| 1574 | da_new = 0; |
| 1575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | /* |
| 1577 | * Set flags determining what part of the previous delayed allocation |
| 1578 | * extent is being replaced by a real allocation. |
| 1579 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1580 | if (PREV.br_startoff == new->br_startoff) |
| 1581 | state |= BMAP_LEFT_FILLING; |
| 1582 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1583 | state |= BMAP_RIGHT_FILLING; |
| 1584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | /* |
| 1586 | * Check and set flags if this segment has a left neighbor. |
| 1587 | * Don't set contiguous if the combined extent would be too large. |
| 1588 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1589 | if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1590 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1591 | if (isnullstartblock(LEFT.br_startblock)) |
| 1592 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1594 | |
| 1595 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1596 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1597 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1598 | LEFT.br_state == new->br_state && |
| 1599 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1600 | state |= BMAP_LEFT_CONTIG; |
| 1601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | /* |
| 1603 | * Check and set flags if this segment has a right neighbor. |
| 1604 | * Don't set contiguous if the combined extent would be too large. |
| 1605 | * Also check for all-three-contiguous being too large. |
| 1606 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1607 | if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1608 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1609 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1610 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1612 | |
| 1613 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1614 | new_endoff == RIGHT.br_startoff && |
| 1615 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1616 | new->br_state == RIGHT.br_state && |
| 1617 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1618 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1619 | BMAP_RIGHT_FILLING)) != |
| 1620 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1621 | BMAP_RIGHT_FILLING) || |
| 1622 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1623 | <= MAXEXTLEN)) |
| 1624 | state |= BMAP_RIGHT_CONTIG; |
| 1625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | error = 0; |
| 1627 | /* |
| 1628 | * Switch out based on the FILLING and CONTIG state bits. |
| 1629 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1630 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1631 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1632 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1633 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | /* |
| 1635 | * Filling in all of a previously delayed allocation extent. |
| 1636 | * The left and right neighbors are both contiguous with new. |
| 1637 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1638 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1639 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1640 | xfs_iext_remove(bma->ip, &bma->icur, state); |
| 1641 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1642 | xfs_iext_prev(ifp, &bma->icur); |
| 1643 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1644 | (*nextents)--; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1645 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1646 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1648 | else { |
| 1649 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1650 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1651 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1653 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1654 | error = xfs_btree_delete(bma->cur, &i); |
| 1655 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1657 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1658 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1659 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1661 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1662 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1663 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | goto done; |
| 1665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | break; |
| 1667 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1668 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | /* |
| 1670 | * Filling in all of a previously delayed allocation extent. |
| 1671 | * The left neighbor is contiguous, the right is not. |
| 1672 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1673 | old = LEFT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1674 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1675 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1676 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1677 | xfs_iext_prev(ifp, &bma->icur); |
| 1678 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1679 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1680 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | rval = XFS_ILOG_DEXT; |
| 1682 | else { |
| 1683 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1684 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1685 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1687 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1688 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1689 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | goto done; |
| 1691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | break; |
| 1693 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1694 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | /* |
| 1696 | * Filling in all of a previously delayed allocation extent. |
Dave Chinner | 9230a0b | 2018-11-19 22:50:08 -0800 | [diff] [blame] | 1697 | * The right neighbor is contiguous, the left is not. Take care |
| 1698 | * with delay -> unwritten extent allocation here because the |
| 1699 | * delalloc record we are overwriting is always written. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1701 | PREV.br_startblock = new->br_startblock; |
| 1702 | PREV.br_blockcount += RIGHT.br_blockcount; |
Dave Chinner | 9230a0b | 2018-11-19 22:50:08 -0800 | [diff] [blame] | 1703 | PREV.br_state = new->br_state; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1704 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1705 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1706 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1707 | xfs_iext_prev(ifp, &bma->icur); |
| 1708 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1709 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1710 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | rval = XFS_ILOG_DEXT; |
| 1712 | else { |
| 1713 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1714 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1715 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1717 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1718 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1719 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | goto done; |
| 1721 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | break; |
| 1723 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1724 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | /* |
| 1726 | * Filling in all of a previously delayed allocation extent. |
| 1727 | * Neither the left nor right neighbors are contiguous with |
| 1728 | * the new one. |
| 1729 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1730 | PREV.br_startblock = new->br_startblock; |
| 1731 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1732 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1733 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1734 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1735 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1737 | else { |
| 1738 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1739 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1740 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1742 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1743 | error = xfs_btree_insert(bma->cur, &i); |
| 1744 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1746 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | break; |
| 1749 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1750 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | /* |
| 1752 | * Filling in the first part of a previous delayed allocation. |
| 1753 | * The left neighbor is contiguous. |
| 1754 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1755 | old = LEFT; |
| 1756 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1757 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1758 | startblockval(PREV.br_startblock)); |
| 1759 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1760 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1761 | |
Christoph Hellwig | bf99971 | 2017-11-03 10:34:38 -0700 | [diff] [blame] | 1762 | PREV.br_blockcount = temp; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1763 | PREV.br_startoff += new->br_blockcount; |
| 1764 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1765 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1766 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1767 | xfs_iext_prev(ifp, &bma->icur); |
| 1768 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 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_DEXT; |
| 1772 | else { |
| 1773 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1774 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &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; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1777 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1778 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1779 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | goto done; |
| 1781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | break; |
| 1783 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1784 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | /* |
| 1786 | * Filling in the first part of a previous delayed allocation. |
| 1787 | * The left neighbor is not contiguous. |
| 1788 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1789 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1790 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1791 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1793 | else { |
| 1794 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1795 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1796 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1798 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1799 | error = xfs_btree_insert(bma->cur, &i); |
| 1800 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1802 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1804 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1805 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1806 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1807 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | rval |= tmp_rval; |
| 1809 | if (error) |
| 1810 | goto done; |
| 1811 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1812 | |
| 1813 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1814 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1815 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1816 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1817 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1818 | PREV.br_startoff = new_endoff; |
| 1819 | PREV.br_blockcount = temp; |
| 1820 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1821 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1822 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1823 | xfs_iext_prev(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | break; |
| 1825 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1826 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | /* |
| 1828 | * Filling in the last part of a previous delayed allocation. |
| 1829 | * The right neighbor is contiguous with the new allocation. |
| 1830 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1831 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1832 | RIGHT.br_startoff = new->br_startoff; |
| 1833 | RIGHT.br_startblock = new->br_startblock; |
| 1834 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1835 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1836 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | rval = XFS_ILOG_DEXT; |
| 1838 | else { |
| 1839 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1840 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1841 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1843 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1844 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1845 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | goto done; |
| 1847 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1848 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1849 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1850 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1851 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1852 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1853 | PREV.br_blockcount = temp; |
| 1854 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1855 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1856 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1857 | xfs_iext_next(ifp, &bma->icur); |
| 1858 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | break; |
| 1860 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1861 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | /* |
| 1863 | * Filling in the last part of a previous delayed allocation. |
| 1864 | * The right neighbor is not contiguous. |
| 1865 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1866 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1867 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1868 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1870 | else { |
| 1871 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1872 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1873 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1875 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1876 | error = xfs_btree_insert(bma->cur, &i); |
| 1877 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1879 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1881 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1882 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1883 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1884 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | rval |= tmp_rval; |
| 1886 | if (error) |
| 1887 | goto done; |
| 1888 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1889 | |
| 1890 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1891 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1892 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1893 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1894 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1895 | PREV.br_startblock = nullstartblock(da_new); |
| 1896 | PREV.br_blockcount = temp; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1897 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1898 | xfs_iext_next(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | break; |
| 1900 | |
| 1901 | case 0: |
| 1902 | /* |
| 1903 | * Filling in the middle part of a previous delayed allocation. |
| 1904 | * Contiguity is impossible here. |
| 1905 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1906 | * |
| 1907 | * We start with a delayed allocation: |
| 1908 | * |
| 1909 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1910 | * PREV @ idx |
| 1911 | * |
| 1912 | * and we are allocating: |
| 1913 | * +rrrrrrrrrrrrrrrrr+ |
| 1914 | * new |
| 1915 | * |
| 1916 | * and we set it up for insertion as: |
| 1917 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1918 | * new |
| 1919 | * PREV @ idx LEFT RIGHT |
| 1920 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1922 | old = PREV; |
| 1923 | |
| 1924 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1925 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1926 | |
| 1927 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1928 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1929 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1930 | RIGHT.br_blockcount = |
| 1931 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1932 | RIGHT.br_startblock = |
| 1933 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1934 | RIGHT.br_blockcount)); |
| 1935 | |
| 1936 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1937 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1938 | PREV.br_startblock = |
| 1939 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1940 | PREV.br_blockcount)); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1941 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1942 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1943 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1944 | xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); |
| 1945 | xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1946 | (*nextents)++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1947 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1948 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1950 | else { |
| 1951 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1952 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1953 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1955 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1956 | error = xfs_btree_insert(bma->cur, &i); |
| 1957 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1959 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1961 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1962 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1963 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1964 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | rval |= tmp_rval; |
| 1966 | if (error) |
| 1967 | goto done; |
| 1968 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1969 | |
| 1970 | da_new = startblockval(PREV.br_startblock) + |
| 1971 | startblockval(RIGHT.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | break; |
| 1973 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1974 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1975 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1976 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1977 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1978 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1979 | case BMAP_LEFT_CONTIG: |
| 1980 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | /* |
| 1982 | * These cases are all impossible. |
| 1983 | */ |
| 1984 | ASSERT(0); |
| 1985 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1986 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 1987 | /* add reverse mapping unless caller opted out */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 1988 | if (!(bma->flags & XFS_BMAPI_NORMAP)) |
| 1989 | xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 1990 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1991 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1992 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1993 | int tmp_logflags; /* partial log flag return val */ |
| 1994 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1995 | ASSERT(bma->cur == NULL); |
| 1996 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1997 | &bma->cur, da_old > 0, &tmp_logflags, |
| 1998 | whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1999 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2000 | if (error) |
| 2001 | goto done; |
| 2002 | } |
| 2003 | |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 2004 | if (da_new != da_old) |
| 2005 | xfs_mod_delalloc(mp, (int64_t)da_new - da_old); |
| 2006 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2007 | if (bma->cur) { |
| 2008 | da_new += bma->cur->bc_private.b.allocated; |
| 2009 | bma->cur->bc_private.b.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2012 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2013 | if (da_new != da_old) { |
| 2014 | ASSERT(state == 0 || da_new < da_old); |
| 2015 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2016 | false); |
| 2017 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2018 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2019 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2021 | if (whichfork != XFS_COW_FORK) |
| 2022 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | return error; |
| 2024 | #undef LEFT |
| 2025 | #undef RIGHT |
| 2026 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2030 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | */ |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 2032 | int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2034 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2036 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2037 | struct xfs_iext_cursor *icur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2039 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2040 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | int i; /* temp state */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 2045 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2048 | /* left is 0, right is 1, prev is 2 */ |
| 2049 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2050 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2051 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2052 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2054 | *logflagsp = 0; |
| 2055 | |
| 2056 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2057 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2058 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2059 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2060 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2061 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | #define LEFT r[0] |
| 2064 | #define RIGHT r[1] |
| 2065 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | /* |
| 2068 | * Set up a bunch of variables to make the tests simpler. |
| 2069 | */ |
| 2070 | error = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2071 | xfs_iext_get_extent(ifp, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2072 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2074 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2075 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | /* |
| 2078 | * Set flags determining what part of the previous oldext allocation |
| 2079 | * extent is being replaced by a newext allocation. |
| 2080 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2081 | if (PREV.br_startoff == new->br_startoff) |
| 2082 | state |= BMAP_LEFT_FILLING; |
| 2083 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2084 | state |= BMAP_RIGHT_FILLING; |
| 2085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | /* |
| 2087 | * Check and set flags if this segment has a left neighbor. |
| 2088 | * Don't set contiguous if the combined extent would be too large. |
| 2089 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2090 | if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2091 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2092 | if (isnullstartblock(LEFT.br_startblock)) |
| 2093 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2095 | |
| 2096 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2097 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2098 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2099 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2100 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2101 | state |= BMAP_LEFT_CONTIG; |
| 2102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | /* |
| 2104 | * Check and set flags if this segment has a right neighbor. |
| 2105 | * Don't set contiguous if the combined extent would be too large. |
| 2106 | * Also check for all-three-contiguous being too large. |
| 2107 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2108 | if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2109 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2110 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2111 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2113 | |
| 2114 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2115 | new_endoff == RIGHT.br_startoff && |
| 2116 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2117 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2118 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2119 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2120 | BMAP_RIGHT_FILLING)) != |
| 2121 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2122 | BMAP_RIGHT_FILLING) || |
| 2123 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2124 | <= MAXEXTLEN)) |
| 2125 | state |= BMAP_RIGHT_CONTIG; |
| 2126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | /* |
| 2128 | * Switch out based on the FILLING and CONTIG state bits. |
| 2129 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2130 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2131 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2132 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2133 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | /* |
| 2135 | * Setting all of a previous oldext extent to newext. |
| 2136 | * The left and right neighbors are both contiguous with new. |
| 2137 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2138 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2139 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2140 | xfs_iext_remove(ip, icur, state); |
| 2141 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2142 | xfs_iext_prev(ifp, icur); |
| 2143 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2144 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2145 | XFS_IFORK_NEXTENTS(ip, whichfork) - 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; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2153 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2154 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2156 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2157 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2159 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2160 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2162 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
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; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2165 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2166 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2167 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | goto done; |
| 2169 | } |
| 2170 | break; |
| 2171 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2172 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | /* |
| 2174 | * Setting all of a previous oldext extent to newext. |
| 2175 | * The left neighbor is contiguous, the right is not. |
| 2176 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2177 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2178 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2179 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2180 | xfs_iext_prev(ifp, icur); |
| 2181 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2182 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2183 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | if (cur == NULL) |
| 2185 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2186 | else { |
| 2187 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2188 | error = xfs_bmbt_lookup_eq(cur, &PREV, &i); |
| 2189 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2191 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2192 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2194 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2195 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2197 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2198 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2199 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | goto done; |
| 2201 | } |
| 2202 | break; |
| 2203 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2204 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | /* |
| 2206 | * Setting all of a previous oldext extent to newext. |
| 2207 | * The right neighbor is contiguous, the left is not. |
| 2208 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2209 | PREV.br_blockcount += RIGHT.br_blockcount; |
| 2210 | PREV.br_state = new->br_state; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2211 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2212 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2213 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2214 | xfs_iext_prev(ifp, icur); |
| 2215 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2216 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2217 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2218 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | if (cur == NULL) |
| 2220 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2221 | else { |
| 2222 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2223 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2224 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2226 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2227 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2229 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2230 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2232 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2233 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2234 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | goto done; |
| 2236 | } |
| 2237 | break; |
| 2238 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2239 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | /* |
| 2241 | * Setting all of a previous oldext extent to newext. |
| 2242 | * Neither the left nor right neighbors are contiguous with |
| 2243 | * the new one. |
| 2244 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2245 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2246 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | if (cur == NULL) |
| 2249 | rval = XFS_ILOG_DEXT; |
| 2250 | else { |
| 2251 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2252 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2253 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2255 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2256 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2257 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | goto done; |
| 2259 | } |
| 2260 | break; |
| 2261 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2262 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | /* |
| 2264 | * Setting the first part of a previous oldext extent to newext. |
| 2265 | * The left neighbor is contiguous. |
| 2266 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2267 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2268 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2269 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2270 | PREV.br_startoff += new->br_blockcount; |
| 2271 | PREV.br_startblock += new->br_blockcount; |
| 2272 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2273 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2274 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2275 | xfs_iext_prev(ifp, icur); |
| 2276 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | if (cur == NULL) |
| 2279 | rval = XFS_ILOG_DEXT; |
| 2280 | else { |
| 2281 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2282 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2283 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2285 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2286 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2287 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2289 | error = xfs_btree_decrement(cur, 0, &i); |
| 2290 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2292 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2293 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | goto done; |
| 2295 | } |
| 2296 | break; |
| 2297 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2298 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | /* |
| 2300 | * Setting the first part of a previous oldext extent to newext. |
| 2301 | * The left neighbor is not contiguous. |
| 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); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2309 | xfs_iext_insert(ip, icur, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2310 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2311 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | if (cur == NULL) |
| 2313 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2314 | else { |
| 2315 | rval = XFS_ILOG_CORE; |
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; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2319 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2320 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2321 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | goto done; |
| 2323 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2324 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2326 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | } |
| 2328 | break; |
| 2329 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2330 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | /* |
| 2332 | * Setting the last part of a previous oldext extent to newext. |
| 2333 | * The right neighbor is contiguous with the new allocation. |
| 2334 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2335 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2336 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2337 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2338 | RIGHT.br_startoff = new->br_startoff; |
| 2339 | RIGHT.br_startblock = new->br_startblock; |
| 2340 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2341 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2342 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2343 | xfs_iext_next(ifp, icur); |
| 2344 | xfs_iext_update_extent(ip, state, icur, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | if (cur == NULL) |
| 2347 | rval = XFS_ILOG_DEXT; |
| 2348 | else { |
| 2349 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2350 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2351 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2353 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2354 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2355 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2357 | error = xfs_btree_increment(cur, 0, &i); |
| 2358 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2360 | error = xfs_bmbt_update(cur, &RIGHT); |
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 | } |
| 2364 | break; |
| 2365 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2366 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | /* |
| 2368 | * Setting the last part of a previous oldext extent to newext. |
| 2369 | * The right neighbor is not contiguous. |
| 2370 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2371 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2372 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2373 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2374 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2375 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2376 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2377 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2378 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2379 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | if (cur == NULL) |
| 2381 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2382 | else { |
| 2383 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2384 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2385 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2387 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2388 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2389 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2391 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2392 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2394 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2395 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2397 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | } |
| 2399 | break; |
| 2400 | |
| 2401 | case 0: |
| 2402 | /* |
| 2403 | * Setting the middle part of a previous oldext extent to |
| 2404 | * newext. Contiguity is impossible here. |
| 2405 | * One extent becomes three extents. |
| 2406 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2407 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2408 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | r[0] = *new; |
| 2411 | r[1].br_startoff = new_endoff; |
| 2412 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2413 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2415 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2416 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2417 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2418 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2419 | xfs_iext_insert(ip, icur, &r[1], state); |
| 2420 | xfs_iext_insert(ip, icur, &r[0], state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2421 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2422 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2423 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | if (cur == NULL) |
| 2425 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2426 | else { |
| 2427 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2428 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2429 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2431 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2433 | error = xfs_bmbt_update(cur, &r[1]); |
| 2434 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | goto done; |
| 2436 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2438 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2440 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2441 | /* |
| 2442 | * Reset the cursor to the position of the new extent |
| 2443 | * we are about to insert as we can't trust it after |
| 2444 | * the previous insert. |
| 2445 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2446 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2447 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2449 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2451 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2453 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | } |
| 2455 | break; |
| 2456 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2457 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2458 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2459 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2460 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2461 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2462 | case BMAP_LEFT_CONTIG: |
| 2463 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | /* |
| 2465 | * These cases are all impossible. |
| 2466 | */ |
| 2467 | ASSERT(0); |
| 2468 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2469 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2470 | /* update reverse mappings */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 2471 | xfs_rmap_convert_extent(mp, tp, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2472 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2473 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2474 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2475 | int tmp_logflags; /* partial log flag return val */ |
| 2476 | |
| 2477 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 2478 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 2479 | &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2480 | *logflagsp |= tmp_logflags; |
| 2481 | if (error) |
| 2482 | goto done; |
| 2483 | } |
| 2484 | |
| 2485 | /* clear out the allocated field, done with it now in any case. */ |
| 2486 | if (cur) { |
| 2487 | cur->bc_private.b.allocated = 0; |
| 2488 | *curp = cur; |
| 2489 | } |
| 2490 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2491 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2493 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | return error; |
| 2495 | #undef LEFT |
| 2496 | #undef RIGHT |
| 2497 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2501 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2503 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | xfs_bmap_add_extent_hole_delay( |
| 2505 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2506 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2507 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2508 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | { |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 2510 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2512 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2513 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2514 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2515 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2516 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2518 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2519 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | /* |
| 2522 | * Check and set flags if this segment has a left neighbor |
| 2523 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2524 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2525 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2526 | if (isnullstartblock(left.br_startblock)) |
| 2527 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | /* |
| 2531 | * Check and set flags if the current (right) segment exists. |
| 2532 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2533 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2534 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2535 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2536 | if (isnullstartblock(right.br_startblock)) |
| 2537 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | /* |
| 2541 | * Set contiguity flags on the left and right neighbors. |
| 2542 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2543 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2544 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2545 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2546 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2547 | state |= BMAP_LEFT_CONTIG; |
| 2548 | |
| 2549 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2550 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2551 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2552 | (!(state & BMAP_LEFT_CONTIG) || |
| 2553 | (left.br_blockcount + new->br_blockcount + |
| 2554 | right.br_blockcount <= MAXEXTLEN))) |
| 2555 | state |= BMAP_RIGHT_CONTIG; |
| 2556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | /* |
| 2558 | * Switch out based on the contiguity flags. |
| 2559 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2560 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2561 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | /* |
| 2563 | * New allocation is contiguous with delayed allocations |
| 2564 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2565 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | */ |
| 2567 | temp = left.br_blockcount + new->br_blockcount + |
| 2568 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2569 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2570 | oldlen = startblockval(left.br_startblock) + |
| 2571 | startblockval(new->br_startblock) + |
| 2572 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2573 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2574 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2575 | left.br_startblock = nullstartblock(newlen); |
| 2576 | left.br_blockcount = temp; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2577 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2578 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2579 | xfs_iext_prev(ifp, icur); |
| 2580 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | break; |
| 2582 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2583 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | /* |
| 2585 | * New allocation is contiguous with a delayed allocation |
| 2586 | * on the left. |
| 2587 | * Merge the new allocation with the left neighbor. |
| 2588 | */ |
| 2589 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2590 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2591 | oldlen = startblockval(left.br_startblock) + |
| 2592 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2593 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2594 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2595 | left.br_blockcount = temp; |
| 2596 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | 41d196f | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 2597 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2598 | xfs_iext_prev(ifp, icur); |
| 2599 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | break; |
| 2601 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2602 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | /* |
| 2604 | * New allocation is contiguous with a delayed allocation |
| 2605 | * on the right. |
| 2606 | * Merge the new allocation with the right neighbor. |
| 2607 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2609 | oldlen = startblockval(new->br_startblock) + |
| 2610 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2611 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2612 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2613 | right.br_startoff = new->br_startoff; |
| 2614 | right.br_startblock = nullstartblock(newlen); |
| 2615 | right.br_blockcount = temp; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2616 | xfs_iext_update_extent(ip, state, icur, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | break; |
| 2618 | |
| 2619 | case 0: |
| 2620 | /* |
| 2621 | * New allocation is not contiguous with another |
| 2622 | * delayed allocation. |
| 2623 | * Insert a new entry. |
| 2624 | */ |
| 2625 | oldlen = newlen = 0; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2626 | xfs_iext_insert(ip, icur, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | break; |
| 2628 | } |
| 2629 | if (oldlen != newlen) { |
| 2630 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2631 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2632 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | /* |
| 2634 | * Nothing to do for disk quota accounting here. |
| 2635 | */ |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 2636 | xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2641 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | */ |
| 2643 | STATIC int /* error */ |
| 2644 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2645 | struct xfs_trans *tp, |
| 2646 | struct xfs_inode *ip, |
| 2647 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2648 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2649 | struct xfs_btree_cur **curp, |
| 2650 | struct xfs_bmbt_irec *new, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2651 | int *logflagsp, |
| 2652 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2654 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2655 | struct xfs_mount *mp = ip->i_mount; |
| 2656 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2660 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2661 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2662 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2663 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2665 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2666 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2667 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2668 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | /* |
| 2671 | * Check and set flags if this segment has a left neighbor. |
| 2672 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2673 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2674 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2675 | if (isnullstartblock(left.br_startblock)) |
| 2676 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | /* |
| 2680 | * Check and set flags if this segment has a current value. |
| 2681 | * Not true if we're inserting into the "hole" at eof. |
| 2682 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2683 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2684 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2685 | if (isnullstartblock(right.br_startblock)) |
| 2686 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | /* |
| 2690 | * We're inserting a real allocation between "left" and "right". |
| 2691 | * Set the contiguity flags. Don't let extents get too large. |
| 2692 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2693 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2694 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2695 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2696 | left.br_state == new->br_state && |
| 2697 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2698 | state |= BMAP_LEFT_CONTIG; |
| 2699 | |
| 2700 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2701 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2702 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2703 | new->br_state == right.br_state && |
| 2704 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2705 | (!(state & BMAP_LEFT_CONTIG) || |
| 2706 | left.br_blockcount + new->br_blockcount + |
| 2707 | right.br_blockcount <= MAXEXTLEN)) |
| 2708 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2710 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | /* |
| 2712 | * Select which case we're in here, and implement it. |
| 2713 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2714 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2715 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | /* |
| 2717 | * New allocation is contiguous with real allocations on the |
| 2718 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2719 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2721 | left.br_blockcount += new->br_blockcount + right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2722 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2723 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2724 | xfs_iext_prev(ifp, icur); |
| 2725 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2726 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2727 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2728 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2729 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2730 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2731 | } else { |
| 2732 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2733 | error = xfs_bmbt_lookup_eq(cur, &right, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2734 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2735 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2736 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2737 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2738 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2739 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2740 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2741 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2742 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2743 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2744 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2745 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2746 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2747 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2749 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2751 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | /* |
| 2753 | * New allocation is contiguous with a real allocation |
| 2754 | * on the left. |
| 2755 | * Merge the new allocation with the left neighbor. |
| 2756 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2757 | old = left; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2758 | left.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 1d2e008 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2759 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2760 | xfs_iext_prev(ifp, icur); |
| 2761 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2762 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2763 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2764 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2765 | } else { |
| 2766 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2767 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2768 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2769 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2770 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2771 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2772 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2773 | goto done; |
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 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2777 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | /* |
| 2779 | * New allocation is contiguous with a real allocation |
| 2780 | * on the right. |
| 2781 | * Merge the new allocation with the right neighbor. |
| 2782 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2783 | old = right; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2784 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2785 | right.br_startoff = new->br_startoff; |
| 2786 | right.br_startblock = new->br_startblock; |
| 2787 | right.br_blockcount += new->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2788 | xfs_iext_update_extent(ip, state, icur, &right); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2789 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2790 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2791 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2792 | } else { |
| 2793 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2794 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2795 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2796 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2797 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2798 | error = xfs_bmbt_update(cur, &right); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2799 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2800 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2802 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | |
| 2804 | case 0: |
| 2805 | /* |
| 2806 | * New allocation is not contiguous with another |
| 2807 | * real allocation. |
| 2808 | * Insert a new entry. |
| 2809 | */ |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2810 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2811 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2812 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2813 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2814 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2815 | } else { |
| 2816 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2817 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2818 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2819 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2820 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2821 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2822 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2823 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2824 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2826 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2828 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2829 | /* add reverse mapping unless caller opted out */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 2830 | if (!(flags & XFS_BMAPI_NORMAP)) |
| 2831 | xfs_rmap_map_extent(tp, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2832 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2833 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2834 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2835 | int tmp_logflags; /* partial log flag return val */ |
| 2836 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2837 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 2838 | error = xfs_bmap_extents_to_btree(tp, ip, curp, 0, |
| 2839 | &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2840 | *logflagsp |= tmp_logflags; |
| 2841 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2842 | if (error) |
| 2843 | goto done; |
| 2844 | } |
| 2845 | |
| 2846 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2847 | if (cur) |
| 2848 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2849 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2850 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2851 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2852 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2853 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | } |
| 2855 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2856 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2857 | * Functions used in the extent read, allocate and remove paths |
| 2858 | */ |
| 2859 | |
| 2860 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2861 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2862 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2863 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2864 | xfs_bmap_extsize_align( |
| 2865 | xfs_mount_t *mp, |
| 2866 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2867 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2868 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2869 | int rt, /* is this a realtime inode? */ |
| 2870 | int eof, /* is extent at end-of-file? */ |
| 2871 | int delay, /* creating delalloc extent? */ |
| 2872 | int convert, /* overwriting unwritten extent? */ |
| 2873 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2874 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2875 | { |
| 2876 | xfs_fileoff_t orig_off; /* original offset */ |
| 2877 | xfs_extlen_t orig_alen; /* original length */ |
| 2878 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2879 | xfs_fileoff_t nexto; /* next file offset */ |
| 2880 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2881 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2882 | xfs_extlen_t align_alen; /* temp for length */ |
| 2883 | xfs_extlen_t temp; /* temp for calculations */ |
| 2884 | |
| 2885 | if (convert) |
| 2886 | return 0; |
| 2887 | |
| 2888 | orig_off = align_off = *offp; |
| 2889 | orig_alen = align_alen = *lenp; |
| 2890 | orig_end = orig_off + orig_alen; |
| 2891 | |
| 2892 | /* |
| 2893 | * If this request overlaps an existing extent, then don't |
| 2894 | * attempt to perform any additional alignment. |
| 2895 | */ |
| 2896 | if (!delay && !eof && |
| 2897 | (orig_off >= gotp->br_startoff) && |
| 2898 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2899 | return 0; |
| 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | * If the file offset is unaligned vs. the extent size |
| 2904 | * we need to align it. This will be possible unless |
| 2905 | * the file was previously written with a kernel that didn't |
| 2906 | * perform this alignment, or if a truncate shot us in the |
| 2907 | * foot. |
| 2908 | */ |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 2909 | div_u64_rem(orig_off, extsz, &temp); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2910 | if (temp) { |
| 2911 | align_alen += temp; |
| 2912 | align_off -= temp; |
| 2913 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2914 | |
| 2915 | /* Same adjustment for the end of the requested area. */ |
| 2916 | temp = (align_alen % extsz); |
| 2917 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2918 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2919 | |
| 2920 | /* |
| 2921 | * For large extent hint sizes, the aligned extent might be larger than |
| 2922 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 2923 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 2924 | * short allocation just fine, so it is safe to do this. We only want to |
| 2925 | * do it when we are forced to, though, because it means more allocation |
| 2926 | * operations are required. |
| 2927 | */ |
| 2928 | while (align_alen > MAXEXTLEN) |
| 2929 | align_alen -= extsz; |
| 2930 | ASSERT(align_alen <= MAXEXTLEN); |
| 2931 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2932 | /* |
| 2933 | * If the previous block overlaps with this proposed allocation |
| 2934 | * then move the start forward without adjusting the length. |
| 2935 | */ |
| 2936 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2937 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2938 | prevo = prevp->br_startoff; |
| 2939 | else |
| 2940 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2941 | } else |
| 2942 | prevo = 0; |
| 2943 | if (align_off != orig_off && align_off < prevo) |
| 2944 | align_off = prevo; |
| 2945 | /* |
| 2946 | * If the next block overlaps with this proposed allocation |
| 2947 | * then move the start back without adjusting the length, |
| 2948 | * but not before offset 0. |
| 2949 | * This may of course make the start overlap previous block, |
| 2950 | * and if we hit the offset 0 limit then the next block |
| 2951 | * can still overlap too. |
| 2952 | */ |
| 2953 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2954 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2955 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2956 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2957 | else |
| 2958 | nexto = gotp->br_startoff; |
| 2959 | } else |
| 2960 | nexto = NULLFILEOFF; |
| 2961 | if (!eof && |
| 2962 | align_off + align_alen != orig_end && |
| 2963 | align_off + align_alen > nexto) |
| 2964 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2965 | /* |
| 2966 | * If we're now overlapping the next or previous extent that |
| 2967 | * means we can't fit an extsz piece in this hole. Just move |
| 2968 | * the start forward to the first valid spot and set |
| 2969 | * the length so we hit the end. |
| 2970 | */ |
| 2971 | if (align_off != orig_off && align_off < prevo) |
| 2972 | align_off = prevo; |
| 2973 | if (align_off + align_alen != orig_end && |
| 2974 | align_off + align_alen > nexto && |
| 2975 | nexto != NULLFILEOFF) { |
| 2976 | ASSERT(nexto > prevo); |
| 2977 | align_alen = nexto - align_off; |
| 2978 | } |
| 2979 | |
| 2980 | /* |
| 2981 | * If realtime, and the result isn't a multiple of the realtime |
| 2982 | * extent size we need to remove blocks until it is. |
| 2983 | */ |
| 2984 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 2985 | /* |
| 2986 | * We're not covering the original request, or |
| 2987 | * we won't be able to once we fix the length. |
| 2988 | */ |
| 2989 | if (orig_off < align_off || |
| 2990 | orig_end > align_off + align_alen || |
| 2991 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2992 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2993 | /* |
| 2994 | * Try to fix it by moving the start up. |
| 2995 | */ |
| 2996 | if (align_off + temp <= orig_off) { |
| 2997 | align_alen -= temp; |
| 2998 | align_off += temp; |
| 2999 | } |
| 3000 | /* |
| 3001 | * Try to fix it by moving the end in. |
| 3002 | */ |
| 3003 | else if (align_off + align_alen - temp >= orig_end) |
| 3004 | align_alen -= temp; |
| 3005 | /* |
| 3006 | * Set the start to the minimum then trim the length. |
| 3007 | */ |
| 3008 | else { |
| 3009 | align_alen -= orig_off - align_off; |
| 3010 | align_off = orig_off; |
| 3011 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3012 | } |
| 3013 | /* |
| 3014 | * Result doesn't cover the request, fail it. |
| 3015 | */ |
| 3016 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3017 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3018 | } else { |
| 3019 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3020 | /* see MAXEXTLEN handling above */ |
| 3021 | ASSERT(orig_end <= align_off + align_alen || |
| 3022 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | #ifdef DEBUG |
| 3026 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3027 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3028 | if (prevp->br_startoff != NULLFILEOFF) |
| 3029 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3030 | #endif |
| 3031 | |
| 3032 | *lenp = align_alen; |
| 3033 | *offp = align_off; |
| 3034 | return 0; |
| 3035 | } |
| 3036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3038 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3039 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3040 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3041 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | { |
| 3043 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3045 | xfs_mount_t *mp; /* mount point structure */ |
| 3046 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3047 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | |
| 3049 | #define ISVALID(x,y) \ |
| 3050 | (rt ? \ |
| 3051 | (x) < mp->m_sb.sb_rblocks : \ |
| 3052 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3053 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3054 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | mp = ap->ip->i_mount; |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3057 | nullfb = ap->tp->t_firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3058 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3059 | xfs_alloc_is_userdata(ap->datatype); |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3060 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, |
| 3061 | ap->tp->t_firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | /* |
| 3063 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3064 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3066 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3067 | !isnullstartblock(ap->prev.br_startblock) && |
| 3068 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3069 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3070 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | /* |
| 3072 | * Adjust for the gap between prevp and us. |
| 3073 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3074 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3075 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3077 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3078 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | } |
| 3080 | /* |
| 3081 | * If not at eof, then compare the two neighbor blocks. |
| 3082 | * Figure out whether either one gives us a good starting point, |
| 3083 | * and pick the better one. |
| 3084 | */ |
| 3085 | else if (!ap->eof) { |
| 3086 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3087 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3088 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3089 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3090 | |
| 3091 | /* |
| 3092 | * If there's a previous (left) block, select a requested |
| 3093 | * start block based on it. |
| 3094 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3095 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3096 | !isnullstartblock(ap->prev.br_startblock) && |
| 3097 | (prevbno = ap->prev.br_startblock + |
| 3098 | ap->prev.br_blockcount) && |
| 3099 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | /* |
| 3101 | * Calculate gap to end of previous block. |
| 3102 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3103 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3104 | (ap->prev.br_startoff + |
| 3105 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | /* |
| 3107 | * Figure the startblock based on the previous block's |
| 3108 | * end and the gap size. |
| 3109 | * Heuristic! |
| 3110 | * If the gap is large relative to the piece we're |
| 3111 | * allocating, or using it gives us an invalid block |
| 3112 | * number, then just use the end of the previous block. |
| 3113 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3114 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3116 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | prevbno += adjust; |
| 3118 | else |
| 3119 | prevdiff += adjust; |
| 3120 | /* |
| 3121 | * If the firstblock forbids it, can't use it, |
| 3122 | * must use default. |
| 3123 | */ |
| 3124 | if (!rt && !nullfb && |
| 3125 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3126 | prevbno = NULLFSBLOCK; |
| 3127 | } |
| 3128 | /* |
| 3129 | * No previous block or can't follow it, just default. |
| 3130 | */ |
| 3131 | else |
| 3132 | prevbno = NULLFSBLOCK; |
| 3133 | /* |
| 3134 | * If there's a following (right) block, select a requested |
| 3135 | * start block based on it. |
| 3136 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3137 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | /* |
| 3139 | * Calculate gap to start of next block. |
| 3140 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3141 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | /* |
| 3143 | * Figure the startblock based on the next block's |
| 3144 | * start and the gap size. |
| 3145 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3146 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | /* |
| 3148 | * Heuristic! |
| 3149 | * If the gap is large relative to the piece we're |
| 3150 | * allocating, or using it gives us an invalid block |
| 3151 | * number, then just use the start of the next block |
| 3152 | * offset by our length. |
| 3153 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3154 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3156 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3157 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3158 | gotbno -= ap->length; |
| 3159 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | } else |
| 3161 | gotdiff += adjust; |
| 3162 | /* |
| 3163 | * If the firstblock forbids it, can't use it, |
| 3164 | * must use default. |
| 3165 | */ |
| 3166 | if (!rt && !nullfb && |
| 3167 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3168 | gotbno = NULLFSBLOCK; |
| 3169 | } |
| 3170 | /* |
| 3171 | * No next block, just default. |
| 3172 | */ |
| 3173 | else |
| 3174 | gotbno = NULLFSBLOCK; |
| 3175 | /* |
| 3176 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3177 | * 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] | 3178 | */ |
| 3179 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3180 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3182 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3184 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3186 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3187 | } |
| 3188 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3189 | static int |
| 3190 | xfs_bmap_longest_free_extent( |
| 3191 | struct xfs_trans *tp, |
| 3192 | xfs_agnumber_t ag, |
| 3193 | xfs_extlen_t *blen, |
| 3194 | int *notinit) |
| 3195 | { |
| 3196 | struct xfs_mount *mp = tp->t_mountp; |
| 3197 | struct xfs_perag *pag; |
| 3198 | xfs_extlen_t longest; |
| 3199 | int error = 0; |
| 3200 | |
| 3201 | pag = xfs_perag_get(mp, ag); |
| 3202 | if (!pag->pagf_init) { |
| 3203 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3204 | if (error) |
| 3205 | goto out; |
| 3206 | |
| 3207 | if (!pag->pagf_init) { |
| 3208 | *notinit = 1; |
| 3209 | goto out; |
| 3210 | } |
| 3211 | } |
| 3212 | |
Eric Sandeen | a1f6941 | 2018-04-06 10:09:42 -0700 | [diff] [blame] | 3213 | longest = xfs_alloc_longest_free_extent(pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3214 | xfs_alloc_min_freelist(mp, pag), |
| 3215 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3216 | if (*blen < longest) |
| 3217 | *blen = longest; |
| 3218 | |
| 3219 | out: |
| 3220 | xfs_perag_put(pag); |
| 3221 | return error; |
| 3222 | } |
| 3223 | |
| 3224 | static void |
| 3225 | xfs_bmap_select_minlen( |
| 3226 | struct xfs_bmalloca *ap, |
| 3227 | struct xfs_alloc_arg *args, |
| 3228 | xfs_extlen_t *blen, |
| 3229 | int notinit) |
| 3230 | { |
| 3231 | if (notinit || *blen < ap->minlen) { |
| 3232 | /* |
| 3233 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3234 | * there is space for this request. |
| 3235 | */ |
| 3236 | args->minlen = ap->minlen; |
| 3237 | } else if (*blen < args->maxlen) { |
| 3238 | /* |
| 3239 | * If the best seen length is less than the request length, |
| 3240 | * use the best as the minimum. |
| 3241 | */ |
| 3242 | args->minlen = *blen; |
| 3243 | } else { |
| 3244 | /* |
| 3245 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3246 | * as the minimum. |
| 3247 | */ |
| 3248 | args->minlen = args->maxlen; |
| 3249 | } |
| 3250 | } |
| 3251 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3252 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3253 | xfs_bmap_btalloc_nullfb( |
| 3254 | struct xfs_bmalloca *ap, |
| 3255 | struct xfs_alloc_arg *args, |
| 3256 | xfs_extlen_t *blen) |
| 3257 | { |
| 3258 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3259 | xfs_agnumber_t ag, startag; |
| 3260 | int notinit = 0; |
| 3261 | int error; |
| 3262 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3263 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3264 | args->total = ap->total; |
| 3265 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3266 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3267 | if (startag == NULLAGNUMBER) |
| 3268 | startag = ag = 0; |
| 3269 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3270 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3271 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3272 | ¬init); |
| 3273 | if (error) |
| 3274 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3275 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3276 | if (++ag == mp->m_sb.sb_agcount) |
| 3277 | ag = 0; |
| 3278 | if (ag == startag) |
| 3279 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3280 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3281 | |
| 3282 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3283 | return 0; |
| 3284 | } |
| 3285 | |
| 3286 | STATIC int |
| 3287 | xfs_bmap_btalloc_filestreams( |
| 3288 | struct xfs_bmalloca *ap, |
| 3289 | struct xfs_alloc_arg *args, |
| 3290 | xfs_extlen_t *blen) |
| 3291 | { |
| 3292 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3293 | xfs_agnumber_t ag; |
| 3294 | int notinit = 0; |
| 3295 | int error; |
| 3296 | |
| 3297 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3298 | args->total = ap->total; |
| 3299 | |
| 3300 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3301 | if (ag == NULLAGNUMBER) |
| 3302 | ag = 0; |
| 3303 | |
| 3304 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3305 | if (error) |
| 3306 | return error; |
| 3307 | |
| 3308 | if (*blen < args->maxlen) { |
| 3309 | error = xfs_filestream_new_ag(ap, &ag); |
| 3310 | if (error) |
| 3311 | return error; |
| 3312 | |
| 3313 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3314 | ¬init); |
| 3315 | if (error) |
| 3316 | return error; |
| 3317 | |
| 3318 | } |
| 3319 | |
| 3320 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3321 | |
| 3322 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3323 | * Set the failure fallback case to look in the selected AG as stream |
| 3324 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3325 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3326 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3327 | return 0; |
| 3328 | } |
| 3329 | |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3330 | /* Update all inode and quota accounting for the allocation we just did. */ |
| 3331 | static void |
| 3332 | xfs_bmap_btalloc_accounting( |
| 3333 | struct xfs_bmalloca *ap, |
| 3334 | struct xfs_alloc_arg *args) |
| 3335 | { |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3336 | if (ap->flags & XFS_BMAPI_COWFORK) { |
| 3337 | /* |
| 3338 | * COW fork blocks are in-core only and thus are treated as |
| 3339 | * in-core quota reservation (like delalloc blocks) even when |
| 3340 | * converted to real blocks. The quota reservation is not |
| 3341 | * accounted to disk until blocks are remapped to the data |
| 3342 | * fork. So if these blocks were previously delalloc, we |
| 3343 | * already have quota reservation and there's nothing to do |
| 3344 | * yet. |
| 3345 | */ |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3346 | if (ap->wasdel) { |
| 3347 | xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len); |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3348 | return; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3349 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3350 | |
| 3351 | /* |
| 3352 | * Otherwise, we've allocated blocks in a hole. The transaction |
| 3353 | * has acquired in-core quota reservation for this extent. |
| 3354 | * Rather than account these as real blocks, however, we reduce |
| 3355 | * the transaction quota reservation based on the allocation. |
| 3356 | * This essentially transfers the transaction quota reservation |
| 3357 | * to that of a delalloc extent. |
| 3358 | */ |
| 3359 | ap->ip->i_delayed_blks += args->len; |
| 3360 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS, |
| 3361 | -(long)args->len); |
| 3362 | return; |
| 3363 | } |
| 3364 | |
| 3365 | /* data/attr fork only */ |
| 3366 | ap->ip->i_d.di_nblocks += args->len; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3367 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3368 | if (ap->wasdel) { |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3369 | ap->ip->i_delayed_blks -= args->len; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3370 | xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len); |
| 3371 | } |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3372 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 3373 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT, |
| 3374 | args->len); |
| 3375 | } |
| 3376 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3377 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3378 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3379 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3380 | { |
| 3381 | xfs_mount_t *mp; /* mount point structure */ |
| 3382 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3383 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3384 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3385 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3386 | xfs_alloc_arg_t args; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3387 | xfs_fileoff_t orig_offset; |
| 3388 | xfs_extlen_t orig_length; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3389 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3390 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3391 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3392 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3393 | int tryagain; |
| 3394 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3395 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3396 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3397 | ASSERT(ap->length); |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3398 | orig_offset = ap->offset; |
| 3399 | orig_length = ap->length; |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3400 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3401 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3402 | |
| 3403 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3404 | stripe_align = 0; |
| 3405 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3406 | stripe_align = mp->m_swidth; |
| 3407 | else if (mp->m_dalign) |
| 3408 | stripe_align = mp->m_dalign; |
| 3409 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3410 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3411 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3412 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3413 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3414 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3415 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3416 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3417 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3418 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3419 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3420 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3421 | |
| 3422 | |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3423 | nullfb = ap->tp->t_firstblock == NULLFSBLOCK; |
| 3424 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, |
| 3425 | ap->tp->t_firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3426 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3427 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3428 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3429 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3430 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3431 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3432 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3433 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3434 | } |
| 3435 | } else |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3436 | ap->blkno = ap->tp->t_firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3437 | |
| 3438 | xfs_bmap_adjacent(ap); |
| 3439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3441 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | * it's in the right allocation group. |
| 3443 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3444 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | ; |
| 3446 | else |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3447 | ap->blkno = ap->tp->t_firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | * Normal allocation, done through xfs_alloc_vextent. |
| 3450 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3451 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3452 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3453 | args.tp = ap->tp; |
| 3454 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3455 | args.fsbno = ap->blkno; |
Darrick J. Wong | 7280fed | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 3456 | args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3457 | |
| 3458 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 9bb54cb | 2018-06-07 07:54:02 -0700 | [diff] [blame] | 3459 | args.maxlen = min(ap->length, mp->m_ag_max_usable); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3460 | blen = 0; |
| 3461 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3462 | /* |
| 3463 | * Search for an allocation group with a single extent large |
| 3464 | * enough for the request. If one isn't found, then adjust |
| 3465 | * the minimum allocation size to the largest space found. |
| 3466 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3467 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3468 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3469 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3470 | else |
| 3471 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3472 | if (error) |
| 3473 | return error; |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3474 | } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3475 | if (xfs_inode_is_filestream(ap->ip)) |
| 3476 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3477 | else |
| 3478 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3479 | args.total = args.minlen = ap->minlen; |
| 3480 | } else { |
| 3481 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3482 | args.total = ap->total; |
| 3483 | args.minlen = ap->minlen; |
| 3484 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3485 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3486 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3487 | args.prod = align; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3488 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3489 | if (args.mod) |
| 3490 | args.mod = args.prod - args.mod; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3491 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3492 | args.prod = 1; |
| 3493 | args.mod = 0; |
| 3494 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3495 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3496 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3497 | if (args.mod) |
| 3498 | args.mod = args.prod - args.mod; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3499 | } |
| 3500 | /* |
| 3501 | * If we are not low on available data blocks, and the |
| 3502 | * underlying logical volume manager is a stripe, and |
| 3503 | * the file offset is zero then try to allocate data |
| 3504 | * blocks on stripe unit boundary. |
| 3505 | * NOTE: ap->aeof is only set if the allocation length |
| 3506 | * is >= the stripe unit and the allocation offset is |
| 3507 | * at the end of file. |
| 3508 | */ |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3509 | if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3510 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3511 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3512 | atype = args.type; |
| 3513 | isaligned = 1; |
| 3514 | /* |
| 3515 | * Adjust for alignment |
| 3516 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3517 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3518 | args.minlen = blen - args.alignment; |
| 3519 | args.minalignslop = 0; |
| 3520 | } else { |
| 3521 | /* |
| 3522 | * First try an exact bno allocation. |
| 3523 | * If it fails then do a near or start bno |
| 3524 | * allocation with alignment turned on. |
| 3525 | */ |
| 3526 | atype = args.type; |
| 3527 | tryagain = 1; |
| 3528 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3529 | args.alignment = 1; |
| 3530 | /* |
| 3531 | * Compute the minlen+alignment for the |
| 3532 | * next case. Set slop so that the value |
| 3533 | * of minlen+alignment+slop doesn't go up |
| 3534 | * between the calls. |
| 3535 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3536 | if (blen > stripe_align && blen <= args.maxlen) |
| 3537 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3538 | else |
| 3539 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3540 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3541 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3542 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3543 | args.minlen - 1; |
| 3544 | else |
| 3545 | args.minalignslop = 0; |
| 3546 | } |
| 3547 | } else { |
| 3548 | args.alignment = 1; |
| 3549 | args.minalignslop = 0; |
| 3550 | } |
| 3551 | args.minleft = ap->minleft; |
| 3552 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3553 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3554 | args.datatype = ap->datatype; |
| 3555 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3556 | args.ip = ap->ip; |
| 3557 | |
| 3558 | error = xfs_alloc_vextent(&args); |
| 3559 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3560 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3561 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3562 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3563 | /* |
| 3564 | * Exact allocation failed. Now try with alignment |
| 3565 | * turned on. |
| 3566 | */ |
| 3567 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3568 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3569 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3570 | args.minlen = nextminlen; |
| 3571 | args.minalignslop = 0; |
| 3572 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3573 | if ((error = xfs_alloc_vextent(&args))) |
| 3574 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3575 | } |
| 3576 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3577 | /* |
| 3578 | * allocation failed, so turn off alignment and |
| 3579 | * try again. |
| 3580 | */ |
| 3581 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3582 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3583 | args.alignment = 0; |
| 3584 | if ((error = xfs_alloc_vextent(&args))) |
| 3585 | return error; |
| 3586 | } |
| 3587 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3588 | args.minlen > ap->minlen) { |
| 3589 | args.minlen = ap->minlen; |
| 3590 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3591 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3592 | if ((error = xfs_alloc_vextent(&args))) |
| 3593 | return error; |
| 3594 | } |
| 3595 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3596 | args.fsbno = 0; |
| 3597 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3598 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3599 | if ((error = xfs_alloc_vextent(&args))) |
| 3600 | return error; |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3601 | ap->tp->t_flags |= XFS_TRANS_LOWMODE; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3602 | } |
| 3603 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3604 | /* |
| 3605 | * check the allocation happened at the same or higher AG than |
| 3606 | * the first block that was allocated. |
| 3607 | */ |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3608 | ASSERT(ap->tp->t_firstblock == NULLFSBLOCK || |
| 3609 | XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <= |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3610 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3611 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3612 | ap->blkno = args.fsbno; |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3613 | if (ap->tp->t_firstblock == NULLFSBLOCK) |
| 3614 | ap->tp->t_firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3615 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3616 | ap->length = args.len; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3617 | /* |
| 3618 | * If the extent size hint is active, we tried to round the |
| 3619 | * caller's allocation request offset down to extsz and the |
| 3620 | * length up to another extsz boundary. If we found a free |
| 3621 | * extent we mapped it in starting at this new offset. If the |
| 3622 | * newly mapped space isn't long enough to cover any of the |
| 3623 | * range of offsets that was originally requested, move the |
| 3624 | * mapping up so that we can fill as much of the caller's |
| 3625 | * original request as possible. Free space is apparently |
| 3626 | * very fragmented so we're unlikely to be able to satisfy the |
| 3627 | * hints anyway. |
| 3628 | */ |
| 3629 | if (ap->length <= orig_length) |
| 3630 | ap->offset = orig_offset; |
| 3631 | else if (ap->offset + ap->length < orig_offset + orig_length) |
| 3632 | ap->offset = orig_offset + orig_length - ap->length; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3633 | xfs_bmap_btalloc_accounting(ap, &args); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3634 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3635 | ap->blkno = NULLFSBLOCK; |
| 3636 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | } |
| 3638 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | /* |
| 3642 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3643 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3644 | */ |
| 3645 | STATIC int |
| 3646 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3647 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3648 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3649 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3650 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3651 | return xfs_bmap_rtalloc(ap); |
| 3652 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | } |
| 3654 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3655 | /* Trim extent to fit a logical block range. */ |
| 3656 | void |
| 3657 | xfs_trim_extent( |
| 3658 | struct xfs_bmbt_irec *irec, |
| 3659 | xfs_fileoff_t bno, |
| 3660 | xfs_filblks_t len) |
| 3661 | { |
| 3662 | xfs_fileoff_t distance; |
| 3663 | xfs_fileoff_t end = bno + len; |
| 3664 | |
| 3665 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3666 | irec->br_startoff >= end) { |
| 3667 | irec->br_blockcount = 0; |
| 3668 | return; |
| 3669 | } |
| 3670 | |
| 3671 | if (irec->br_startoff < bno) { |
| 3672 | distance = bno - irec->br_startoff; |
| 3673 | if (isnullstartblock(irec->br_startblock)) |
| 3674 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3675 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3676 | irec->br_startblock != HOLESTARTBLOCK) |
| 3677 | irec->br_startblock += distance; |
| 3678 | irec->br_startoff += distance; |
| 3679 | irec->br_blockcount -= distance; |
| 3680 | } |
| 3681 | |
| 3682 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3683 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3684 | irec->br_blockcount -= distance; |
| 3685 | } |
| 3686 | } |
| 3687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3689 | * Trim the returned map to the required bounds |
| 3690 | */ |
| 3691 | STATIC void |
| 3692 | xfs_bmapi_trim_map( |
| 3693 | struct xfs_bmbt_irec *mval, |
| 3694 | struct xfs_bmbt_irec *got, |
| 3695 | xfs_fileoff_t *bno, |
| 3696 | xfs_filblks_t len, |
| 3697 | xfs_fileoff_t obno, |
| 3698 | xfs_fileoff_t end, |
| 3699 | int n, |
| 3700 | int flags) |
| 3701 | { |
| 3702 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3703 | got->br_startoff + got->br_blockcount <= obno) { |
| 3704 | *mval = *got; |
| 3705 | if (isnullstartblock(got->br_startblock)) |
| 3706 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3707 | return; |
| 3708 | } |
| 3709 | |
| 3710 | if (obno > *bno) |
| 3711 | *bno = obno; |
| 3712 | ASSERT((*bno >= obno) || (n == 0)); |
| 3713 | ASSERT(*bno < end); |
| 3714 | mval->br_startoff = *bno; |
| 3715 | if (isnullstartblock(got->br_startblock)) |
| 3716 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3717 | else |
| 3718 | mval->br_startblock = got->br_startblock + |
| 3719 | (*bno - got->br_startoff); |
| 3720 | /* |
| 3721 | * Return the minimum of what we got and what we asked for for |
| 3722 | * the length. We can use the len variable here because it is |
| 3723 | * modified below and we could have been there before coming |
| 3724 | * here if the first part of the allocation didn't overlap what |
| 3725 | * was asked for. |
| 3726 | */ |
| 3727 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3728 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3729 | mval->br_state = got->br_state; |
| 3730 | ASSERT(mval->br_blockcount <= len); |
| 3731 | return; |
| 3732 | } |
| 3733 | |
| 3734 | /* |
| 3735 | * Update and validate the extent map to return |
| 3736 | */ |
| 3737 | STATIC void |
| 3738 | xfs_bmapi_update_map( |
| 3739 | struct xfs_bmbt_irec **map, |
| 3740 | xfs_fileoff_t *bno, |
| 3741 | xfs_filblks_t *len, |
| 3742 | xfs_fileoff_t obno, |
| 3743 | xfs_fileoff_t end, |
| 3744 | int *n, |
| 3745 | int flags) |
| 3746 | { |
| 3747 | xfs_bmbt_irec_t *mval = *map; |
| 3748 | |
| 3749 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3750 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3751 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3752 | (mval->br_startoff < obno)); |
| 3753 | |
| 3754 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3755 | *len = end - *bno; |
| 3756 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3757 | /* update previous map with new information */ |
| 3758 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3759 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3760 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3761 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3762 | mval[-1].br_state = mval->br_state; |
| 3763 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3764 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3765 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3766 | mval->br_startblock == mval[-1].br_startblock + |
| 3767 | mval[-1].br_blockcount && |
Christoph Hellwig | c3a2f9f | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 3768 | mval[-1].br_state == mval->br_state) { |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3769 | ASSERT(mval->br_startoff == |
| 3770 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3771 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3772 | } else if (*n > 0 && |
| 3773 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3774 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3775 | mval->br_startoff == |
| 3776 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3777 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3778 | mval[-1].br_state = mval->br_state; |
| 3779 | } else if (!((*n == 0) && |
| 3780 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3781 | obno))) { |
| 3782 | mval++; |
| 3783 | (*n)++; |
| 3784 | } |
| 3785 | *map = mval; |
| 3786 | } |
| 3787 | |
| 3788 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3789 | * Map file blocks to filesystem blocks without allocation. |
| 3790 | */ |
| 3791 | int |
| 3792 | xfs_bmapi_read( |
| 3793 | struct xfs_inode *ip, |
| 3794 | xfs_fileoff_t bno, |
| 3795 | xfs_filblks_t len, |
| 3796 | struct xfs_bmbt_irec *mval, |
| 3797 | int *nmap, |
| 3798 | int flags) |
| 3799 | { |
| 3800 | struct xfs_mount *mp = ip->i_mount; |
| 3801 | struct xfs_ifork *ifp; |
| 3802 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3803 | xfs_fileoff_t obno; |
| 3804 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3805 | struct xfs_iext_cursor icur; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3806 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3807 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3808 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3809 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3810 | |
| 3811 | ASSERT(*nmap >= 1); |
| 3812 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Christoph Hellwig | c3a2f9f | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 3813 | XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3814 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3815 | |
| 3816 | if (unlikely(XFS_TEST_ERROR( |
| 3817 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3818 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 3819 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3820 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3821 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3825 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3826 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3827 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3828 | |
| 3829 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | 8612de3 | 2019-08-11 15:52:27 -0700 | [diff] [blame] | 3830 | if (!ifp) { |
| 3831 | /* No CoW fork? Return a hole. */ |
| 3832 | if (whichfork == XFS_COW_FORK) { |
| 3833 | mval->br_startoff = bno; |
| 3834 | mval->br_startblock = HOLESTARTBLOCK; |
| 3835 | mval->br_blockcount = len; |
| 3836 | mval->br_state = XFS_EXT_NORM; |
| 3837 | *nmap = 1; |
| 3838 | return 0; |
| 3839 | } |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3840 | |
Darrick J. Wong | 8612de3 | 2019-08-11 15:52:27 -0700 | [diff] [blame] | 3841 | /* |
| 3842 | * A missing attr ifork implies that the inode says we're in |
| 3843 | * extents or btree format but failed to pass the inode fork |
| 3844 | * verifier while trying to load it. Treat that as a file |
| 3845 | * corruption too. |
| 3846 | */ |
| 3847 | #ifdef DEBUG |
| 3848 | xfs_alert(mp, "%s: inode %llu missing fork %d", |
| 3849 | __func__, ip->i_ino, whichfork); |
| 3850 | #endif /* DEBUG */ |
| 3851 | return -EFSCORRUPTED; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3852 | } |
| 3853 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3854 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3855 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3856 | if (error) |
| 3857 | return error; |
| 3858 | } |
| 3859 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3860 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3861 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3862 | end = bno + len; |
| 3863 | obno = bno; |
| 3864 | |
| 3865 | while (bno < end && n < *nmap) { |
| 3866 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3867 | if (eof) |
| 3868 | got.br_startoff = end; |
| 3869 | if (got.br_startoff > bno) { |
| 3870 | /* Reading in a hole. */ |
| 3871 | mval->br_startoff = bno; |
| 3872 | mval->br_startblock = HOLESTARTBLOCK; |
| 3873 | mval->br_blockcount = |
| 3874 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3875 | mval->br_state = XFS_EXT_NORM; |
| 3876 | bno += mval->br_blockcount; |
| 3877 | len -= mval->br_blockcount; |
| 3878 | mval++; |
| 3879 | n++; |
| 3880 | continue; |
| 3881 | } |
| 3882 | |
| 3883 | /* set up the extent map to return. */ |
| 3884 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3885 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3886 | |
| 3887 | /* If we're done, stop now. */ |
| 3888 | if (bno >= end || n >= *nmap) |
| 3889 | break; |
| 3890 | |
| 3891 | /* Else go on to the next record. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3892 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3893 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3894 | } |
| 3895 | *nmap = n; |
| 3896 | return 0; |
| 3897 | } |
| 3898 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3899 | /* |
| 3900 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3901 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3902 | * |
| 3903 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3904 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3905 | * to the extent record that was inserted to the inode fork. |
| 3906 | * |
| 3907 | * Note that the allocated extent may have been merged with contiguous extents |
| 3908 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3909 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3910 | * look up the updated record in the inode fork. |
| 3911 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3912 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3913 | xfs_bmapi_reserve_delalloc( |
| 3914 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3915 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3916 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3917 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3918 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3919 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3920 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3921 | int eof) |
| 3922 | { |
| 3923 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3924 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3925 | xfs_extlen_t alen; |
| 3926 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3927 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3928 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3929 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3930 | /* |
| 3931 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3932 | * tag the inode before we return. |
| 3933 | */ |
| 3934 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3935 | if (!eof) |
| 3936 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3937 | if (prealloc && alen >= len) |
| 3938 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3939 | |
| 3940 | /* Figure out the extent size, adjust alen */ |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3941 | if (whichfork == XFS_COW_FORK) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3942 | struct xfs_bmbt_irec prev; |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3943 | xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3944 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3945 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3946 | prev.br_startoff = NULLFILEOFF; |
| 3947 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3948 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3949 | 1, 0, &aoff, &alen); |
| 3950 | ASSERT(!error); |
| 3951 | } |
| 3952 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3953 | /* |
| 3954 | * Make a transaction-less quota reservation for delayed allocation |
| 3955 | * blocks. This number gets adjusted later. We return if we haven't |
| 3956 | * allocated blocks already inside this loop. |
| 3957 | */ |
| 3958 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3959 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3960 | if (error) |
| 3961 | return error; |
| 3962 | |
| 3963 | /* |
| 3964 | * Split changing sb for alen and indlen since they could be coming |
| 3965 | * from different places. |
| 3966 | */ |
| 3967 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 3968 | ASSERT(indlen > 0); |
| 3969 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3970 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3971 | if (error) |
| 3972 | goto out_unreserve_quota; |
| 3973 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3974 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3975 | if (error) |
| 3976 | goto out_unreserve_blocks; |
| 3977 | |
| 3978 | |
| 3979 | ip->i_delayed_blks += alen; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 3980 | xfs_mod_delalloc(ip->i_mount, alen + indlen); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3981 | |
| 3982 | got->br_startoff = aoff; |
| 3983 | got->br_startblock = nullstartblock(indlen); |
| 3984 | got->br_blockcount = alen; |
| 3985 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3986 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3987 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3988 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3989 | /* |
| 3990 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 3991 | * preallocation can occur at the start or end of the extent, even when |
| 3992 | * prealloc == 0, so we must also check the aligned offset and length. |
| 3993 | */ |
| 3994 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 3995 | xfs_inode_set_eofblocks_tag(ip); |
| 3996 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 3997 | xfs_inode_set_cowblocks_tag(ip); |
| 3998 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3999 | return 0; |
| 4000 | |
| 4001 | out_unreserve_blocks: |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4002 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4003 | out_unreserve_quota: |
| 4004 | if (XFS_IS_QUOTA_ON(mp)) |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4005 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4006 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4007 | return error; |
| 4008 | } |
| 4009 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4010 | static int |
| 4011 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4012 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4013 | { |
| 4014 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4015 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4016 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4017 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4018 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4019 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4020 | ASSERT(bma->length > 0); |
| 4021 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4022 | /* |
| 4023 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4024 | * for in this bmap call but that wouldn't be as good. |
| 4025 | */ |
| 4026 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4027 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4028 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4029 | xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4030 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4031 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4032 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4033 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4034 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4038 | * Set the data type being allocated. For the data fork, the first data |
| 4039 | * in the file is treated differently to all other allocations. For the |
| 4040 | * attribute fork, we only need to ensure the allocated range is not on |
| 4041 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4042 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4043 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4044 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4045 | if (whichfork == XFS_DATA_FORK) { |
| 4046 | if (bma->offset == 0) |
| 4047 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4048 | else |
| 4049 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4050 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4051 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4052 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4053 | } |
| 4054 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4055 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4056 | |
| 4057 | /* |
| 4058 | * Only want to do the alignment at the eof if it is userdata and |
| 4059 | * allocation length is larger than a stripe unit. |
| 4060 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4061 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4062 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4063 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4064 | if (error) |
| 4065 | return error; |
| 4066 | } |
| 4067 | |
| 4068 | error = xfs_bmap_alloc(bma); |
| 4069 | if (error) |
| 4070 | return error; |
| 4071 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4072 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4073 | return 0; |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 4074 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4075 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4076 | /* |
| 4077 | * Bump the number of extents we've allocated |
| 4078 | * in this call. |
| 4079 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4080 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4081 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4082 | if (bma->cur) |
| 4083 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4084 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4085 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4086 | bma->got.br_startoff = bma->offset; |
| 4087 | bma->got.br_startblock = bma->blkno; |
| 4088 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4089 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4090 | |
| 4091 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4092 | * In the data fork, a wasdelay extent has been initialized, so |
| 4093 | * shouldn't be flagged as unwritten. |
| 4094 | * |
| 4095 | * For the cow fork, however, we convert delalloc reservations |
| 4096 | * (extents allocated for speculative preallocation) to |
| 4097 | * allocated unwritten extents, and only convert the unwritten |
| 4098 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4099 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4100 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 4101 | (bma->flags & XFS_BMAPI_PREALLOC)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4102 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4103 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4104 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4105 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4106 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4107 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4108 | whichfork, &bma->icur, &bma->cur, &bma->got, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4109 | &bma->logflags, bma->flags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4110 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4111 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4112 | if (error) |
| 4113 | return error; |
| 4114 | |
| 4115 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4116 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4117 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4118 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4119 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4120 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4121 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4122 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4123 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4124 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4125 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4126 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4127 | return 0; |
| 4128 | } |
| 4129 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4130 | STATIC int |
| 4131 | xfs_bmapi_convert_unwritten( |
| 4132 | struct xfs_bmalloca *bma, |
| 4133 | struct xfs_bmbt_irec *mval, |
| 4134 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4135 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4136 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4137 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4138 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4139 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4140 | int error; |
| 4141 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4142 | /* check if we need to do unwritten->real conversion */ |
| 4143 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4144 | (flags & XFS_BMAPI_PREALLOC)) |
| 4145 | return 0; |
| 4146 | |
| 4147 | /* check if we need to do real->unwritten conversion */ |
| 4148 | if (mval->br_state == XFS_EXT_NORM && |
| 4149 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4150 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4151 | return 0; |
| 4152 | |
| 4153 | /* |
| 4154 | * Modify (by adding) the state flag, if writing. |
| 4155 | */ |
| 4156 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4157 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4158 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4159 | bma->ip, whichfork); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4160 | } |
| 4161 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4162 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4163 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4164 | /* |
| 4165 | * Before insertion into the bmbt, zero the range being converted |
| 4166 | * if required. |
| 4167 | */ |
| 4168 | if (flags & XFS_BMAPI_ZERO) { |
| 4169 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4170 | mval->br_blockcount); |
| 4171 | if (error) |
| 4172 | return error; |
| 4173 | } |
| 4174 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4175 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4176 | &bma->icur, &bma->cur, mval, &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4177 | /* |
| 4178 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4179 | * path because the conversion might not have done so (e.g., if the |
| 4180 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4181 | * in the transaction for the sake of fsync(), even if nothing has |
| 4182 | * changed, because fsync() will not force the log for this transaction |
| 4183 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4184 | * |
| 4185 | * Note: If we're only converting cow fork extents, there aren't |
| 4186 | * 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] | 4187 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4188 | if (whichfork != XFS_COW_FORK) |
| 4189 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4190 | if (error) |
| 4191 | return error; |
| 4192 | |
| 4193 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4194 | * Update our extent pointer, given that |
| 4195 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4196 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4197 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4198 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4199 | |
| 4200 | /* |
| 4201 | * We may have combined previously unwritten space with written space, |
| 4202 | * so generate another request. |
| 4203 | */ |
| 4204 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4205 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4206 | return 0; |
| 4207 | } |
| 4208 | |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4209 | static inline xfs_extlen_t |
| 4210 | xfs_bmapi_minleft( |
| 4211 | struct xfs_trans *tp, |
| 4212 | struct xfs_inode *ip, |
| 4213 | int fork) |
| 4214 | { |
| 4215 | if (tp && tp->t_firstblock != NULLFSBLOCK) |
| 4216 | return 0; |
| 4217 | if (XFS_IFORK_FORMAT(ip, fork) != XFS_DINODE_FMT_BTREE) |
| 4218 | return 1; |
| 4219 | return be16_to_cpu(XFS_IFORK_PTR(ip, fork)->if_broot->bb_level) + 1; |
| 4220 | } |
| 4221 | |
| 4222 | /* |
| 4223 | * Log whatever the flags say, even if error. Otherwise we might miss detecting |
| 4224 | * a case where the data is changed, there's an error, and it's not logged so we |
| 4225 | * don't shutdown when we should. Don't bother logging extents/btree changes if |
| 4226 | * we converted to the other format. |
| 4227 | */ |
| 4228 | static void |
| 4229 | xfs_bmapi_finish( |
| 4230 | struct xfs_bmalloca *bma, |
| 4231 | int whichfork, |
| 4232 | int error) |
| 4233 | { |
| 4234 | if ((bma->logflags & xfs_ilog_fext(whichfork)) && |
| 4235 | XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 4236 | bma->logflags &= ~xfs_ilog_fext(whichfork); |
| 4237 | else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) && |
| 4238 | XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_BTREE) |
| 4239 | bma->logflags &= ~xfs_ilog_fbroot(whichfork); |
| 4240 | |
| 4241 | if (bma->logflags) |
| 4242 | xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags); |
| 4243 | if (bma->cur) |
| 4244 | xfs_btree_del_cursor(bma->cur, error); |
| 4245 | } |
| 4246 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4247 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4248 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4249 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4250 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4251 | * locking problems. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4253 | int |
| 4254 | xfs_bmapi_write( |
| 4255 | struct xfs_trans *tp, /* transaction pointer */ |
| 4256 | struct xfs_inode *ip, /* incore inode */ |
| 4257 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4258 | xfs_filblks_t len, /* length to map in file */ |
| 4259 | int flags, /* XFS_BMAPI_... */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4260 | xfs_extlen_t total, /* total blocks needed */ |
| 4261 | struct xfs_bmbt_irec *mval, /* output: map values */ |
Brian Foster | 6e702a5 | 2018-07-11 22:26:12 -0700 | [diff] [blame] | 4262 | int *nmap) /* i/o: mval size/count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | { |
Darrick J. Wong | 4b0bce3 | 2019-03-19 08:16:22 -0700 | [diff] [blame] | 4264 | struct xfs_bmalloca bma = { |
| 4265 | .tp = tp, |
| 4266 | .ip = ip, |
| 4267 | .total = total, |
| 4268 | }; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4269 | struct xfs_mount *mp = ip->i_mount; |
| 4270 | struct xfs_ifork *ifp; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4271 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4272 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4273 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4274 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4275 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4276 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4279 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4280 | int orig_flags; /* original flags arg value */ |
| 4281 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4282 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4283 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4284 | |
| 4285 | orig_bno = bno; |
| 4286 | orig_len = len; |
| 4287 | orig_flags = flags; |
| 4288 | orig_mval = mval; |
| 4289 | orig_nmap = *nmap; |
| 4290 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4291 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4294 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 4295 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4296 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4297 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4298 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4299 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4300 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4301 | /* zeroing is for currently only for data extents, not metadata */ |
| 4302 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4303 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4304 | /* |
| 4305 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4306 | * but it makes no sense to do both at once. This would result in |
| 4307 | * zeroing the unwritten extent twice, but it still being an |
| 4308 | * unwritten extent.... |
| 4309 | */ |
| 4310 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4311 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | if (unlikely(XFS_TEST_ERROR( |
| 4314 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4315 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4316 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4317 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4318 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4322 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4324 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4325 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4326 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4327 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4328 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4329 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4330 | if (error) |
| 4331 | goto error0; |
| 4332 | } |
| 4333 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4334 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4335 | eof = true; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4336 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4337 | bma.prev.br_startoff = NULLFILEOFF; |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4338 | bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4339 | |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4340 | n = 0; |
| 4341 | end = bno + len; |
| 4342 | obno = bno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4344 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4345 | |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4346 | /* in hole or beyond EOF? */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4347 | if (eof || bma.got.br_startoff > bno) { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4348 | /* |
| 4349 | * CoW fork conversions should /never/ hit EOF or |
| 4350 | * holes. There should always be something for us |
| 4351 | * to work on. |
| 4352 | */ |
| 4353 | ASSERT(!((flags & XFS_BMAPI_CONVERT) && |
| 4354 | (flags & XFS_BMAPI_COWFORK))); |
| 4355 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4356 | need_alloc = true; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4357 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4358 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4359 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4360 | |
| 4361 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | * First, deal with the hole before the allocated space |
| 4363 | * that we found, if any. |
| 4364 | */ |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 4365 | if (need_alloc || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4366 | bma.eof = eof; |
| 4367 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4368 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4369 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4370 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4371 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4372 | /* |
| 4373 | * There's a 32/64 bit type mismatch between the |
| 4374 | * allocation length request (which can be 64 bits in |
| 4375 | * length) and the bma length request, which is |
| 4376 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4377 | * check for 32-bit overflows and handle them here. |
| 4378 | */ |
| 4379 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4380 | bma.length = MAXEXTLEN; |
| 4381 | else |
| 4382 | bma.length = len; |
| 4383 | |
| 4384 | ASSERT(len > 0); |
| 4385 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4386 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | if (error) |
| 4388 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4389 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4390 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4391 | |
| 4392 | /* |
| 4393 | * If this is a CoW allocation, record the data in |
| 4394 | * the refcount btree for orphan recovery. |
| 4395 | */ |
Darrick J. Wong | 74b4c5d | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 4396 | if (whichfork == XFS_COW_FORK) |
| 4397 | xfs_refcount_alloc_cow_extent(tp, bma.blkno, |
| 4398 | bma.length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4400 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4401 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4402 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4403 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4405 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4406 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4407 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4408 | continue; |
| 4409 | if (error) |
| 4410 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4412 | /* update the extent map to return */ |
| 4413 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | /* |
| 4416 | * If we're done, stop now. Stop when we've allocated |
| 4417 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4418 | * the transaction may get too big. |
| 4419 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4420 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4422 | |
| 4423 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4424 | bma.prev = bma.got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4425 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4426 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4429 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 4430 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, |
| 4431 | whichfork); |
| 4432 | if (error) |
| 4433 | goto error0; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4436 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4437 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4438 | xfs_bmapi_finish(&bma, whichfork, 0); |
| 4439 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4440 | orig_nmap, *nmap); |
| 4441 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | error0: |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4443 | xfs_bmapi_finish(&bma, whichfork, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | return error; |
| 4445 | } |
| 4446 | |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4447 | /* |
| 4448 | * Convert an existing delalloc extent to real blocks based on file offset. This |
| 4449 | * attempts to allocate the entire delalloc extent and may require multiple |
| 4450 | * invocations to allocate the target offset if a large enough physical extent |
| 4451 | * is not available. |
| 4452 | */ |
| 4453 | int |
| 4454 | xfs_bmapi_convert_delalloc( |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4455 | struct xfs_inode *ip, |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4456 | int whichfork, |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4457 | xfs_fileoff_t offset_fsb, |
| 4458 | struct xfs_bmbt_irec *imap, |
| 4459 | unsigned int *seq) |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4460 | { |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4461 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4462 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4463 | struct xfs_bmalloca bma = { NULL }; |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4464 | struct xfs_trans *tp; |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4465 | int error; |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4466 | |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4467 | /* |
| 4468 | * Space for the extent and indirect blocks was reserved when the |
| 4469 | * delalloc extent was created so there's no need to do so here. |
| 4470 | */ |
| 4471 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, |
| 4472 | XFS_TRANS_RESERVE, &tp); |
| 4473 | if (error) |
| 4474 | return error; |
| 4475 | |
| 4476 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 4477 | xfs_trans_ijoin(tp, ip, 0); |
| 4478 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4479 | if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) || |
| 4480 | bma.got.br_startoff > offset_fsb) { |
| 4481 | /* |
| 4482 | * No extent found in the range we are trying to convert. This |
| 4483 | * should only happen for the COW fork, where another thread |
| 4484 | * might have moved the extent to the data fork in the meantime. |
| 4485 | */ |
| 4486 | WARN_ON_ONCE(whichfork != XFS_COW_FORK); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4487 | error = -EAGAIN; |
| 4488 | goto out_trans_cancel; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4489 | } |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4490 | |
| 4491 | /* |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4492 | * If we find a real extent here we raced with another thread converting |
| 4493 | * the extent. Just return the real extent at this offset. |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4494 | */ |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4495 | if (!isnullstartblock(bma.got.br_startblock)) { |
| 4496 | *imap = bma.got; |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4497 | *seq = READ_ONCE(ifp->if_seq); |
| 4498 | goto out_trans_cancel; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | bma.tp = tp; |
| 4502 | bma.ip = ip; |
| 4503 | bma.wasdel = true; |
| 4504 | bma.offset = bma.got.br_startoff; |
| 4505 | bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN); |
| 4506 | bma.total = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK); |
| 4507 | bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); |
| 4508 | if (whichfork == XFS_COW_FORK) |
| 4509 | bma.flags = XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC; |
| 4510 | |
| 4511 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
| 4512 | bma.prev.br_startoff = NULLFILEOFF; |
| 4513 | |
| 4514 | error = xfs_bmapi_allocate(&bma); |
| 4515 | if (error) |
| 4516 | goto out_finish; |
| 4517 | |
| 4518 | error = -ENOSPC; |
| 4519 | if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK)) |
| 4520 | goto out_finish; |
| 4521 | error = -EFSCORRUPTED; |
| 4522 | if (WARN_ON_ONCE(!bma.got.br_startblock && !XFS_IS_REALTIME_INODE(ip))) |
| 4523 | goto out_finish; |
| 4524 | |
Christoph Hellwig | 125851a | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4525 | XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length)); |
| 4526 | XFS_STATS_INC(mp, xs_xstrat_quick); |
| 4527 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4528 | ASSERT(!isnullstartblock(bma.got.br_startblock)); |
| 4529 | *imap = bma.got; |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4530 | *seq = READ_ONCE(ifp->if_seq); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4531 | |
Darrick J. Wong | 74b4c5d | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 4532 | if (whichfork == XFS_COW_FORK) |
| 4533 | xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4534 | |
| 4535 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, |
| 4536 | whichfork); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4537 | if (error) |
| 4538 | goto out_finish; |
| 4539 | |
| 4540 | xfs_bmapi_finish(&bma, whichfork, 0); |
| 4541 | error = xfs_trans_commit(tp); |
| 4542 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 4543 | return error; |
| 4544 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4545 | out_finish: |
| 4546 | xfs_bmapi_finish(&bma, whichfork, error); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4547 | out_trans_cancel: |
| 4548 | xfs_trans_cancel(tp); |
| 4549 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4550 | return error; |
| 4551 | } |
| 4552 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4553 | int |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4554 | xfs_bmapi_remap( |
| 4555 | struct xfs_trans *tp, |
| 4556 | struct xfs_inode *ip, |
| 4557 | xfs_fileoff_t bno, |
| 4558 | xfs_filblks_t len, |
| 4559 | xfs_fsblock_t startblock, |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4560 | int flags) |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4561 | { |
| 4562 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4563 | struct xfs_ifork *ifp; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4564 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4565 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4566 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4567 | int whichfork = xfs_bmapi_whichfork(flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4568 | int logflags = 0, error; |
| 4569 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4570 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4571 | ASSERT(len > 0); |
| 4572 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4573 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4574 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC | |
| 4575 | XFS_BMAPI_NORMAP))); |
| 4576 | ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) != |
| 4577 | (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4578 | |
| 4579 | if (unlikely(XFS_TEST_ERROR( |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4580 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4581 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4582 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4583 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4584 | return -EFSCORRUPTED; |
| 4585 | } |
| 4586 | |
| 4587 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4588 | return -EIO; |
| 4589 | |
| 4590 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4591 | error = xfs_iread_extents(tp, ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4592 | if (error) |
| 4593 | return error; |
| 4594 | } |
| 4595 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4596 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4597 | /* make sure we only reflink into a hole. */ |
| 4598 | ASSERT(got.br_startoff > bno); |
| 4599 | ASSERT(got.br_startoff - bno >= len); |
| 4600 | } |
| 4601 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4602 | ip->i_d.di_nblocks += len; |
| 4603 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4604 | |
| 4605 | if (ifp->if_flags & XFS_IFBROOT) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4606 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4607 | cur->bc_private.b.flags = 0; |
| 4608 | } |
| 4609 | |
| 4610 | got.br_startoff = bno; |
| 4611 | got.br_startblock = startblock; |
| 4612 | got.br_blockcount = len; |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4613 | if (flags & XFS_BMAPI_PREALLOC) |
| 4614 | got.br_state = XFS_EXT_UNWRITTEN; |
| 4615 | else |
| 4616 | got.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4617 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4618 | error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4619 | &cur, &got, &logflags, flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4620 | if (error) |
| 4621 | goto error0; |
| 4622 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 4623 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4624 | |
| 4625 | error0: |
| 4626 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4627 | logflags &= ~XFS_ILOG_DEXT; |
| 4628 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4629 | logflags &= ~XFS_ILOG_DBROOT; |
| 4630 | |
| 4631 | if (logflags) |
| 4632 | xfs_trans_log_inode(tp, ip, logflags); |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 4633 | if (cur) |
| 4634 | xfs_btree_del_cursor(cur, error); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4635 | return error; |
| 4636 | } |
| 4637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4639 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4640 | * indlen reservation must be shared across the two new extents that are left |
| 4641 | * behind. |
| 4642 | * |
| 4643 | * Given the original reservation and the worst case indlen for the two new |
| 4644 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4645 | * reservation fairly across the two new extents. If necessary, steal available |
| 4646 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4647 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4648 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4649 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4650 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4651 | xfs_bmap_split_indlen( |
| 4652 | xfs_filblks_t ores, /* original res. */ |
| 4653 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4654 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4655 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4656 | { |
| 4657 | xfs_filblks_t len1 = *indlen1; |
| 4658 | xfs_filblks_t len2 = *indlen2; |
| 4659 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4660 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4661 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4662 | |
| 4663 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4664 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4665 | * indlen for both new extents. |
| 4666 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4667 | if (ores < nres && avail) |
| 4668 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4669 | ores += stolen; |
| 4670 | |
| 4671 | /* nothing else to do if we've satisfied the new reservation */ |
| 4672 | if (ores >= nres) |
| 4673 | return stolen; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4674 | |
| 4675 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4676 | * We can't meet the total required reservation for the two extents. |
| 4677 | * Calculate the percent of the overall shortage between both extents |
| 4678 | * and apply this percentage to each of the requested indlen values. |
| 4679 | * This distributes the shortage fairly and reduces the chances that one |
| 4680 | * of the two extents is left with nothing when extents are repeatedly |
| 4681 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4682 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4683 | resfactor = (ores * 100); |
| 4684 | do_div(resfactor, nres); |
| 4685 | len1 *= resfactor; |
| 4686 | do_div(len1, 100); |
| 4687 | len2 *= resfactor; |
| 4688 | do_div(len2, 100); |
| 4689 | ASSERT(len1 + len2 <= ores); |
| 4690 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4691 | |
| 4692 | /* |
| 4693 | * Hand out the remainder to each extent. If one of the two reservations |
| 4694 | * is zero, we want to make sure that one gets a block first. The loop |
| 4695 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4696 | * is zero. |
| 4697 | */ |
| 4698 | ores -= (len1 + len2); |
| 4699 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4700 | if (ores && !len2 && *indlen2) { |
| 4701 | len2++; |
| 4702 | ores--; |
| 4703 | } |
| 4704 | while (ores) { |
| 4705 | if (len1 < *indlen1) { |
| 4706 | len1++; |
| 4707 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4708 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4709 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4710 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4711 | if (len2 < *indlen2) { |
| 4712 | len2++; |
| 4713 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4714 | } |
| 4715 | } |
| 4716 | |
| 4717 | *indlen1 = len1; |
| 4718 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4719 | |
| 4720 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4721 | } |
| 4722 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4723 | int |
| 4724 | xfs_bmap_del_extent_delay( |
| 4725 | struct xfs_inode *ip, |
| 4726 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4727 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4728 | struct xfs_bmbt_irec *got, |
| 4729 | struct xfs_bmbt_irec *del) |
| 4730 | { |
| 4731 | struct xfs_mount *mp = ip->i_mount; |
| 4732 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4733 | struct xfs_bmbt_irec new; |
| 4734 | int64_t da_old, da_new, da_diff = 0; |
| 4735 | xfs_fileoff_t del_endoff, got_endoff; |
| 4736 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4737 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4738 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4739 | bool isrt; |
| 4740 | |
| 4741 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4742 | |
| 4743 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4744 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4745 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4746 | da_old = startblockval(got->br_startblock); |
| 4747 | da_new = 0; |
| 4748 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4749 | ASSERT(del->br_blockcount > 0); |
| 4750 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4751 | ASSERT(got_endoff >= del_endoff); |
| 4752 | |
| 4753 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4754 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4755 | |
| 4756 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4757 | xfs_mod_frextents(mp, rtexts); |
| 4758 | } |
| 4759 | |
| 4760 | /* |
| 4761 | * Update the inode delalloc counter now and wait to update the |
| 4762 | * sb counters as we might have to borrow some blocks for the |
| 4763 | * indirect block accounting. |
| 4764 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4765 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4766 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4767 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4768 | if (error) |
| 4769 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4770 | ip->i_delayed_blks -= del->br_blockcount; |
| 4771 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4772 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4773 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4774 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4775 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4776 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4777 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4778 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4779 | /* |
| 4780 | * Matches the whole extent. Delete the entry. |
| 4781 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4782 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4783 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4784 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4785 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4786 | /* |
| 4787 | * Deleting the first part of the extent. |
| 4788 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4789 | got->br_startoff = del_endoff; |
| 4790 | got->br_blockcount -= del->br_blockcount; |
| 4791 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4792 | got->br_blockcount), da_old); |
| 4793 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4794 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4795 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4796 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4797 | /* |
| 4798 | * Deleting the last part of the extent. |
| 4799 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4800 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4801 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4802 | got->br_blockcount), da_old); |
| 4803 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4804 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4805 | break; |
| 4806 | case 0: |
| 4807 | /* |
| 4808 | * Deleting the middle of the extent. |
| 4809 | * |
| 4810 | * Distribute the original indlen reservation across the two new |
| 4811 | * extents. Steal blocks from the deleted extent if necessary. |
| 4812 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4813 | * Warn if either of the new indlen reservations is zero as this |
| 4814 | * can lead to delalloc problems. |
| 4815 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4816 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4817 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4818 | |
| 4819 | new.br_blockcount = got_endoff - del_endoff; |
| 4820 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4821 | |
| 4822 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4823 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4824 | del->br_blockcount); |
| 4825 | |
| 4826 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4827 | |
| 4828 | new.br_startoff = del_endoff; |
| 4829 | new.br_state = got->br_state; |
| 4830 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4831 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4832 | xfs_iext_update_extent(ip, state, icur, got); |
| 4833 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4834 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4835 | |
| 4836 | da_new = got_indlen + new_indlen - stolen; |
| 4837 | del->br_blockcount -= stolen; |
| 4838 | break; |
| 4839 | } |
| 4840 | |
| 4841 | ASSERT(da_old >= da_new); |
| 4842 | da_diff = da_old - da_new; |
| 4843 | if (!isrt) |
| 4844 | da_diff += del->br_blockcount; |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 4845 | if (da_diff) { |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4846 | xfs_mod_fdblocks(mp, da_diff, false); |
Darrick J. Wong | 9fe82b8 | 2019-04-25 18:26:22 -0700 | [diff] [blame] | 4847 | xfs_mod_delalloc(mp, -da_diff); |
| 4848 | } |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4849 | return error; |
| 4850 | } |
| 4851 | |
| 4852 | void |
| 4853 | xfs_bmap_del_extent_cow( |
| 4854 | struct xfs_inode *ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4855 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4856 | struct xfs_bmbt_irec *got, |
| 4857 | struct xfs_bmbt_irec *del) |
| 4858 | { |
| 4859 | struct xfs_mount *mp = ip->i_mount; |
| 4860 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4861 | struct xfs_bmbt_irec new; |
| 4862 | xfs_fileoff_t del_endoff, got_endoff; |
| 4863 | int state = BMAP_COWFORK; |
| 4864 | |
| 4865 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4866 | |
| 4867 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4868 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4869 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4870 | ASSERT(del->br_blockcount > 0); |
| 4871 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4872 | ASSERT(got_endoff >= del_endoff); |
| 4873 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4874 | |
| 4875 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4876 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4877 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4878 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4879 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4880 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4881 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4882 | /* |
| 4883 | * Matches the whole extent. Delete the entry. |
| 4884 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4885 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4886 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4887 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4888 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4889 | /* |
| 4890 | * Deleting the first part of the extent. |
| 4891 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4892 | got->br_startoff = del_endoff; |
| 4893 | got->br_blockcount -= del->br_blockcount; |
| 4894 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4895 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4896 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4897 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4898 | /* |
| 4899 | * Deleting the last part of the extent. |
| 4900 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4901 | got->br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4902 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4903 | break; |
| 4904 | case 0: |
| 4905 | /* |
| 4906 | * Deleting the middle of the extent. |
| 4907 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4908 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4909 | |
| 4910 | new.br_startoff = del_endoff; |
| 4911 | new.br_blockcount = got_endoff - del_endoff; |
| 4912 | new.br_state = got->br_state; |
| 4913 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4914 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4915 | xfs_iext_update_extent(ip, state, icur, got); |
| 4916 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4917 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4918 | break; |
| 4919 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 4920 | ip->i_delayed_blks -= del->br_blockcount; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4921 | } |
| 4922 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4923 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4924 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4925 | * after removing space. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4926 | */ |
| 4927 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4928 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4929 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4930 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4931 | struct xfs_iext_cursor *icur, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4932 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4933 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4934 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4935 | int whichfork, /* data or attr fork */ |
| 4936 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4937 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4938 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4939 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4940 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4941 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4942 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4943 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4944 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4945 | int i; /* temp state */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 4946 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4947 | xfs_mount_t *mp; /* mount structure */ |
| 4948 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4949 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4950 | /* REFERENCED */ |
| 4951 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4952 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4953 | struct xfs_bmbt_irec old; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4954 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4955 | mp = ip->i_mount; |
| 4956 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4957 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4958 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4959 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4960 | xfs_iext_get_extent(ifp, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4961 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4962 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4963 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4964 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4965 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4966 | qfield = 0; |
| 4967 | error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4968 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4969 | /* |
| 4970 | * If it's the case where the directory code is running with no block |
| 4971 | * reservation, and the deleted block is in the middle of its extent, |
| 4972 | * and the resulting insert of an extent would cause transformation to |
| 4973 | * btree format, then reject it. The calling code will then swap blocks |
| 4974 | * around instead. We have to do this now, rather than waiting for the |
| 4975 | * conversion to btree format, since the transaction will be dirty then. |
| 4976 | */ |
| 4977 | if (tp->t_blk_res == 0 && |
| 4978 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 4979 | XFS_IFORK_NEXTENTS(ip, whichfork) >= |
| 4980 | XFS_IFORK_MAXEXT(ip, whichfork) && |
| 4981 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 4982 | return -ENOSPC; |
| 4983 | |
| 4984 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4985 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4986 | xfs_fsblock_t bno; |
| 4987 | xfs_filblks_t len; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 4988 | xfs_extlen_t mod; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4989 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 4990 | bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize, |
| 4991 | &mod); |
| 4992 | ASSERT(mod == 0); |
| 4993 | len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize, |
| 4994 | &mod); |
| 4995 | ASSERT(mod == 0); |
| 4996 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4997 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4998 | if (error) |
| 4999 | goto done; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5000 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5001 | nblks = len * mp->m_sb.sb_rextsize; |
| 5002 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 5003 | } else { |
| 5004 | do_fx = 1; |
| 5005 | nblks = del->br_blockcount; |
| 5006 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 5007 | } |
| 5008 | |
| 5009 | del_endblock = del->br_startblock + del->br_blockcount; |
| 5010 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5011 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5012 | if (error) |
| 5013 | goto done; |
| 5014 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5015 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 5016 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5017 | if (got.br_startoff == del->br_startoff) |
| 5018 | state |= BMAP_LEFT_FILLING; |
| 5019 | if (got_endoff == del_endoff) |
| 5020 | state |= BMAP_RIGHT_FILLING; |
| 5021 | |
| 5022 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 5023 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5024 | /* |
| 5025 | * Matches the whole extent. Delete the entry. |
| 5026 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5027 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5028 | xfs_iext_prev(ifp, icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5029 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5030 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5031 | flags |= XFS_ILOG_CORE; |
| 5032 | if (!cur) { |
| 5033 | flags |= xfs_ilog_fext(whichfork); |
| 5034 | break; |
| 5035 | } |
| 5036 | if ((error = xfs_btree_delete(cur, &i))) |
| 5037 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5038 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5039 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5040 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5041 | /* |
| 5042 | * Deleting the first part of the extent. |
| 5043 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5044 | got.br_startoff = del_endoff; |
| 5045 | got.br_startblock = del_endblock; |
| 5046 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5047 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5048 | if (!cur) { |
| 5049 | flags |= xfs_ilog_fext(whichfork); |
| 5050 | break; |
| 5051 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5052 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5053 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5054 | goto done; |
| 5055 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5056 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5057 | /* |
| 5058 | * Deleting the last part of the extent. |
| 5059 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5060 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5061 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5062 | if (!cur) { |
| 5063 | flags |= xfs_ilog_fext(whichfork); |
| 5064 | break; |
| 5065 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5066 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5067 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5068 | goto done; |
| 5069 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5070 | case 0: |
| 5071 | /* |
| 5072 | * Deleting the middle of the extent. |
| 5073 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5074 | old = got; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5075 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5076 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5077 | xfs_iext_update_extent(ip, state, icur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5078 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5079 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5080 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5081 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5082 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5083 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5084 | flags |= XFS_ILOG_CORE; |
| 5085 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5086 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5087 | if (error) |
| 5088 | goto done; |
| 5089 | error = xfs_btree_increment(cur, 0, &i); |
| 5090 | if (error) |
| 5091 | goto done; |
| 5092 | cur->bc_rec.b = new; |
| 5093 | error = xfs_btree_insert(cur, &i); |
| 5094 | if (error && error != -ENOSPC) |
| 5095 | goto done; |
| 5096 | /* |
| 5097 | * If get no-space back from btree insert, it tried a |
| 5098 | * split, and we have a zero block reservation. Fix up |
| 5099 | * our state and return the error. |
| 5100 | */ |
| 5101 | if (error == -ENOSPC) { |
| 5102 | /* |
| 5103 | * Reset the cursor, don't trust it after any |
| 5104 | * insert operation. |
| 5105 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5106 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5107 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5108 | goto done; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5109 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5110 | /* |
| 5111 | * Update the btree record back |
| 5112 | * to the original value. |
| 5113 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5114 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5115 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5116 | goto done; |
| 5117 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5118 | * Reset the extent record back |
| 5119 | * to the original value. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5120 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5121 | xfs_iext_update_extent(ip, state, icur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5122 | flags = 0; |
| 5123 | error = -ENOSPC; |
| 5124 | goto done; |
| 5125 | } |
| 5126 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5127 | } else |
| 5128 | flags |= xfs_ilog_fext(whichfork); |
| 5129 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5130 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5131 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5132 | xfs_iext_insert(ip, icur, &new, state); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5133 | break; |
| 5134 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5135 | |
| 5136 | /* remove reverse mapping */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5137 | xfs_rmap_unmap_extent(tp, ip, whichfork, del); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5138 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5139 | /* |
| 5140 | * If we need to, add to list of extents to delete. |
| 5141 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5142 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5143 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
Darrick J. Wong | 74b4c5d | 2019-08-26 17:06:04 -0700 | [diff] [blame] | 5144 | xfs_refcount_decrease_extent(tp, del); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5145 | } else { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5146 | __xfs_bmap_add_free(tp, del->br_startblock, |
Brian Foster | 4e52933 | 2018-05-10 09:35:42 -0700 | [diff] [blame] | 5147 | del->br_blockcount, NULL, |
| 5148 | (bflags & XFS_BMAPI_NODISCARD) || |
| 5149 | del->br_state == XFS_EXT_UNWRITTEN); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5150 | } |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5151 | } |
| 5152 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5153 | /* |
| 5154 | * Adjust inode # blocks in the file. |
| 5155 | */ |
| 5156 | if (nblks) |
| 5157 | ip->i_d.di_nblocks -= nblks; |
| 5158 | /* |
| 5159 | * Adjust quota data. |
| 5160 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5161 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5162 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5163 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5164 | done: |
| 5165 | *logflagsp = flags; |
| 5166 | return error; |
| 5167 | } |
| 5168 | |
| 5169 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | * Unmap (remove) blocks from a file. |
| 5171 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5172 | * that value. If not all extents in the block range can be removed then |
| 5173 | * *done is set. |
| 5174 | */ |
| 5175 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5176 | __xfs_bunmapi( |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5177 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5179 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5180 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | int flags, /* misc flags */ |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 5182 | xfs_extnum_t nexts) /* number of extents max */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | { |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5184 | struct xfs_btree_cur *cur; /* bmap btree cursor */ |
| 5185 | struct xfs_bmbt_irec del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | int error; /* error return value */ |
| 5187 | xfs_extnum_t extno; /* extent number in list */ |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5188 | struct xfs_bmbt_irec got; /* current extent record */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 5189 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | int isrt; /* freeing in rt area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | int logflags; /* transaction logging flags */ |
| 5192 | xfs_extlen_t mod; /* rt extent offset */ |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5193 | struct xfs_mount *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | int tmp_logflags; /* partial logging flags */ |
| 5195 | int wasdel; /* was a delayed alloc extent */ |
| 5196 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5198 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5199 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5200 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5201 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5202 | struct xfs_iext_cursor icur; |
| 5203 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5205 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5206 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5207 | whichfork = xfs_bmapi_whichfork(flags); |
| 5208 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5210 | if (unlikely( |
| 5211 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5212 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5213 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5214 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5215 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | } |
| 5217 | mp = ip->i_mount; |
| 5218 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5219 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5220 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5221 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | ASSERT(len > 0); |
| 5223 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5224 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5225 | /* |
| 5226 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5227 | * blowing out the transaction with a mix of EFIs and reflink |
| 5228 | * adjustments. |
| 5229 | */ |
Darrick J. Wong | 8c57b88 | 2017-12-10 18:03:53 -0800 | [diff] [blame] | 5230 | if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5231 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5232 | else |
| 5233 | max_len = len; |
| 5234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5236 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5237 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5238 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5239 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | return 0; |
| 5241 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5242 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5243 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5244 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5245 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5246 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5247 | *rlen = 0; |
| 5248 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5250 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | logflags = 0; |
| 5253 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5254 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5255 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | cur->bc_private.b.flags = 0; |
| 5257 | } else |
| 5258 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5259 | |
| 5260 | if (isrt) { |
| 5261 | /* |
| 5262 | * Synchronize by locking the bitmap inode. |
| 5263 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5264 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5265 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5266 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5267 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | extno = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5271 | while (end != (xfs_fileoff_t)-1 && end >= start && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5272 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5274 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | * Just back up to the previous extent, if so. |
| 5276 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5277 | if (got.br_startoff > end && |
| 5278 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5279 | done = true; |
| 5280 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | } |
| 5282 | /* |
| 5283 | * Is the last block of this extent before the range |
| 5284 | * we're supposed to delete? If so, we're done. |
| 5285 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5286 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5288 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5289 | break; |
| 5290 | /* |
| 5291 | * Then deal with the (possibly delayed) allocated space |
| 5292 | * we found. |
| 5293 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5295 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5296 | |
| 5297 | /* |
| 5298 | * Make sure we don't touch multiple AGF headers out of order |
| 5299 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5300 | */ |
| 5301 | if (!wasdel) { |
| 5302 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5303 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5304 | break; |
| 5305 | prev_agno = agno; |
| 5306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | if (got.br_startoff < start) { |
| 5308 | del.br_startoff = start; |
| 5309 | del.br_blockcount -= start - got.br_startoff; |
| 5310 | if (!wasdel) |
| 5311 | del.br_startblock += start - got.br_startoff; |
| 5312 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5313 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5314 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5315 | |
| 5316 | /* How much can we safely unmap? */ |
| 5317 | if (max_len < del.br_blockcount) { |
| 5318 | del.br_startoff += del.br_blockcount - max_len; |
| 5319 | if (!wasdel) |
| 5320 | del.br_startblock += del.br_blockcount - max_len; |
| 5321 | del.br_blockcount = max_len; |
| 5322 | } |
| 5323 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5324 | if (!isrt) |
| 5325 | goto delete; |
| 5326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | sum = del.br_startblock + del.br_blockcount; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5328 | div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod); |
| 5329 | if (mod) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | /* |
| 5331 | * Realtime extent not lined up at the end. |
| 5332 | * The extent could have been split into written |
| 5333 | * and unwritten pieces, or we could just be |
| 5334 | * unmapping part of it. But we can't really |
| 5335 | * get rid of part of a realtime extent. |
| 5336 | */ |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 5337 | if (del.br_state == XFS_EXT_UNWRITTEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | /* |
| 5339 | * This piece is unwritten, or we're not |
| 5340 | * using unwritten extents. Skip over it. |
| 5341 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5342 | ASSERT(end >= mod); |
| 5343 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | del.br_blockcount : mod; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5345 | if (end < got.br_startoff && |
| 5346 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5347 | done = true; |
| 5348 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | } |
| 5350 | continue; |
| 5351 | } |
| 5352 | /* |
| 5353 | * It's written, turn it unwritten. |
| 5354 | * This is better than zeroing it. |
| 5355 | */ |
| 5356 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5357 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | /* |
| 5359 | * If this spans a realtime extent boundary, |
| 5360 | * chop it back to the start of the one we end at. |
| 5361 | */ |
| 5362 | if (del.br_blockcount > mod) { |
| 5363 | del.br_startoff += del.br_blockcount - mod; |
| 5364 | del.br_startblock += del.br_blockcount - mod; |
| 5365 | del.br_blockcount = mod; |
| 5366 | } |
| 5367 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5368 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5369 | whichfork, &icur, &cur, &del, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5370 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | if (error) |
| 5372 | goto error0; |
| 5373 | goto nodelete; |
| 5374 | } |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5375 | div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod); |
| 5376 | if (mod) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | /* |
| 5378 | * Realtime extent is lined up at the end but not |
| 5379 | * at the front. We'll get rid of full extents if |
| 5380 | * we can. |
| 5381 | */ |
| 5382 | mod = mp->m_sb.sb_rextsize - mod; |
| 5383 | if (del.br_blockcount > mod) { |
| 5384 | del.br_blockcount -= mod; |
| 5385 | del.br_startoff += mod; |
| 5386 | del.br_startblock += mod; |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 5387 | } else if (del.br_startoff == start && |
| 5388 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5389 | tp->t_blk_res == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | /* |
| 5391 | * Can't make it unwritten. There isn't |
| 5392 | * a full extent here so just skip it. |
| 5393 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5394 | ASSERT(end >= del.br_blockcount); |
| 5395 | end -= del.br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5396 | if (got.br_startoff > end && |
| 5397 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5398 | done = true; |
| 5399 | break; |
| 5400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | continue; |
| 5402 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5403 | struct xfs_bmbt_irec prev; |
| 5404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | /* |
| 5406 | * This one is already unwritten. |
| 5407 | * It must have a written left neighbor. |
| 5408 | * Unwrite the killed part of that one and |
| 5409 | * try again. |
| 5410 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5411 | if (!xfs_iext_prev_extent(ifp, &icur, &prev)) |
| 5412 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5414 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | ASSERT(del.br_startblock == |
| 5416 | prev.br_startblock + prev.br_blockcount); |
| 5417 | if (prev.br_startoff < start) { |
| 5418 | mod = start - prev.br_startoff; |
| 5419 | prev.br_blockcount -= mod; |
| 5420 | prev.br_startblock += mod; |
| 5421 | prev.br_startoff = start; |
| 5422 | } |
| 5423 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5424 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5425 | ip, whichfork, &icur, &cur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5426 | &prev, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | if (error) |
| 5428 | goto error0; |
| 5429 | goto nodelete; |
| 5430 | } else { |
| 5431 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5432 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5433 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5434 | ip, whichfork, &icur, &cur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5435 | &del, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | if (error) |
| 5437 | goto error0; |
| 5438 | goto nodelete; |
| 5439 | } |
| 5440 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5441 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5442 | delete: |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5443 | if (wasdel) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5444 | error = xfs_bmap_del_extent_delay(ip, whichfork, &icur, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5445 | &got, &del); |
| 5446 | } else { |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 5447 | error = xfs_bmap_del_extent_real(ip, tp, &icur, cur, |
| 5448 | &del, &tmp_logflags, whichfork, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5449 | flags); |
| 5450 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5451 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | if (error) |
| 5454 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5455 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5456 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5457 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | /* |
| 5460 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5462 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5463 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
| 5464 | (got.br_startoff > end && |
| 5465 | !xfs_iext_prev_extent(ifp, &icur, &got))) { |
| 5466 | done = true; |
| 5467 | break; |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | extno++; |
| 5470 | } |
| 5471 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5472 | if (done || end == (xfs_fileoff_t)-1 || end < start) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5473 | *rlen = 0; |
| 5474 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5475 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | /* |
| 5478 | * Convert to a btree if necessary. |
| 5479 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5480 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 5482 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 5483 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | logflags |= tmp_logflags; |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 5485 | } else { |
| 5486 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | } |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 5489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | error0: |
| 5491 | /* |
| 5492 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5493 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5495 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5497 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5498 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5500 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | /* |
| 5502 | * Log inode even in the error case, if the transaction |
| 5503 | * is dirty we'll need to shut down the filesystem. |
| 5504 | */ |
| 5505 | if (logflags) |
| 5506 | xfs_trans_log_inode(tp, ip, logflags); |
| 5507 | if (cur) { |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 5508 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | cur->bc_private.b.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5510 | xfs_btree_del_cursor(cur, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | } |
| 5512 | return error; |
| 5513 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5514 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5515 | /* Unmap a range of a file. */ |
| 5516 | int |
| 5517 | xfs_bunmapi( |
| 5518 | xfs_trans_t *tp, |
| 5519 | struct xfs_inode *ip, |
| 5520 | xfs_fileoff_t bno, |
| 5521 | xfs_filblks_t len, |
| 5522 | int flags, |
| 5523 | xfs_extnum_t nexts, |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5524 | int *done) |
| 5525 | { |
| 5526 | int error; |
| 5527 | |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 5528 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts); |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5529 | *done = (len == 0); |
| 5530 | return error; |
| 5531 | } |
| 5532 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5533 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5534 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5535 | * extent that precedes the target hole of the shift. |
| 5536 | */ |
| 5537 | STATIC bool |
| 5538 | xfs_bmse_can_merge( |
| 5539 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5540 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5541 | xfs_fileoff_t shift) /* shift fsb */ |
| 5542 | { |
| 5543 | xfs_fileoff_t startoff; |
| 5544 | |
| 5545 | startoff = got->br_startoff - shift; |
| 5546 | |
| 5547 | /* |
| 5548 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5549 | * the preceding extent. |
| 5550 | */ |
| 5551 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5552 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5553 | (left->br_state != got->br_state) || |
| 5554 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5555 | return false; |
| 5556 | |
| 5557 | return true; |
| 5558 | } |
| 5559 | |
| 5560 | /* |
| 5561 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5562 | * hole in the file. If an extent shift would result in the extent being fully |
| 5563 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5564 | * the preceding extent rather than do the shift. |
| 5565 | * |
| 5566 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5567 | * with the provided extents via xfs_bmse_can_merge(). |
| 5568 | */ |
| 5569 | STATIC int |
| 5570 | xfs_bmse_merge( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5571 | struct xfs_trans *tp, |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5572 | struct xfs_inode *ip, |
| 5573 | int whichfork, |
| 5574 | xfs_fileoff_t shift, /* shift fsb */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5575 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5576 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5577 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5578 | struct xfs_btree_cur *cur, |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5579 | int *logflags) /* output */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5580 | { |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5581 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5582 | xfs_filblks_t blockcount; |
| 5583 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5584 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5585 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5586 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5587 | |
| 5588 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5589 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5590 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5591 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5592 | new = *left; |
| 5593 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5594 | |
| 5595 | /* |
| 5596 | * Update the on-disk extent count, the btree if necessary and log the |
| 5597 | * inode. |
| 5598 | */ |
| 5599 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5600 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5601 | *logflags |= XFS_ILOG_CORE; |
| 5602 | if (!cur) { |
| 5603 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5604 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5605 | } |
| 5606 | |
| 5607 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5608 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5609 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5610 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5611 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5612 | |
| 5613 | error = xfs_btree_delete(cur, &i); |
| 5614 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5615 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5616 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5617 | |
| 5618 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5619 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5620 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5621 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5622 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5623 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5624 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5625 | if (error) |
| 5626 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5627 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5628 | done: |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5629 | xfs_iext_remove(ip, icur, 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5630 | xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur); |
| 5631 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5632 | &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5633 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5634 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5635 | xfs_rmap_unmap_extent(tp, ip, whichfork, got); |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5636 | memcpy(&new, got, sizeof(new)); |
| 5637 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5638 | xfs_rmap_map_extent(tp, ip, whichfork, &new); |
| 5639 | return 0; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5640 | } |
| 5641 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5642 | static int |
| 5643 | xfs_bmap_shift_update_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5644 | struct xfs_trans *tp, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5645 | struct xfs_inode *ip, |
| 5646 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5647 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5648 | struct xfs_bmbt_irec *got, |
| 5649 | struct xfs_btree_cur *cur, |
| 5650 | int *logflags, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5651 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5652 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5653 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5654 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5655 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5656 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5657 | *logflags |= XFS_ILOG_CORE; |
| 5658 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5659 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5660 | |
| 5661 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5662 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5663 | if (error) |
| 5664 | return error; |
| 5665 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
| 5666 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5667 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5668 | if (error) |
| 5669 | return error; |
| 5670 | } else { |
| 5671 | *logflags |= XFS_ILOG_DEXT; |
| 5672 | } |
| 5673 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5674 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5675 | got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5676 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5677 | /* update reverse mapping */ |
Darrick J. Wong | bc46ac6 | 2019-08-26 17:06:03 -0700 | [diff] [blame] | 5678 | xfs_rmap_unmap_extent(tp, ip, whichfork, &prev); |
| 5679 | xfs_rmap_map_extent(tp, ip, whichfork, got); |
| 5680 | return 0; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5681 | } |
| 5682 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5683 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5684 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5685 | struct xfs_trans *tp, |
| 5686 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5687 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5688 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 333f950 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5689 | bool *done) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5690 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5691 | int whichfork = XFS_DATA_FORK; |
| 5692 | struct xfs_mount *mp = ip->i_mount; |
| 5693 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5694 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5695 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5696 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5697 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5698 | int error = 0; |
| 5699 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5700 | |
| 5701 | if (unlikely(XFS_TEST_ERROR( |
| 5702 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5703 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5704 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5705 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5706 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5707 | } |
| 5708 | |
| 5709 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5710 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5711 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5712 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5713 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5714 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5715 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5716 | if (error) |
| 5717 | return error; |
| 5718 | } |
| 5719 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5720 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5721 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5722 | cur->bc_private.b.flags = 0; |
| 5723 | } |
| 5724 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5725 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5726 | *done = true; |
| 5727 | goto del_cursor; |
| 5728 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5729 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5730 | del_cursor); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5731 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5732 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5733 | if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5734 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5735 | error = -EINVAL; |
| 5736 | goto del_cursor; |
| 5737 | } |
| 5738 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5739 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5740 | error = xfs_bmse_merge(tp, ip, whichfork, |
| 5741 | offset_shift_fsb, &icur, &got, &prev, |
| 5742 | cur, &logflags); |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5743 | if (error) |
| 5744 | goto del_cursor; |
| 5745 | goto done; |
| 5746 | } |
| 5747 | } else { |
| 5748 | if (got.br_startoff < offset_shift_fsb) { |
| 5749 | error = -EINVAL; |
| 5750 | goto del_cursor; |
| 5751 | } |
| 5752 | } |
| 5753 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5754 | error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got, |
| 5755 | cur, &logflags, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5756 | if (error) |
| 5757 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5758 | |
Christoph Hellwig | 4263036 | 2017-11-03 10:34:41 -0700 | [diff] [blame] | 5759 | done: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5760 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
| 5761 | *done = true; |
| 5762 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5763 | } |
| 5764 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5765 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5766 | del_cursor: |
| 5767 | if (cur) |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5768 | xfs_btree_del_cursor(cur, error); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5769 | if (logflags) |
| 5770 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5771 | return error; |
| 5772 | } |
| 5773 | |
Darrick J. Wong | f62cb48 | 2018-06-21 23:26:57 -0700 | [diff] [blame] | 5774 | /* Make sure we won't be right-shifting an extent past the maximum bound. */ |
| 5775 | int |
| 5776 | xfs_bmap_can_insert_extents( |
| 5777 | struct xfs_inode *ip, |
| 5778 | xfs_fileoff_t off, |
| 5779 | xfs_fileoff_t shift) |
| 5780 | { |
| 5781 | struct xfs_bmbt_irec got; |
| 5782 | int is_empty; |
| 5783 | int error = 0; |
| 5784 | |
| 5785 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5786 | |
| 5787 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 5788 | return -EIO; |
| 5789 | |
| 5790 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 5791 | error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty); |
| 5792 | if (!error && !is_empty && got.br_startoff >= off && |
| 5793 | ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff) |
| 5794 | error = -EINVAL; |
| 5795 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 5796 | |
| 5797 | return error; |
| 5798 | } |
| 5799 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5800 | int |
| 5801 | xfs_bmap_insert_extents( |
| 5802 | struct xfs_trans *tp, |
| 5803 | struct xfs_inode *ip, |
| 5804 | xfs_fileoff_t *next_fsb, |
| 5805 | xfs_fileoff_t offset_shift_fsb, |
| 5806 | bool *done, |
Brian Foster | 333f950 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5807 | xfs_fileoff_t stop_fsb) |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5808 | { |
| 5809 | int whichfork = XFS_DATA_FORK; |
| 5810 | struct xfs_mount *mp = ip->i_mount; |
| 5811 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5812 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5813 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5814 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5815 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5816 | int error = 0; |
| 5817 | int logflags = 0; |
| 5818 | |
| 5819 | if (unlikely(XFS_TEST_ERROR( |
| 5820 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5821 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5822 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
| 5823 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 5824 | return -EFSCORRUPTED; |
| 5825 | } |
| 5826 | |
| 5827 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5828 | return -EIO; |
| 5829 | |
| 5830 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5831 | |
| 5832 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5833 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5834 | if (error) |
| 5835 | return error; |
| 5836 | } |
| 5837 | |
| 5838 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5839 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5840 | cur->bc_private.b.flags = 0; |
| 5841 | } |
| 5842 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5843 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5844 | xfs_iext_last(ifp, &icur); |
| 5845 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5846 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5847 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5848 | goto del_cursor; |
| 5849 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5850 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5851 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5852 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5853 | goto del_cursor; |
| 5854 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5855 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5856 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5857 | del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5858 | |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5859 | if (stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5860 | error = -EIO; |
| 5861 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5862 | } |
| 5863 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5864 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5865 | if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5866 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5867 | error = -EINVAL; |
| 5868 | goto del_cursor; |
| 5869 | } |
| 5870 | |
| 5871 | /* |
| 5872 | * Unlike a left shift (which involves a hole punch), a right |
| 5873 | * shift does not modify extent neighbors in any way. We should |
| 5874 | * never find mergeable extents in this scenario. Check anyways |
| 5875 | * and warn if we encounter two extents that could be one. |
| 5876 | */ |
| 5877 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5878 | WARN_ON_ONCE(1); |
| 5879 | } |
| 5880 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5881 | error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got, |
| 5882 | cur, &logflags, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5883 | if (error) |
| 5884 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5885 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5886 | if (!xfs_iext_prev_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5887 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5888 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5889 | goto del_cursor; |
| 5890 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5891 | |
| 5892 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5893 | del_cursor: |
| 5894 | if (cur) |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5895 | xfs_btree_del_cursor(cur, error); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5896 | if (logflags) |
| 5897 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5898 | return error; |
| 5899 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5900 | |
| 5901 | /* |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5902 | * Splits an extent into two extents at split_fsb block such that it is the |
| 5903 | * first block of the current_ext. @ext is a target extent to be split. |
| 5904 | * @split_fsb is a block where the extents is split. If split_fsb lies in a |
| 5905 | * hole or the first block of extents, just return 0. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5906 | */ |
| 5907 | STATIC int |
| 5908 | xfs_bmap_split_extent_at( |
| 5909 | struct xfs_trans *tp, |
| 5910 | struct xfs_inode *ip, |
Brian Foster | 4b77a08 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5911 | xfs_fileoff_t split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5912 | { |
| 5913 | int whichfork = XFS_DATA_FORK; |
| 5914 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5915 | struct xfs_bmbt_irec got; |
| 5916 | struct xfs_bmbt_irec new; /* split extent */ |
| 5917 | struct xfs_mount *mp = ip->i_mount; |
| 5918 | struct xfs_ifork *ifp; |
| 5919 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5920 | struct xfs_iext_cursor icur; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5921 | int error = 0; |
| 5922 | int logflags = 0; |
| 5923 | int i = 0; |
| 5924 | |
| 5925 | if (unlikely(XFS_TEST_ERROR( |
| 5926 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5927 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5928 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5929 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5930 | XFS_ERRLEVEL_LOW, mp); |
| 5931 | return -EFSCORRUPTED; |
| 5932 | } |
| 5933 | |
| 5934 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5935 | return -EIO; |
| 5936 | |
| 5937 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5938 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5939 | /* Read in all the extents */ |
| 5940 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5941 | if (error) |
| 5942 | return error; |
| 5943 | } |
| 5944 | |
| 5945 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5946 | * 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] | 5947 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5948 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5949 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5950 | return 0; |
| 5951 | |
| 5952 | gotblkcnt = split_fsb - got.br_startoff; |
| 5953 | new.br_startoff = split_fsb; |
| 5954 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5955 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5956 | new.br_state = got.br_state; |
| 5957 | |
| 5958 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5959 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5960 | cur->bc_private.b.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5961 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5962 | if (error) |
| 5963 | goto del_cursor; |
| 5964 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5965 | } |
| 5966 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5967 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5968 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, |
| 5969 | &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5970 | |
| 5971 | logflags = XFS_ILOG_CORE; |
| 5972 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5973 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5974 | if (error) |
| 5975 | goto del_cursor; |
| 5976 | } else |
| 5977 | logflags |= XFS_ILOG_DEXT; |
| 5978 | |
| 5979 | /* Add new extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5980 | xfs_iext_next(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5981 | xfs_iext_insert(ip, &icur, &new, 0); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5982 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5983 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5984 | |
| 5985 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5986 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5987 | if (error) |
| 5988 | goto del_cursor; |
| 5989 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5990 | error = xfs_btree_insert(cur, &i); |
| 5991 | if (error) |
| 5992 | goto del_cursor; |
| 5993 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5994 | } |
| 5995 | |
| 5996 | /* |
| 5997 | * Convert to a btree if necessary. |
| 5998 | */ |
| 5999 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 6000 | int tmp_logflags; /* partial log flag return val */ |
| 6001 | |
| 6002 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 6003 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 6004 | &tmp_logflags, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6005 | logflags |= tmp_logflags; |
| 6006 | } |
| 6007 | |
| 6008 | del_cursor: |
| 6009 | if (cur) { |
| 6010 | cur->bc_private.b.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 6011 | xfs_btree_del_cursor(cur, error); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6012 | } |
| 6013 | |
| 6014 | if (logflags) |
| 6015 | xfs_trans_log_inode(tp, ip, logflags); |
| 6016 | return error; |
| 6017 | } |
| 6018 | |
| 6019 | int |
| 6020 | xfs_bmap_split_extent( |
| 6021 | struct xfs_inode *ip, |
| 6022 | xfs_fileoff_t split_fsb) |
| 6023 | { |
| 6024 | struct xfs_mount *mp = ip->i_mount; |
| 6025 | struct xfs_trans *tp; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6026 | int error; |
| 6027 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6028 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6029 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6030 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6031 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6032 | |
| 6033 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6034 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6035 | |
Brian Foster | 4b77a08 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 6036 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6037 | if (error) |
| 6038 | goto out; |
| 6039 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6040 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6041 | |
| 6042 | out: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6043 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6044 | return error; |
| 6045 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6046 | |
| 6047 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6048 | static bool |
| 6049 | xfs_bmap_is_update_needed( |
| 6050 | struct xfs_bmbt_irec *bmap) |
| 6051 | { |
| 6052 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6053 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6054 | } |
| 6055 | |
| 6056 | /* Record a bmap intent. */ |
| 6057 | static int |
| 6058 | __xfs_bmap_add( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6059 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6060 | enum xfs_bmap_intent_type type, |
| 6061 | struct xfs_inode *ip, |
| 6062 | int whichfork, |
| 6063 | struct xfs_bmbt_irec *bmap) |
| 6064 | { |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6065 | struct xfs_bmap_intent *bi; |
| 6066 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6067 | trace_xfs_bmap_defer(tp->t_mountp, |
| 6068 | XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock), |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6069 | type, |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6070 | XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock), |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6071 | ip->i_ino, whichfork, |
| 6072 | bmap->br_startoff, |
| 6073 | bmap->br_blockcount, |
| 6074 | bmap->br_state); |
| 6075 | |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 6076 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_NOFS); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6077 | INIT_LIST_HEAD(&bi->bi_list); |
| 6078 | bi->bi_type = type; |
| 6079 | bi->bi_owner = ip; |
| 6080 | bi->bi_whichfork = whichfork; |
| 6081 | bi->bi_bmap = *bmap; |
| 6082 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6083 | 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] | 6084 | return 0; |
| 6085 | } |
| 6086 | |
| 6087 | /* Map an extent into a file. */ |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame^] | 6088 | void |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6089 | xfs_bmap_map_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6090 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6091 | struct xfs_inode *ip, |
| 6092 | struct xfs_bmbt_irec *PREV) |
| 6093 | { |
| 6094 | if (!xfs_bmap_is_update_needed(PREV)) |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame^] | 6095 | return; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6096 | |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame^] | 6097 | __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] | 6098 | } |
| 6099 | |
| 6100 | /* Unmap an extent out of a file. */ |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame^] | 6101 | void |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6102 | xfs_bmap_unmap_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6103 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6104 | struct xfs_inode *ip, |
| 6105 | struct xfs_bmbt_irec *PREV) |
| 6106 | { |
| 6107 | if (!xfs_bmap_is_update_needed(PREV)) |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame^] | 6108 | return; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6109 | |
Darrick J. Wong | 3e08f42 | 2019-08-26 17:06:04 -0700 | [diff] [blame^] | 6110 | __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] | 6111 | } |
| 6112 | |
| 6113 | /* |
| 6114 | * Process one of the deferred bmap operations. We pass back the |
| 6115 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6116 | */ |
| 6117 | int |
| 6118 | xfs_bmap_finish_one( |
| 6119 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6120 | struct xfs_inode *ip, |
| 6121 | enum xfs_bmap_intent_type type, |
| 6122 | int whichfork, |
| 6123 | xfs_fileoff_t startoff, |
| 6124 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6125 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6126 | xfs_exntst_t state) |
| 6127 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6128 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6129 | |
Brian Foster | 3728379 | 2018-07-11 22:26:23 -0700 | [diff] [blame] | 6130 | ASSERT(tp->t_firstblock == NULLFSBLOCK); |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6131 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6132 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6133 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6134 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6135 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6136 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6137 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6138 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6139 | |
| 6140 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6141 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6142 | return -EIO; |
| 6143 | |
| 6144 | switch (type) { |
| 6145 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6146 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Brian Foster | ff3edf2 | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 6147 | startblock, 0); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6148 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6149 | break; |
| 6150 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6151 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 6152 | XFS_BMAPI_REMAP, 1); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6153 | break; |
| 6154 | default: |
| 6155 | ASSERT(0); |
| 6156 | error = -EFSCORRUPTED; |
| 6157 | } |
| 6158 | |
| 6159 | return error; |
| 6160 | } |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6161 | |
| 6162 | /* Check that an inode's extent does not have invalid flags or bad ranges. */ |
| 6163 | xfs_failaddr_t |
| 6164 | xfs_bmap_validate_extent( |
| 6165 | struct xfs_inode *ip, |
| 6166 | int whichfork, |
| 6167 | struct xfs_bmbt_irec *irec) |
| 6168 | { |
| 6169 | struct xfs_mount *mp = ip->i_mount; |
| 6170 | xfs_fsblock_t endfsb; |
| 6171 | bool isrt; |
| 6172 | |
| 6173 | isrt = XFS_IS_REALTIME_INODE(ip); |
| 6174 | endfsb = irec->br_startblock + irec->br_blockcount - 1; |
| 6175 | if (isrt) { |
| 6176 | if (!xfs_verify_rtbno(mp, irec->br_startblock)) |
| 6177 | return __this_address; |
| 6178 | if (!xfs_verify_rtbno(mp, endfsb)) |
| 6179 | return __this_address; |
| 6180 | } else { |
| 6181 | if (!xfs_verify_fsbno(mp, irec->br_startblock)) |
| 6182 | return __this_address; |
| 6183 | if (!xfs_verify_fsbno(mp, endfsb)) |
| 6184 | return __this_address; |
| 6185 | if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) != |
| 6186 | XFS_FSB_TO_AGNO(mp, endfsb)) |
| 6187 | return __this_address; |
| 6188 | } |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 6189 | if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK) |
| 6190 | return __this_address; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6191 | return NULL; |
| 6192 | } |