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 | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 16 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 17 | #include "xfs_da_btree.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 18 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_btree.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_extfree_item.h" |
| 24 | #include "xfs_alloc.h" |
| 25 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 26 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 27 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_rtalloc.h" |
Darrick J. Wong | e9e899a | 2017-10-31 12:04:49 -0700 | [diff] [blame] | 29 | #include "xfs_errortag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_quota.h" |
| 32 | #include "xfs_trans_space.h" |
| 33 | #include "xfs_buf_item.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 34 | #include "xfs_trace.h" |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 35 | #include "xfs_symlink.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 36 | #include "xfs_attr_leaf.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 37 | #include "xfs_filestream.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 38 | #include "xfs_rmap.h" |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 39 | #include "xfs_ag_resv.h" |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 40 | #include "xfs_refcount.h" |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 41 | #include "xfs_icache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 45 | |
| 46 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 47 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | */ |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 51 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 52 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 54 | void |
| 55 | xfs_bmap_compute_maxlevels( |
| 56 | xfs_mount_t *mp, /* file system mount structure */ |
| 57 | int whichfork) /* data or attr fork */ |
| 58 | { |
| 59 | int level; /* btree level */ |
| 60 | uint maxblocks; /* max blocks at this level */ |
| 61 | uint maxleafents; /* max leaf entries possible */ |
| 62 | int maxrootrecs; /* max records in root block */ |
| 63 | int minleafrecs; /* min records in leaf block */ |
| 64 | int minnoderecs; /* min records in node block */ |
| 65 | int sz; /* root block size */ |
| 66 | |
| 67 | /* |
| 68 | * The maximum number of extents in a file, hence the maximum |
| 69 | * number of leaf entries, is controlled by the type of di_nextents |
| 70 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 71 | * (a signed 16-bit number, xfs_aextnum_t). |
| 72 | * |
| 73 | * Note that we can no longer assume that if we are in ATTR1 that |
| 74 | * the fork offset of all the inodes will be |
| 75 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 76 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 77 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 78 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 79 | * of a minimum size available. |
| 80 | */ |
| 81 | if (whichfork == XFS_DATA_FORK) { |
| 82 | maxleafents = MAXEXTNUM; |
| 83 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 84 | } else { |
| 85 | maxleafents = MAXAEXTNUM; |
| 86 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 87 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 88 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 89 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 90 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 91 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 92 | for (level = 1; maxblocks > 1; level++) { |
| 93 | if (maxblocks <= maxrootrecs) |
| 94 | maxblocks = 1; |
| 95 | else |
| 96 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 97 | } |
| 98 | mp->m_bm_maxlevels[whichfork] = level; |
| 99 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 101 | STATIC int /* error */ |
| 102 | xfs_bmbt_lookup_eq( |
| 103 | struct xfs_btree_cur *cur, |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 104 | struct xfs_bmbt_irec *irec, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 105 | int *stat) /* success/failure */ |
| 106 | { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 107 | cur->bc_rec.b = *irec; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 108 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 109 | } |
| 110 | |
| 111 | STATIC int /* error */ |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 112 | xfs_bmbt_lookup_first( |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 113 | struct xfs_btree_cur *cur, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 114 | int *stat) /* success/failure */ |
| 115 | { |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 116 | cur->bc_rec.b.br_startoff = 0; |
| 117 | cur->bc_rec.b.br_startblock = 0; |
| 118 | cur->bc_rec.b.br_blockcount = 0; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 119 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 120 | } |
| 121 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 122 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 123 | * Check if the inode needs to be converted to btree format. |
| 124 | */ |
| 125 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 126 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 127 | return whichfork != XFS_COW_FORK && |
| 128 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 129 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 130 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Check if the inode should be converted to extent format. |
| 135 | */ |
| 136 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 137 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 138 | return whichfork != XFS_COW_FORK && |
| 139 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 140 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 141 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 142 | } |
| 143 | |
| 144 | /* |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 145 | * 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] | 146 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 147 | */ |
| 148 | STATIC int |
| 149 | xfs_bmbt_update( |
| 150 | struct xfs_btree_cur *cur, |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 151 | struct xfs_bmbt_irec *irec) |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 152 | { |
| 153 | union xfs_btree_rec rec; |
| 154 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 155 | xfs_bmbt_disk_set_all(&rec.bmbt, irec); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 156 | return xfs_btree_update(cur, &rec); |
| 157 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 160 | * Compute the worst-case number of indirect blocks that will be used |
| 161 | * for ip's delayed extent of length "len". |
| 162 | */ |
| 163 | STATIC xfs_filblks_t |
| 164 | xfs_bmap_worst_indlen( |
| 165 | xfs_inode_t *ip, /* incore inode pointer */ |
| 166 | xfs_filblks_t len) /* delayed extent length */ |
| 167 | { |
| 168 | int level; /* btree level number */ |
| 169 | int maxrecs; /* maximum record count at this level */ |
| 170 | xfs_mount_t *mp; /* mount structure */ |
| 171 | xfs_filblks_t rval; /* return value */ |
| 172 | |
| 173 | mp = ip->i_mount; |
| 174 | maxrecs = mp->m_bmap_dmxr[0]; |
| 175 | for (level = 0, rval = 0; |
| 176 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 177 | level++) { |
| 178 | len += maxrecs - 1; |
| 179 | do_div(len, maxrecs); |
| 180 | rval += len; |
Darrick J. Wong | 5e5c943 | 2017-09-18 09:41:17 -0700 | [diff] [blame] | 181 | if (len == 1) |
| 182 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 183 | level - 1; |
| 184 | if (level == 0) |
| 185 | maxrecs = mp->m_bmap_dmxr[1]; |
| 186 | } |
| 187 | return rval; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Calculate the default attribute fork offset for newly created inodes. |
| 192 | */ |
| 193 | uint |
| 194 | xfs_default_attroffset( |
| 195 | struct xfs_inode *ip) |
| 196 | { |
| 197 | struct xfs_mount *mp = ip->i_mount; |
| 198 | uint offset; |
| 199 | |
| 200 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 201 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 202 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 203 | } else { |
| 204 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 205 | } |
| 206 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 207 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 208 | return offset; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Helper routine to reset inode di_forkoff field when switching |
| 213 | * attribute fork from local to extent format - we reset it where |
| 214 | * possible to make space available for inline data fork extents. |
| 215 | */ |
| 216 | STATIC void |
| 217 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 218 | xfs_inode_t *ip, |
| 219 | int whichfork) |
| 220 | { |
| 221 | if (whichfork == XFS_ATTR_FORK && |
| 222 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 223 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 224 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 225 | |
| 226 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 227 | ip->i_d.di_forkoff = dfl_forkoff; |
| 228 | } |
| 229 | } |
| 230 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 231 | #ifdef DEBUG |
| 232 | STATIC struct xfs_buf * |
| 233 | xfs_bmap_get_bp( |
| 234 | struct xfs_btree_cur *cur, |
| 235 | xfs_fsblock_t bno) |
| 236 | { |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame] | 237 | struct xfs_log_item *lip; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 238 | int i; |
| 239 | |
| 240 | if (!cur) |
| 241 | return NULL; |
| 242 | |
| 243 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 244 | if (!cur->bc_bufs[i]) |
| 245 | break; |
| 246 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 247 | return cur->bc_bufs[i]; |
| 248 | } |
| 249 | |
| 250 | /* 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] | 251 | list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) { |
| 252 | struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip; |
| 253 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 254 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 255 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 256 | return bip->bli_buf; |
| 257 | } |
| 258 | |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | STATIC void |
| 263 | xfs_check_block( |
| 264 | struct xfs_btree_block *block, |
| 265 | xfs_mount_t *mp, |
| 266 | int root, |
| 267 | short sz) |
| 268 | { |
| 269 | int i, j, dmxr; |
| 270 | __be64 *pp, *thispa; /* pointer to block address */ |
| 271 | xfs_bmbt_key_t *prevp, *keyp; |
| 272 | |
| 273 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 274 | |
| 275 | prevp = NULL; |
| 276 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 277 | dmxr = mp->m_bmap_dmxr[0]; |
| 278 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 279 | |
| 280 | if (prevp) { |
| 281 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 282 | be64_to_cpu(keyp->br_startoff)); |
| 283 | } |
| 284 | prevp = keyp; |
| 285 | |
| 286 | /* |
| 287 | * Compare the block numbers to see if there are dups. |
| 288 | */ |
| 289 | if (root) |
| 290 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 291 | else |
| 292 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 293 | |
| 294 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 295 | if (root) |
| 296 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 297 | else |
| 298 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 299 | if (*thispa == *pp) { |
| 300 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 301 | __func__, j, i, |
| 302 | (unsigned long long)be64_to_cpu(*thispa)); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 303 | xfs_err(mp, "%s: ptrs are equal in node\n", |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 304 | __func__); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 305 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * 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] | 313 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 314 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 315 | * them. The btree record ordering checks will still be done, so for such large |
| 316 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 317 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 318 | STATIC void |
| 319 | xfs_bmap_check_leaf_extents( |
| 320 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 321 | xfs_inode_t *ip, /* incore inode pointer */ |
| 322 | int whichfork) /* data or attr fork */ |
| 323 | { |
| 324 | struct xfs_btree_block *block; /* current btree block */ |
| 325 | xfs_fsblock_t bno; /* block # of "block" */ |
| 326 | xfs_buf_t *bp; /* buffer for "block" */ |
| 327 | int error; /* error return value */ |
| 328 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 329 | struct xfs_ifork *ifp; /* fork structure */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 330 | int level; /* btree level, for checking */ |
| 331 | xfs_mount_t *mp; /* file system mount structure */ |
| 332 | __be64 *pp; /* pointer to block address */ |
| 333 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 334 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 335 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 336 | int bp_release = 0; |
| 337 | |
| 338 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 339 | return; |
| 340 | } |
| 341 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 342 | /* skip large extent count inodes */ |
| 343 | if (ip->i_d.di_nextents > 10000) |
| 344 | return; |
| 345 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 346 | bno = NULLFSBLOCK; |
| 347 | mp = ip->i_mount; |
| 348 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 349 | block = ifp->if_broot; |
| 350 | /* |
| 351 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 352 | */ |
| 353 | level = be16_to_cpu(block->bb_level); |
| 354 | ASSERT(level > 0); |
| 355 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 356 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 357 | bno = be64_to_cpu(*pp); |
| 358 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 359 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 360 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 361 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 362 | |
| 363 | /* |
| 364 | * Go down the tree until leaf level is reached, following the first |
| 365 | * pointer (leftmost) at each level. |
| 366 | */ |
| 367 | while (level-- > 0) { |
| 368 | /* See if buf is in cur first */ |
| 369 | bp_release = 0; |
| 370 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 371 | if (!bp) { |
| 372 | bp_release = 1; |
| 373 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 374 | XFS_BMAP_BTREE_REF, |
| 375 | &xfs_bmbt_buf_ops); |
| 376 | if (error) |
| 377 | goto error_norelse; |
| 378 | } |
| 379 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 380 | if (level == 0) |
| 381 | break; |
| 382 | |
| 383 | /* |
| 384 | * Check this block for basic sanity (increasing keys and |
| 385 | * no duplicate blocks). |
| 386 | */ |
| 387 | |
| 388 | xfs_check_block(block, mp, 0, 0); |
| 389 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 390 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 391 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 392 | xfs_verify_fsbno(mp, bno), error0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 393 | if (bp_release) { |
| 394 | bp_release = 0; |
| 395 | xfs_trans_brelse(NULL, bp); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 401 | */ |
| 402 | i = 0; |
| 403 | |
| 404 | /* |
| 405 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 406 | */ |
| 407 | for (;;) { |
| 408 | xfs_fsblock_t nextbno; |
| 409 | xfs_extnum_t num_recs; |
| 410 | |
| 411 | |
| 412 | num_recs = xfs_btree_get_numrecs(block); |
| 413 | |
| 414 | /* |
| 415 | * Read-ahead the next leaf block, if any. |
| 416 | */ |
| 417 | |
| 418 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 419 | |
| 420 | /* |
| 421 | * Check all the extents to make sure they are OK. |
| 422 | * If we had a previous block, the last entry should |
| 423 | * conform with the first entry in this one. |
| 424 | */ |
| 425 | |
| 426 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 427 | if (i) { |
| 428 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 429 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 430 | xfs_bmbt_disk_get_startoff(ep)); |
| 431 | } |
| 432 | for (j = 1; j < num_recs; j++) { |
| 433 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 434 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 435 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 436 | xfs_bmbt_disk_get_startoff(nextp)); |
| 437 | ep = nextp; |
| 438 | } |
| 439 | |
| 440 | last = *ep; |
| 441 | i += num_recs; |
| 442 | if (bp_release) { |
| 443 | bp_release = 0; |
| 444 | xfs_trans_brelse(NULL, bp); |
| 445 | } |
| 446 | bno = nextbno; |
| 447 | /* |
| 448 | * If we've reached the end, stop. |
| 449 | */ |
| 450 | if (bno == NULLFSBLOCK) |
| 451 | break; |
| 452 | |
| 453 | bp_release = 0; |
| 454 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 455 | if (!bp) { |
| 456 | bp_release = 1; |
| 457 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 458 | XFS_BMAP_BTREE_REF, |
| 459 | &xfs_bmbt_buf_ops); |
| 460 | if (error) |
| 461 | goto error_norelse; |
| 462 | } |
| 463 | block = XFS_BUF_TO_BLOCK(bp); |
| 464 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 465 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 466 | return; |
| 467 | |
| 468 | error0: |
| 469 | xfs_warn(mp, "%s: at error0", __func__); |
| 470 | if (bp_release) |
| 471 | xfs_trans_brelse(NULL, bp); |
| 472 | error_norelse: |
| 473 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 474 | __func__, i); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 475 | xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 476 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 477 | return; |
| 478 | } |
| 479 | |
| 480 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 481 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 482 | * given the caller's original parameters. Specifically check the |
| 483 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 484 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 485 | */ |
| 486 | STATIC void |
| 487 | xfs_bmap_validate_ret( |
| 488 | xfs_fileoff_t bno, |
| 489 | xfs_filblks_t len, |
| 490 | int flags, |
| 491 | xfs_bmbt_irec_t *mval, |
| 492 | int nmap, |
| 493 | int ret_nmap) |
| 494 | { |
| 495 | int i; /* index to map values */ |
| 496 | |
| 497 | ASSERT(ret_nmap <= nmap); |
| 498 | |
| 499 | for (i = 0; i < ret_nmap; i++) { |
| 500 | ASSERT(mval[i].br_blockcount > 0); |
| 501 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 502 | ASSERT(mval[i].br_startoff >= bno); |
| 503 | ASSERT(mval[i].br_blockcount <= len); |
| 504 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 505 | bno + len); |
| 506 | } else { |
| 507 | ASSERT(mval[i].br_startoff < bno + len); |
| 508 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 509 | bno); |
| 510 | } |
| 511 | ASSERT(i == 0 || |
| 512 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 513 | mval[i].br_startoff); |
| 514 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 515 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 516 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 517 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | #else |
| 522 | #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] | 523 | #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] | 524 | #endif /* DEBUG */ |
| 525 | |
| 526 | /* |
| 527 | * bmap free list manipulation functions |
| 528 | */ |
| 529 | |
| 530 | /* |
| 531 | * Add the extent to the list of extents to be free at transaction end. |
| 532 | * The list is maintained sorted (by block number). |
| 533 | */ |
| 534 | void |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 535 | __xfs_bmap_add_free( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 536 | struct xfs_trans *tp, |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 537 | xfs_fsblock_t bno, |
| 538 | xfs_filblks_t len, |
Darrick J. Wong | 66e3237 | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 539 | const struct xfs_owner_info *oinfo, |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 540 | bool skip_discard) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 541 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 542 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 543 | #ifdef DEBUG |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 544 | struct xfs_mount *mp = tp->t_mountp; |
| 545 | xfs_agnumber_t agno; |
| 546 | xfs_agblock_t agbno; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 547 | |
| 548 | ASSERT(bno != NULLFSBLOCK); |
| 549 | ASSERT(len > 0); |
| 550 | ASSERT(len <= MAXEXTLEN); |
| 551 | ASSERT(!isnullstartblock(bno)); |
| 552 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 553 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 554 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 555 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 556 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 557 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 558 | #endif |
| 559 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 560 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 561 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 562 | new->xefi_startblock = bno; |
| 563 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 564 | if (oinfo) |
| 565 | new->xefi_oinfo = *oinfo; |
| 566 | else |
Darrick J. Wong | 7280fed | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 567 | new->xefi_oinfo = XFS_RMAP_OINFO_SKIP_UPDATE; |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 568 | new->xefi_skip_discard = skip_discard; |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 569 | trace_xfs_bmap_free_defer(tp->t_mountp, |
| 570 | XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0, |
| 571 | XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len); |
| 572 | xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Inode fork format manipulation functions |
| 577 | */ |
| 578 | |
| 579 | /* |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 580 | * Convert the inode format to extent format if it currently is in btree format, |
| 581 | * but the extent list is small enough that it fits into the extent format. |
| 582 | * |
| 583 | * Since the extents are already in-core, all we have to do is give up the space |
| 584 | * for the btree root and pitch the leaf block. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 585 | */ |
| 586 | STATIC int /* error */ |
| 587 | xfs_bmap_btree_to_extents( |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 588 | struct xfs_trans *tp, /* transaction pointer */ |
| 589 | struct xfs_inode *ip, /* incore inode pointer */ |
| 590 | struct xfs_btree_cur *cur, /* btree cursor */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 591 | int *logflagsp, /* inode logging flags */ |
| 592 | int whichfork) /* data or attr fork */ |
| 593 | { |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 594 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 595 | struct xfs_mount *mp = ip->i_mount; |
| 596 | struct xfs_btree_block *rblock = ifp->if_broot; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 597 | struct xfs_btree_block *cblock;/* child btree block */ |
| 598 | xfs_fsblock_t cbno; /* child block number */ |
| 599 | xfs_buf_t *cbp; /* child block's buffer */ |
| 600 | int error; /* error return value */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 601 | __be64 *pp; /* ptr to block address */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 602 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 603 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 604 | /* check if we actually need the extent format first: */ |
| 605 | if (!xfs_bmap_wants_extents(ip, whichfork)) |
| 606 | return 0; |
| 607 | |
| 608 | ASSERT(cur); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 609 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 610 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 611 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 612 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 613 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 614 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 615 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 616 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 617 | cbno = be64_to_cpu(*pp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 618 | #ifdef DEBUG |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 619 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, |
| 620 | xfs_btree_check_lptr(cur, cbno, 1)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 621 | #endif |
| 622 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 623 | &xfs_bmbt_buf_ops); |
| 624 | if (error) |
| 625 | return error; |
| 626 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 627 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 628 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 629 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 630 | xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 631 | ip->i_d.di_nblocks--; |
| 632 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 633 | xfs_trans_binval(tp, cbp); |
| 634 | if (cur->bc_bufs[0] == cbp) |
| 635 | cur->bc_bufs[0] = NULL; |
| 636 | xfs_iroot_realloc(ip, -1, whichfork); |
| 637 | ASSERT(ifp->if_broot == NULL); |
| 638 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 639 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 640 | *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Convert an extents-format file into a btree-format file. |
| 646 | * The new file will have a root block (in the inode) and a single child block. |
| 647 | */ |
| 648 | STATIC int /* error */ |
| 649 | xfs_bmap_extents_to_btree( |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 650 | struct xfs_trans *tp, /* transaction pointer */ |
| 651 | struct xfs_inode *ip, /* incore inode pointer */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 652 | struct xfs_btree_cur **curp, /* cursor returned to caller */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 653 | int wasdel, /* converting a delayed alloc */ |
| 654 | int *logflagsp, /* inode logging flags */ |
| 655 | int whichfork) /* data or attr fork */ |
| 656 | { |
| 657 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 658 | struct xfs_buf *abp; /* buffer for ablock */ |
| 659 | struct xfs_alloc_arg args; /* allocation arguments */ |
| 660 | struct xfs_bmbt_rec *arp; /* child record pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 661 | struct xfs_btree_block *block; /* btree root block */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 662 | struct xfs_btree_cur *cur; /* bmap btree cursor */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 663 | int error; /* error return value */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 664 | struct xfs_ifork *ifp; /* inode fork pointer */ |
| 665 | struct xfs_bmbt_key *kp; /* root block key pointer */ |
| 666 | struct xfs_mount *mp; /* mount structure */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 667 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 668 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 669 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 670 | xfs_extnum_t cnt = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 671 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 672 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 673 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 674 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 675 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 676 | |
| 677 | /* |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 678 | * Make space in the inode incore. This needs to be undone if we fail |
| 679 | * to expand the root. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 680 | */ |
| 681 | xfs_iroot_realloc(ip, 1, whichfork); |
| 682 | ifp->if_flags |= XFS_IFBROOT; |
| 683 | |
| 684 | /* |
| 685 | * Fill in the root. |
| 686 | */ |
| 687 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 688 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 689 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 690 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 691 | /* |
| 692 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 693 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 694 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 695 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 696 | /* |
| 697 | * Convert to a btree with two levels, one record in root. |
| 698 | */ |
| 699 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 700 | memset(&args, 0, sizeof(args)); |
| 701 | args.tp = tp; |
| 702 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 703 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 704 | if (tp->t_firstblock == NULLFSBLOCK) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 705 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 706 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 707 | } else if (tp->t_flags & XFS_TRANS_LOWMODE) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 708 | args.type = XFS_ALLOCTYPE_START_BNO; |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 709 | args.fsbno = tp->t_firstblock; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 710 | } else { |
| 711 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 712 | args.fsbno = tp->t_firstblock; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 713 | } |
| 714 | args.minlen = args.maxlen = args.prod = 1; |
| 715 | args.wasdel = wasdel; |
| 716 | *logflagsp = 0; |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 717 | error = xfs_alloc_vextent(&args); |
| 718 | if (error) |
| 719 | goto out_root_realloc; |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 720 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 721 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 722 | error = -ENOSPC; |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 723 | goto out_root_realloc; |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 724 | } |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 725 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 726 | /* |
| 727 | * Allocation can't fail, the space was reserved. |
| 728 | */ |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 729 | ASSERT(tp->t_firstblock == NULLFSBLOCK || |
| 730 | args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock)); |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 731 | tp->t_firstblock = args.fsbno; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 732 | cur->bc_private.b.allocated++; |
| 733 | ip->i_d.di_nblocks++; |
| 734 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 735 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 736 | if (!abp) { |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 737 | error = -EFSCORRUPTED; |
| 738 | goto out_unreserve_dquot; |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 739 | } |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 740 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 741 | /* |
| 742 | * Fill in the child block. |
| 743 | */ |
| 744 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 745 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 746 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 747 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 748 | XFS_BTREE_LONG_PTRS); |
| 749 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 750 | for_each_xfs_iext(ifp, &icur, &rec) { |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 751 | if (isnullstartblock(rec.br_startblock)) |
| 752 | continue; |
| 753 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt); |
| 754 | xfs_bmbt_disk_set_all(arp, &rec); |
| 755 | cnt++; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 756 | } |
| 757 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 758 | xfs_btree_set_numrecs(ablock, cnt); |
| 759 | |
| 760 | /* |
| 761 | * Fill in the root key and pointer. |
| 762 | */ |
| 763 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 764 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 765 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 766 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 767 | be16_to_cpu(block->bb_level))); |
| 768 | *pp = cpu_to_be64(args.fsbno); |
| 769 | |
| 770 | /* |
| 771 | * Do all this logging at the end so that |
| 772 | * the root is at the right level. |
| 773 | */ |
| 774 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 775 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 776 | ASSERT(*curp == NULL); |
| 777 | *curp = cur; |
| 778 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 779 | return 0; |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 780 | |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 781 | out_unreserve_dquot: |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 782 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 783 | out_root_realloc: |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 784 | xfs_iroot_realloc(ip, -1, whichfork); |
| 785 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Dave Chinner | e55ec4d | 2018-10-01 08:11:07 +1000 | [diff] [blame] | 786 | ASSERT(ifp->if_broot == NULL); |
Shan Hai | 01239d77 | 2018-08-10 17:55:55 -0700 | [diff] [blame] | 787 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 788 | |
| 789 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* |
| 793 | * Convert a local file to an extents file. |
| 794 | * This code is out of bounds for data forks of regular files, |
| 795 | * since the file data needs to get logged so things will stay consistent. |
| 796 | * (The bmap-level manipulations are ok, though). |
| 797 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 798 | void |
| 799 | xfs_bmap_local_to_extents_empty( |
| 800 | struct xfs_inode *ip, |
| 801 | int whichfork) |
| 802 | { |
| 803 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 804 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 805 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 806 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 807 | ASSERT(ifp->if_bytes == 0); |
| 808 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 809 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 810 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 811 | ifp->if_flags &= ~XFS_IFINLINE; |
| 812 | ifp->if_flags |= XFS_IFEXTENTS; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 813 | ifp->if_u1.if_root = NULL; |
| 814 | ifp->if_height = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 815 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 816 | } |
| 817 | |
| 818 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 819 | STATIC int /* error */ |
| 820 | xfs_bmap_local_to_extents( |
| 821 | xfs_trans_t *tp, /* transaction pointer */ |
| 822 | xfs_inode_t *ip, /* incore inode pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 823 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 824 | int *logflagsp, /* inode logging flags */ |
| 825 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 826 | void (*init_fn)(struct xfs_trans *tp, |
| 827 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 828 | struct xfs_inode *ip, |
| 829 | struct xfs_ifork *ifp)) |
| 830 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 831 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 832 | int flags; /* logging flags returned */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 833 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 834 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 835 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 836 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 837 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 838 | |
| 839 | /* |
| 840 | * We don't want to deal with the case of keeping inode data inline yet. |
| 841 | * So sending the data fork of a regular inode is invalid. |
| 842 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 843 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 844 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 845 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 846 | |
| 847 | if (!ifp->if_bytes) { |
| 848 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 849 | flags = XFS_ILOG_CORE; |
| 850 | goto done; |
| 851 | } |
| 852 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 853 | flags = 0; |
| 854 | error = 0; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 855 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 856 | memset(&args, 0, sizeof(args)); |
| 857 | args.tp = tp; |
| 858 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 859 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 860 | /* |
| 861 | * Allocate a block. We know we need only one, since the |
| 862 | * file currently fits in an inode. |
| 863 | */ |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 864 | if (tp->t_firstblock == NULLFSBLOCK) { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 865 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 866 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 867 | } else { |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 868 | args.fsbno = tp->t_firstblock; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 869 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 870 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 871 | args.total = total; |
| 872 | args.minlen = args.maxlen = args.prod = 1; |
| 873 | error = xfs_alloc_vextent(&args); |
| 874 | if (error) |
| 875 | goto done; |
| 876 | |
| 877 | /* Can't fail, the space was reserved. */ |
| 878 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 879 | ASSERT(args.len == 1); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 880 | tp->t_firstblock = args.fsbno; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 881 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 882 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 883 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 884 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 885 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 886 | * The callout is responsible for logging because the remote format |
| 887 | * might differ from the local format and thus we don't know how much to |
| 888 | * log here. Note that init_fn must also set the buffer log item type |
| 889 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 890 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 891 | init_fn(tp, bp, ip, ifp); |
| 892 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 893 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 894 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 895 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 896 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 897 | |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 898 | ifp->if_u1.if_root = NULL; |
| 899 | ifp->if_height = 0; |
| 900 | |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 901 | rec.br_startoff = 0; |
| 902 | rec.br_startblock = args.fsbno; |
| 903 | rec.br_blockcount = 1; |
| 904 | rec.br_state = XFS_EXT_NORM; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 905 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 906 | xfs_iext_insert(ip, &icur, &rec, 0); |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 907 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 908 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 909 | ip->i_d.di_nblocks = 1; |
| 910 | xfs_trans_mod_dquot_byino(tp, ip, |
| 911 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 912 | flags |= xfs_ilog_fext(whichfork); |
| 913 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 914 | done: |
| 915 | *logflagsp = flags; |
| 916 | return error; |
| 917 | } |
| 918 | |
| 919 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 921 | */ |
| 922 | STATIC int /* error */ |
| 923 | xfs_bmap_add_attrfork_btree( |
| 924 | xfs_trans_t *tp, /* transaction pointer */ |
| 925 | xfs_inode_t *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | int *flags) /* inode logging flags */ |
| 927 | { |
| 928 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 929 | int error; /* error return value */ |
| 930 | xfs_mount_t *mp; /* file system mount struct */ |
| 931 | int stat; /* newroot status */ |
| 932 | |
| 933 | mp = ip->i_mount; |
| 934 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 935 | *flags |= XFS_ILOG_DBROOT; |
| 936 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 937 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 938 | error = xfs_bmbt_lookup_first(cur, &stat); |
| 939 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 941 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 942 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 943 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | goto error0; |
| 945 | if (stat == 0) { |
| 946 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 947 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | cur->bc_private.b.allocated = 0; |
| 950 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 951 | } |
| 952 | return 0; |
| 953 | error0: |
| 954 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 955 | return error; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 960 | */ |
| 961 | STATIC int /* error */ |
| 962 | xfs_bmap_add_attrfork_extents( |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 963 | struct xfs_trans *tp, /* transaction pointer */ |
| 964 | struct xfs_inode *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | int *flags) /* inode logging flags */ |
| 966 | { |
| 967 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 968 | int error; /* error return value */ |
| 969 | |
| 970 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 971 | return 0; |
| 972 | cur = NULL; |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 973 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags, |
| 974 | XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | if (cur) { |
| 976 | cur->bc_private.b.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 977 | xfs_btree_del_cursor(cur, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } |
| 979 | return error; |
| 980 | } |
| 981 | |
| 982 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 983 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 984 | * different data fork content type needs a different callout to do the |
| 985 | * conversion. Some are basic and only require special block initialisation |
| 986 | * callouts for the data formating, others (directories) are so specialised they |
| 987 | * handle everything themselves. |
| 988 | * |
| 989 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 990 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 991 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | */ |
| 993 | STATIC int /* error */ |
| 994 | xfs_bmap_add_attrfork_local( |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 995 | struct xfs_trans *tp, /* transaction pointer */ |
| 996 | struct xfs_inode *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | int *flags) /* inode logging flags */ |
| 998 | { |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 999 | struct xfs_da_args dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
| 1001 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1002 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1003 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1004 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1006 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | dargs.dp = ip; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1008 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | dargs.whichfork = XFS_DATA_FORK; |
| 1010 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1011 | return xfs_dir2_sf_to_block(&dargs); |
| 1012 | } |
| 1013 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1014 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1015 | return xfs_bmap_local_to_extents(tp, ip, 1, flags, |
| 1016 | XFS_DATA_FORK, |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1017 | xfs_symlink_local_to_remote); |
| 1018 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1019 | /* should only be called for types that support local format data */ |
| 1020 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1021 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 1024 | /* Set an inode attr fork off based on the format */ |
| 1025 | int |
| 1026 | xfs_bmap_set_attrforkoff( |
| 1027 | struct xfs_inode *ip, |
| 1028 | int size, |
| 1029 | int *version) |
| 1030 | { |
| 1031 | switch (ip->i_d.di_format) { |
| 1032 | case XFS_DINODE_FMT_DEV: |
| 1033 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1034 | break; |
| 1035 | case XFS_DINODE_FMT_LOCAL: |
| 1036 | case XFS_DINODE_FMT_EXTENTS: |
| 1037 | case XFS_DINODE_FMT_BTREE: |
| 1038 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1039 | if (!ip->i_d.di_forkoff) |
| 1040 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1041 | else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version) |
| 1042 | *version = 2; |
| 1043 | break; |
| 1044 | default: |
| 1045 | ASSERT(0); |
| 1046 | return -EINVAL; |
| 1047 | } |
| 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1053 | * Convert inode from non-attributed to attributed. |
| 1054 | * Must not be in a transaction, ip must not be locked. |
| 1055 | */ |
| 1056 | int /* error code */ |
| 1057 | xfs_bmap_add_attrfork( |
| 1058 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1059 | int size, /* space new attribute needs */ |
| 1060 | int rsvd) /* xact may use reserved blks */ |
| 1061 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1062 | xfs_mount_t *mp; /* mount structure */ |
| 1063 | xfs_trans_t *tp; /* transaction pointer */ |
| 1064 | int blks; /* space reservation */ |
| 1065 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1066 | int logflags; /* logging flags */ |
| 1067 | int error; /* error return value */ |
| 1068 | |
| 1069 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1070 | |
| 1071 | mp = ip->i_mount; |
| 1072 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1073 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1074 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1075 | |
| 1076 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1077 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1078 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1079 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1080 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1081 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1082 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1083 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1084 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1085 | if (error) |
| 1086 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1087 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1088 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1089 | if (ip->i_d.di_anextents != 0) { |
| 1090 | error = -EFSCORRUPTED; |
| 1091 | goto trans_cancel; |
| 1092 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1093 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1094 | /* |
| 1095 | * For inodes coming from pre-6.2 filesystems. |
| 1096 | */ |
| 1097 | ASSERT(ip->i_d.di_aformat == 0); |
| 1098 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1099 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1100 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1101 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1102 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 1103 | error = xfs_bmap_set_attrforkoff(ip, size, &version); |
| 1104 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1105 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1106 | ASSERT(ip->i_afp == NULL); |
| 1107 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1108 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1109 | logflags = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1110 | switch (ip->i_d.di_format) { |
| 1111 | case XFS_DINODE_FMT_LOCAL: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1112 | error = xfs_bmap_add_attrfork_local(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1113 | break; |
| 1114 | case XFS_DINODE_FMT_EXTENTS: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1115 | error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1116 | break; |
| 1117 | case XFS_DINODE_FMT_BTREE: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame] | 1118 | error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1119 | break; |
| 1120 | default: |
| 1121 | error = 0; |
| 1122 | break; |
| 1123 | } |
| 1124 | if (logflags) |
| 1125 | xfs_trans_log_inode(tp, ip, logflags); |
| 1126 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1127 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1128 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1129 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1130 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1131 | |
| 1132 | spin_lock(&mp->m_sb_lock); |
| 1133 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1134 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1135 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1136 | } |
| 1137 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1138 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1139 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1140 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1141 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1142 | if (log_sb) |
| 1143 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1144 | } |
| 1145 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1146 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1147 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1148 | return error; |
| 1149 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1150 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1151 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1152 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1153 | return error; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * Internal and external extent tree search functions. |
| 1158 | */ |
| 1159 | |
| 1160 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1161 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1162 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1163 | int |
| 1164 | xfs_iread_extents( |
| 1165 | struct xfs_trans *tp, |
| 1166 | struct xfs_inode *ip, |
| 1167 | int whichfork) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1168 | { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1169 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | e8e0e17 | 2017-10-19 11:06:29 -0700 | [diff] [blame] | 1170 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1171 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1172 | xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
| 1173 | struct xfs_btree_block *block = ifp->if_broot; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1174 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1175 | struct xfs_bmbt_irec new; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1176 | xfs_fsblock_t bno; |
| 1177 | struct xfs_buf *bp; |
| 1178 | xfs_extnum_t i, j; |
| 1179 | int level; |
| 1180 | __be64 *pp; |
| 1181 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1182 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1183 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1184 | |
| 1185 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 1186 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 1187 | return -EFSCORRUPTED; |
| 1188 | } |
| 1189 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1190 | /* |
| 1191 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1192 | */ |
| 1193 | level = be16_to_cpu(block->bb_level); |
| 1194 | ASSERT(level > 0); |
| 1195 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1196 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1197 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1198 | /* |
| 1199 | * Go down the tree until leaf level is reached, following the first |
| 1200 | * pointer (leftmost) at each level. |
| 1201 | */ |
| 1202 | while (level-- > 0) { |
| 1203 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1204 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1205 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1206 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1207 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1208 | if (level == 0) |
| 1209 | break; |
| 1210 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1211 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1212 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 1213 | xfs_verify_fsbno(mp, bno), out_brelse); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1214 | xfs_trans_brelse(tp, bp); |
| 1215 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1216 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1217 | /* |
| 1218 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1219 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1220 | i = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1221 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1222 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1223 | /* |
| 1224 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1225 | */ |
| 1226 | for (;;) { |
| 1227 | xfs_bmbt_rec_t *frp; |
| 1228 | xfs_fsblock_t nextbno; |
| 1229 | xfs_extnum_t num_recs; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1230 | |
| 1231 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1232 | if (unlikely(i + num_recs > nextents)) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1233 | xfs_warn(ip->i_mount, |
| 1234 | "corrupt dinode %Lu, (btree extents).", |
| 1235 | (unsigned long long) ip->i_ino); |
Darrick J. Wong | 90a58f9 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1236 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
| 1237 | __func__, block, sizeof(*block), |
| 1238 | __this_address); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1239 | error = -EFSCORRUPTED; |
| 1240 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1241 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1242 | /* |
| 1243 | * Read-ahead the next leaf block, if any. |
| 1244 | */ |
| 1245 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1246 | if (nextbno != NULLFSBLOCK) |
| 1247 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1248 | &xfs_bmbt_buf_ops); |
| 1249 | /* |
| 1250 | * Copy records into the extent records. |
| 1251 | */ |
| 1252 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1253 | for (j = 0; j < num_recs; j++, frp++, i++) { |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1254 | xfs_failaddr_t fa; |
| 1255 | |
Christoph Hellwig | dac9c9b | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1256 | xfs_bmbt_disk_get_all(frp, &new); |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1257 | fa = xfs_bmap_validate_extent(ip, whichfork, &new); |
| 1258 | if (fa) { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1259 | error = -EFSCORRUPTED; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1260 | xfs_inode_verifier_error(ip, error, |
| 1261 | "xfs_iread_extents(2)", |
| 1262 | frp, sizeof(*frp), fa); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1263 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1264 | } |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1265 | xfs_iext_insert(ip, &icur, &new, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1266 | trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); |
| 1267 | xfs_iext_next(ifp, &icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1268 | } |
| 1269 | xfs_trans_brelse(tp, bp); |
| 1270 | bno = nextbno; |
| 1271 | /* |
| 1272 | * If we've reached the end, stop. |
| 1273 | */ |
| 1274 | if (bno == NULLFSBLOCK) |
| 1275 | break; |
| 1276 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1277 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1278 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1279 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1280 | block = XFS_BUF_TO_BLOCK(bp); |
| 1281 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1282 | |
| 1283 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) { |
| 1284 | error = -EFSCORRUPTED; |
| 1285 | goto out; |
| 1286 | } |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1287 | ASSERT(i == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1288 | |
| 1289 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1290 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1291 | |
| 1292 | out_brelse: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1293 | xfs_trans_brelse(tp, bp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1294 | out: |
| 1295 | xfs_iext_destroy(ifp); |
| 1296 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1297 | } |
| 1298 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1299 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1300 | * Returns the relative block number of the first unused block(s) in the given |
| 1301 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1302 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1303 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1304 | */ |
| 1305 | int /* error */ |
| 1306 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1307 | struct xfs_trans *tp, /* transaction pointer */ |
| 1308 | struct xfs_inode *ip, /* incore inode */ |
| 1309 | xfs_extlen_t len, /* size of hole to find */ |
| 1310 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1311 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1312 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1313 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1314 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1315 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1316 | xfs_fileoff_t lastaddr = 0; |
| 1317 | xfs_fileoff_t lowest, max; |
| 1318 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1319 | |
| 1320 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1321 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1322 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1323 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1324 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1325 | *first_unused = 0; |
| 1326 | return 0; |
| 1327 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1328 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1329 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1330 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1331 | if (error) |
| 1332 | return error; |
| 1333 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1334 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1335 | lowest = max = *first_unused; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1336 | for_each_xfs_iext(ifp, &icur, &got) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1337 | /* |
| 1338 | * See if the hole before this extent will work. |
| 1339 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1340 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1341 | got.br_startoff - max >= len) |
| 1342 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1343 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1344 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1345 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1346 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1347 | *first_unused = max; |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1352 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1353 | * last_block (input value) in the file. |
| 1354 | * This is not based on i_size, it is based on the extent records. |
| 1355 | * Returns 0 for local files, as they do not have extent records. |
| 1356 | */ |
| 1357 | int /* error */ |
| 1358 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1359 | struct xfs_trans *tp, /* transaction pointer */ |
| 1360 | struct xfs_inode *ip, /* incore inode */ |
| 1361 | xfs_fileoff_t *last_block, /* last block */ |
| 1362 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1363 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1364 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1365 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1366 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1367 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1368 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1369 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1370 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1371 | *last_block = 0; |
| 1372 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1373 | case XFS_DINODE_FMT_BTREE: |
| 1374 | case XFS_DINODE_FMT_EXTENTS: |
| 1375 | break; |
| 1376 | default: |
| 1377 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1378 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1379 | |
| 1380 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1381 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1382 | if (error) |
| 1383 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1384 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1385 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1386 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1387 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1388 | return 0; |
| 1389 | } |
| 1390 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1391 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1392 | xfs_bmap_last_extent( |
| 1393 | struct xfs_trans *tp, |
| 1394 | struct xfs_inode *ip, |
| 1395 | int whichfork, |
| 1396 | struct xfs_bmbt_irec *rec, |
| 1397 | int *is_empty) |
| 1398 | { |
| 1399 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1400 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1401 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1402 | |
| 1403 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1404 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1405 | if (error) |
| 1406 | return error; |
| 1407 | } |
| 1408 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1409 | xfs_iext_last(ifp, &icur); |
| 1410 | if (!xfs_iext_get_extent(ifp, &icur, rec)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1411 | *is_empty = 1; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1412 | else |
| 1413 | *is_empty = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | /* |
| 1418 | * Check the last inode extent to determine whether this allocation will result |
| 1419 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1420 | * blocks at the end of the file which do not start at the previous data block, |
| 1421 | * we will try to align the new blocks at stripe unit boundaries. |
| 1422 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1423 | * 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] | 1424 | * at, or past the EOF. |
| 1425 | */ |
| 1426 | STATIC int |
| 1427 | xfs_bmap_isaeof( |
| 1428 | struct xfs_bmalloca *bma, |
| 1429 | int whichfork) |
| 1430 | { |
| 1431 | struct xfs_bmbt_irec rec; |
| 1432 | int is_empty; |
| 1433 | int error; |
| 1434 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1435 | bma->aeof = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1436 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1437 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1438 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1439 | return error; |
| 1440 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1441 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1442 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1443 | return 0; |
| 1444 | } |
| 1445 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1446 | /* |
| 1447 | * Check if we are allocation or past the last extent, or at least into |
| 1448 | * the last delayed allocated extent. |
| 1449 | */ |
| 1450 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1451 | (bma->offset >= rec.br_startoff && |
| 1452 | isnullstartblock(rec.br_startblock)); |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1457 | * Returns the file-relative block number of the first block past eof in |
| 1458 | * the file. This is not based on i_size, it is based on the extent records. |
| 1459 | * Returns 0 for local files, as they do not have extent records. |
| 1460 | */ |
| 1461 | int |
| 1462 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1463 | struct xfs_inode *ip, |
| 1464 | xfs_fileoff_t *last_block, |
| 1465 | int whichfork) |
| 1466 | { |
| 1467 | struct xfs_bmbt_irec rec; |
| 1468 | int is_empty; |
| 1469 | int error; |
| 1470 | |
| 1471 | *last_block = 0; |
| 1472 | |
| 1473 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1474 | return 0; |
| 1475 | |
| 1476 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1477 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1478 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1479 | |
| 1480 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1481 | if (error || is_empty) |
| 1482 | return error; |
| 1483 | |
| 1484 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | * Returns whether the selected fork of the inode has exactly one |
| 1490 | * block or not. For the data fork we check this matches di_size, |
| 1491 | * implying the file's range is 0..bsize-1. |
| 1492 | */ |
| 1493 | int /* 1=>1 block, 0=>otherwise */ |
| 1494 | xfs_bmap_one_block( |
| 1495 | xfs_inode_t *ip, /* incore inode */ |
| 1496 | int whichfork) /* data or attr fork */ |
| 1497 | { |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 1498 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1499 | int rval; /* return value */ |
| 1500 | xfs_bmbt_irec_t s; /* internal version of extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1501 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1502 | |
| 1503 | #ifndef DEBUG |
| 1504 | if (whichfork == XFS_DATA_FORK) |
| 1505 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1506 | #endif /* !DEBUG */ |
| 1507 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1508 | return 0; |
| 1509 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1510 | return 0; |
| 1511 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1512 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1513 | xfs_iext_first(ifp, &icur); |
| 1514 | xfs_iext_get_extent(ifp, &icur, &s); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1515 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1516 | if (rval && whichfork == XFS_DATA_FORK) |
| 1517 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1518 | return rval; |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * Extent tree manipulation functions used during allocation. |
| 1523 | */ |
| 1524 | |
| 1525 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1526 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | */ |
| 1528 | STATIC int /* error */ |
| 1529 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1530 | struct xfs_bmalloca *bma, |
| 1531 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1533 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | int i; /* temp state */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 1536 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1538 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1539 | /* left is 0, right is 1, prev is 2 */ |
| 1540 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1541 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1542 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1543 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1544 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1546 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1547 | xfs_extnum_t *nextents; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1548 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1550 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1551 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1552 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1553 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1554 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1555 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1556 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1557 | ASSERT(!bma->cur || |
| 1558 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1559 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1560 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | #define LEFT r[0] |
| 1563 | #define RIGHT r[1] |
| 1564 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
| 1566 | /* |
| 1567 | * Set up a bunch of variables to make the tests simpler. |
| 1568 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1569 | xfs_iext_get_extent(ifp, &bma->icur, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1571 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1573 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1574 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1575 | da_old = startblockval(PREV.br_startblock); |
| 1576 | da_new = 0; |
| 1577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | /* |
| 1579 | * Set flags determining what part of the previous delayed allocation |
| 1580 | * extent is being replaced by a real allocation. |
| 1581 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1582 | if (PREV.br_startoff == new->br_startoff) |
| 1583 | state |= BMAP_LEFT_FILLING; |
| 1584 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1585 | state |= BMAP_RIGHT_FILLING; |
| 1586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | /* |
| 1588 | * Check and set flags if this segment has a left neighbor. |
| 1589 | * Don't set contiguous if the combined extent would be too large. |
| 1590 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1591 | if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1592 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1593 | if (isnullstartblock(LEFT.br_startblock)) |
| 1594 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1596 | |
| 1597 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1598 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1599 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1600 | LEFT.br_state == new->br_state && |
| 1601 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1602 | state |= BMAP_LEFT_CONTIG; |
| 1603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | /* |
| 1605 | * Check and set flags if this segment has a right neighbor. |
| 1606 | * Don't set contiguous if the combined extent would be too large. |
| 1607 | * Also check for all-three-contiguous being too large. |
| 1608 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1609 | if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1610 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1611 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1612 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1614 | |
| 1615 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1616 | new_endoff == RIGHT.br_startoff && |
| 1617 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1618 | new->br_state == RIGHT.br_state && |
| 1619 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1620 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1621 | BMAP_RIGHT_FILLING)) != |
| 1622 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1623 | BMAP_RIGHT_FILLING) || |
| 1624 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1625 | <= MAXEXTLEN)) |
| 1626 | state |= BMAP_RIGHT_CONTIG; |
| 1627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | error = 0; |
| 1629 | /* |
| 1630 | * Switch out based on the FILLING and CONTIG state bits. |
| 1631 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1632 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1633 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1634 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1635 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | /* |
| 1637 | * Filling in all of a previously delayed allocation extent. |
| 1638 | * The left and right neighbors are both contiguous with new. |
| 1639 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1640 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1641 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1642 | xfs_iext_remove(bma->ip, &bma->icur, state); |
| 1643 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1644 | xfs_iext_prev(ifp, &bma->icur); |
| 1645 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1646 | (*nextents)--; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1647 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1648 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1650 | else { |
| 1651 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1652 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1653 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1655 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1656 | error = xfs_btree_delete(bma->cur, &i); |
| 1657 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1659 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1660 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1661 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1663 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1664 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1665 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | goto done; |
| 1667 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | break; |
| 1669 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1670 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | /* |
| 1672 | * Filling in all of a previously delayed allocation extent. |
| 1673 | * The left neighbor is contiguous, the right is not. |
| 1674 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1675 | old = LEFT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1676 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1677 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1678 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1679 | xfs_iext_prev(ifp, &bma->icur); |
| 1680 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1681 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1682 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | rval = XFS_ILOG_DEXT; |
| 1684 | else { |
| 1685 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1686 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1687 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1689 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1690 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1691 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | goto done; |
| 1693 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | break; |
| 1695 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1696 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | /* |
| 1698 | * Filling in all of a previously delayed allocation extent. |
Dave Chinner | 9230a0b | 2018-11-19 22:50:08 -0800 | [diff] [blame] | 1699 | * The right neighbor is contiguous, the left is not. Take care |
| 1700 | * with delay -> unwritten extent allocation here because the |
| 1701 | * delalloc record we are overwriting is always written. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1703 | PREV.br_startblock = new->br_startblock; |
| 1704 | PREV.br_blockcount += RIGHT.br_blockcount; |
Dave Chinner | 9230a0b | 2018-11-19 22:50:08 -0800 | [diff] [blame] | 1705 | PREV.br_state = new->br_state; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1706 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1707 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1708 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1709 | xfs_iext_prev(ifp, &bma->icur); |
| 1710 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1711 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1712 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | rval = XFS_ILOG_DEXT; |
| 1714 | else { |
| 1715 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1716 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1717 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1719 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1720 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1721 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | goto done; |
| 1723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | break; |
| 1725 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1726 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | /* |
| 1728 | * Filling in all of a previously delayed allocation extent. |
| 1729 | * Neither the left nor right neighbors are contiguous with |
| 1730 | * the new one. |
| 1731 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1732 | PREV.br_startblock = new->br_startblock; |
| 1733 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1734 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1735 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1736 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1737 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1739 | else { |
| 1740 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1741 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1742 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1744 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1745 | error = xfs_btree_insert(bma->cur, &i); |
| 1746 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1748 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | break; |
| 1751 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1752 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | /* |
| 1754 | * Filling in the first part of a previous delayed allocation. |
| 1755 | * The left neighbor is contiguous. |
| 1756 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1757 | old = LEFT; |
| 1758 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1759 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1760 | startblockval(PREV.br_startblock)); |
| 1761 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1762 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1763 | |
Christoph Hellwig | bf99971 | 2017-11-03 10:34:38 -0700 | [diff] [blame] | 1764 | PREV.br_blockcount = temp; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1765 | PREV.br_startoff += new->br_blockcount; |
| 1766 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1767 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1768 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1769 | xfs_iext_prev(ifp, &bma->icur); |
| 1770 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1771 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1772 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | rval = XFS_ILOG_DEXT; |
| 1774 | else { |
| 1775 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1776 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1777 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1779 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1780 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1781 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | goto done; |
| 1783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | break; |
| 1785 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1786 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | /* |
| 1788 | * Filling in the first part of a previous delayed allocation. |
| 1789 | * The left neighbor is not contiguous. |
| 1790 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1791 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1792 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1793 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1795 | else { |
| 1796 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1797 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1798 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1800 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1801 | error = xfs_btree_insert(bma->cur, &i); |
| 1802 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1804 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1806 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1807 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1808 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1809 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | rval |= tmp_rval; |
| 1811 | if (error) |
| 1812 | goto done; |
| 1813 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1814 | |
| 1815 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1816 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1817 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1818 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1819 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1820 | PREV.br_startoff = new_endoff; |
| 1821 | PREV.br_blockcount = temp; |
| 1822 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1823 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1824 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1825 | xfs_iext_prev(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | break; |
| 1827 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1828 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | /* |
| 1830 | * Filling in the last part of a previous delayed allocation. |
| 1831 | * The right neighbor is contiguous with the new allocation. |
| 1832 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1833 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1834 | RIGHT.br_startoff = new->br_startoff; |
| 1835 | RIGHT.br_startblock = new->br_startblock; |
| 1836 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1837 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1838 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | rval = XFS_ILOG_DEXT; |
| 1840 | else { |
| 1841 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1842 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1843 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1845 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1846 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1847 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | goto done; |
| 1849 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1850 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1851 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1852 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1853 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1854 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1855 | PREV.br_blockcount = temp; |
| 1856 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1857 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1858 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1859 | xfs_iext_next(ifp, &bma->icur); |
| 1860 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | break; |
| 1862 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1863 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | /* |
| 1865 | * Filling in the last part of a previous delayed allocation. |
| 1866 | * The right neighbor is not contiguous. |
| 1867 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1868 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1869 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1870 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1872 | else { |
| 1873 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1874 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1875 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1877 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1878 | error = xfs_btree_insert(bma->cur, &i); |
| 1879 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1881 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1883 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1884 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1885 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1886 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | rval |= tmp_rval; |
| 1888 | if (error) |
| 1889 | goto done; |
| 1890 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1891 | |
| 1892 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1893 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1894 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1895 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1896 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1897 | PREV.br_startblock = nullstartblock(da_new); |
| 1898 | PREV.br_blockcount = temp; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1899 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1900 | xfs_iext_next(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | break; |
| 1902 | |
| 1903 | case 0: |
| 1904 | /* |
| 1905 | * Filling in the middle part of a previous delayed allocation. |
| 1906 | * Contiguity is impossible here. |
| 1907 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1908 | * |
| 1909 | * We start with a delayed allocation: |
| 1910 | * |
| 1911 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1912 | * PREV @ idx |
| 1913 | * |
| 1914 | * and we are allocating: |
| 1915 | * +rrrrrrrrrrrrrrrrr+ |
| 1916 | * new |
| 1917 | * |
| 1918 | * and we set it up for insertion as: |
| 1919 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1920 | * new |
| 1921 | * PREV @ idx LEFT RIGHT |
| 1922 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1924 | old = PREV; |
| 1925 | |
| 1926 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1927 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1928 | |
| 1929 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1930 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1931 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1932 | RIGHT.br_blockcount = |
| 1933 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1934 | RIGHT.br_startblock = |
| 1935 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1936 | RIGHT.br_blockcount)); |
| 1937 | |
| 1938 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1939 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1940 | PREV.br_startblock = |
| 1941 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1942 | PREV.br_blockcount)); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1943 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1944 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1945 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1946 | xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); |
| 1947 | xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1948 | (*nextents)++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1949 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1950 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1952 | else { |
| 1953 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1954 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1955 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1957 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1958 | error = xfs_btree_insert(bma->cur, &i); |
| 1959 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1961 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1963 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1964 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1965 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 1966 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | rval |= tmp_rval; |
| 1968 | if (error) |
| 1969 | goto done; |
| 1970 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1971 | |
| 1972 | da_new = startblockval(PREV.br_startblock) + |
| 1973 | startblockval(RIGHT.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | break; |
| 1975 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1976 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1977 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1978 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1979 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1980 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1981 | case BMAP_LEFT_CONTIG: |
| 1982 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | /* |
| 1984 | * These cases are all impossible. |
| 1985 | */ |
| 1986 | ASSERT(0); |
| 1987 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1988 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 1989 | /* add reverse mapping unless caller opted out */ |
| 1990 | if (!(bma->flags & XFS_BMAPI_NORMAP)) { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 1991 | error = xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new); |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 1992 | if (error) |
| 1993 | goto done; |
| 1994 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 1995 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1996 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1997 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1998 | int tmp_logflags; /* partial log flag return val */ |
| 1999 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2000 | ASSERT(bma->cur == NULL); |
| 2001 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 2002 | &bma->cur, da_old > 0, &tmp_logflags, |
| 2003 | whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2004 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2005 | if (error) |
| 2006 | goto done; |
| 2007 | } |
| 2008 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2009 | if (bma->cur) { |
| 2010 | da_new += bma->cur->bc_private.b.allocated; |
| 2011 | bma->cur->bc_private.b.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2014 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2015 | if (da_new != da_old) { |
| 2016 | ASSERT(state == 0 || da_new < da_old); |
| 2017 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2018 | false); |
| 2019 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2020 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2021 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2023 | if (whichfork != XFS_COW_FORK) |
| 2024 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | return error; |
| 2026 | #undef LEFT |
| 2027 | #undef RIGHT |
| 2028 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2032 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | */ |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 2034 | int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2036 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2038 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2039 | struct xfs_iext_cursor *icur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2041 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2042 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | int i; /* temp state */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 2047 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2050 | /* left is 0, right is 1, prev is 2 */ |
| 2051 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2052 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2053 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2054 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2056 | *logflagsp = 0; |
| 2057 | |
| 2058 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2059 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2060 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2061 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2062 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2063 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | #define LEFT r[0] |
| 2066 | #define RIGHT r[1] |
| 2067 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | /* |
| 2070 | * Set up a bunch of variables to make the tests simpler. |
| 2071 | */ |
| 2072 | error = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2073 | xfs_iext_get_extent(ifp, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2074 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2076 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2077 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | /* |
| 2080 | * Set flags determining what part of the previous oldext allocation |
| 2081 | * extent is being replaced by a newext allocation. |
| 2082 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2083 | if (PREV.br_startoff == new->br_startoff) |
| 2084 | state |= BMAP_LEFT_FILLING; |
| 2085 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2086 | state |= BMAP_RIGHT_FILLING; |
| 2087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | /* |
| 2089 | * Check and set flags if this segment has a left neighbor. |
| 2090 | * Don't set contiguous if the combined extent would be too large. |
| 2091 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2092 | if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2093 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2094 | if (isnullstartblock(LEFT.br_startblock)) |
| 2095 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2097 | |
| 2098 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2099 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2100 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2101 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2102 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2103 | state |= BMAP_LEFT_CONTIG; |
| 2104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | /* |
| 2106 | * Check and set flags if this segment has a right neighbor. |
| 2107 | * Don't set contiguous if the combined extent would be too large. |
| 2108 | * Also check for all-three-contiguous being too large. |
| 2109 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2110 | if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2111 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2112 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2113 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2115 | |
| 2116 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2117 | new_endoff == RIGHT.br_startoff && |
| 2118 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2119 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2120 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2121 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2122 | BMAP_RIGHT_FILLING)) != |
| 2123 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2124 | BMAP_RIGHT_FILLING) || |
| 2125 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2126 | <= MAXEXTLEN)) |
| 2127 | state |= BMAP_RIGHT_CONTIG; |
| 2128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | /* |
| 2130 | * Switch out based on the FILLING and CONTIG state bits. |
| 2131 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2132 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2133 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2134 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2135 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | /* |
| 2137 | * Setting all of a previous oldext extent to newext. |
| 2138 | * The left and right neighbors are both contiguous with new. |
| 2139 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2140 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2141 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2142 | xfs_iext_remove(ip, icur, state); |
| 2143 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2144 | xfs_iext_prev(ifp, icur); |
| 2145 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2146 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2147 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | if (cur == NULL) |
| 2149 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2150 | else { |
| 2151 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2152 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2153 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2155 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2156 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2158 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2159 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2161 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2162 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2164 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2165 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2167 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2168 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2169 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | goto done; |
| 2171 | } |
| 2172 | break; |
| 2173 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2174 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | /* |
| 2176 | * Setting all of a previous oldext extent to newext. |
| 2177 | * The left neighbor is contiguous, the right is not. |
| 2178 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2179 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2180 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2181 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2182 | xfs_iext_prev(ifp, icur); |
| 2183 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2184 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2185 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | if (cur == NULL) |
| 2187 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2188 | else { |
| 2189 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2190 | error = xfs_bmbt_lookup_eq(cur, &PREV, &i); |
| 2191 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2193 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2194 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2196 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2197 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2199 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2200 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2201 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | goto done; |
| 2203 | } |
| 2204 | break; |
| 2205 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2206 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | /* |
| 2208 | * Setting all of a previous oldext extent to newext. |
| 2209 | * The right neighbor is contiguous, the left is not. |
| 2210 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2211 | PREV.br_blockcount += RIGHT.br_blockcount; |
| 2212 | PREV.br_state = new->br_state; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2213 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2214 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2215 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2216 | xfs_iext_prev(ifp, icur); |
| 2217 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2218 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2219 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2220 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | if (cur == NULL) |
| 2222 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2223 | else { |
| 2224 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2225 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2226 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2228 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2229 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2231 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2232 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2234 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2235 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2236 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | goto done; |
| 2238 | } |
| 2239 | break; |
| 2240 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2241 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | /* |
| 2243 | * Setting all of a previous oldext extent to newext. |
| 2244 | * Neither the left nor right neighbors are contiguous with |
| 2245 | * the new one. |
| 2246 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2247 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2248 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | if (cur == NULL) |
| 2251 | rval = XFS_ILOG_DEXT; |
| 2252 | else { |
| 2253 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2254 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2255 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2257 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2258 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2259 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | goto done; |
| 2261 | } |
| 2262 | break; |
| 2263 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2264 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | /* |
| 2266 | * Setting the first part of a previous oldext extent to newext. |
| 2267 | * The left neighbor is contiguous. |
| 2268 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2269 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2270 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2271 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2272 | PREV.br_startoff += new->br_blockcount; |
| 2273 | PREV.br_startblock += new->br_blockcount; |
| 2274 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2275 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2276 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2277 | xfs_iext_prev(ifp, icur); |
| 2278 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | if (cur == NULL) |
| 2281 | rval = XFS_ILOG_DEXT; |
| 2282 | else { |
| 2283 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2284 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2285 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2287 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2288 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2289 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2291 | error = xfs_btree_decrement(cur, 0, &i); |
| 2292 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2294 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2295 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | goto done; |
| 2297 | } |
| 2298 | break; |
| 2299 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2300 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | /* |
| 2302 | * Setting the first part of a previous oldext extent to newext. |
| 2303 | * The left neighbor is not contiguous. |
| 2304 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2305 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2306 | PREV.br_startoff += new->br_blockcount; |
| 2307 | PREV.br_startblock += new->br_blockcount; |
| 2308 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2309 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2310 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2311 | xfs_iext_insert(ip, icur, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2312 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2313 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | if (cur == NULL) |
| 2315 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2316 | else { |
| 2317 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2318 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2319 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2321 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2322 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2323 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | goto done; |
| 2325 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2326 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2328 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | } |
| 2330 | break; |
| 2331 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2332 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | /* |
| 2334 | * Setting the last part of a previous oldext extent to newext. |
| 2335 | * The right neighbor is contiguous with the new allocation. |
| 2336 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2337 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2338 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2339 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2340 | RIGHT.br_startoff = new->br_startoff; |
| 2341 | RIGHT.br_startblock = new->br_startblock; |
| 2342 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2343 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2344 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2345 | xfs_iext_next(ifp, icur); |
| 2346 | xfs_iext_update_extent(ip, state, icur, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | if (cur == NULL) |
| 2349 | rval = XFS_ILOG_DEXT; |
| 2350 | else { |
| 2351 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2352 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2353 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2355 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2356 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2357 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2359 | error = xfs_btree_increment(cur, 0, &i); |
| 2360 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2362 | error = xfs_bmbt_update(cur, &RIGHT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2363 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | goto done; |
| 2365 | } |
| 2366 | break; |
| 2367 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2368 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | /* |
| 2370 | * Setting the last part of a previous oldext extent to newext. |
| 2371 | * The right neighbor is not contiguous. |
| 2372 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2373 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2374 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2375 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2376 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2377 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2378 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2379 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2380 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2381 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | if (cur == NULL) |
| 2383 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2384 | else { |
| 2385 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2386 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2387 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2389 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2390 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2391 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2393 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2394 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2396 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2397 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2399 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | } |
| 2401 | break; |
| 2402 | |
| 2403 | case 0: |
| 2404 | /* |
| 2405 | * Setting the middle part of a previous oldext extent to |
| 2406 | * newext. Contiguity is impossible here. |
| 2407 | * One extent becomes three extents. |
| 2408 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2409 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2410 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | r[0] = *new; |
| 2413 | r[1].br_startoff = new_endoff; |
| 2414 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2415 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2417 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2418 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2419 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2420 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2421 | xfs_iext_insert(ip, icur, &r[1], state); |
| 2422 | xfs_iext_insert(ip, icur, &r[0], state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2423 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2424 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2425 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | if (cur == NULL) |
| 2427 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2428 | else { |
| 2429 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2430 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2431 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2433 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2435 | error = xfs_bmbt_update(cur, &r[1]); |
| 2436 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | goto done; |
| 2438 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2440 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2442 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2443 | /* |
| 2444 | * Reset the cursor to the position of the new extent |
| 2445 | * we are about to insert as we can't trust it after |
| 2446 | * the previous insert. |
| 2447 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2448 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2449 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2451 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2453 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2455 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | } |
| 2457 | break; |
| 2458 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2459 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2460 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2461 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2462 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2463 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2464 | case BMAP_LEFT_CONTIG: |
| 2465 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | /* |
| 2467 | * These cases are all impossible. |
| 2468 | */ |
| 2469 | ASSERT(0); |
| 2470 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2471 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2472 | /* update reverse mappings */ |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 2473 | error = xfs_rmap_convert_extent(mp, tp, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2474 | if (error) |
| 2475 | goto done; |
| 2476 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2477 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2478 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2479 | int tmp_logflags; /* partial log flag return val */ |
| 2480 | |
| 2481 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 2482 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 2483 | &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2484 | *logflagsp |= tmp_logflags; |
| 2485 | if (error) |
| 2486 | goto done; |
| 2487 | } |
| 2488 | |
| 2489 | /* clear out the allocated field, done with it now in any case. */ |
| 2490 | if (cur) { |
| 2491 | cur->bc_private.b.allocated = 0; |
| 2492 | *curp = cur; |
| 2493 | } |
| 2494 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2495 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2497 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | return error; |
| 2499 | #undef LEFT |
| 2500 | #undef RIGHT |
| 2501 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2505 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2507 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | xfs_bmap_add_extent_hole_delay( |
| 2509 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2510 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2511 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2512 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | { |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 2514 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2516 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2517 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2518 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2519 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2520 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2522 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2523 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | /* |
| 2526 | * Check and set flags if this segment has a left neighbor |
| 2527 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2528 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2529 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2530 | if (isnullstartblock(left.br_startblock)) |
| 2531 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | /* |
| 2535 | * Check and set flags if the current (right) segment exists. |
| 2536 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2537 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2538 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2539 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2540 | if (isnullstartblock(right.br_startblock)) |
| 2541 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | /* |
| 2545 | * Set contiguity flags on the left and right neighbors. |
| 2546 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2547 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2548 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2549 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2550 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2551 | state |= BMAP_LEFT_CONTIG; |
| 2552 | |
| 2553 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2554 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2555 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2556 | (!(state & BMAP_LEFT_CONTIG) || |
| 2557 | (left.br_blockcount + new->br_blockcount + |
| 2558 | right.br_blockcount <= MAXEXTLEN))) |
| 2559 | state |= BMAP_RIGHT_CONTIG; |
| 2560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | /* |
| 2562 | * Switch out based on the contiguity flags. |
| 2563 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2564 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2565 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | /* |
| 2567 | * New allocation is contiguous with delayed allocations |
| 2568 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2569 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | */ |
| 2571 | temp = left.br_blockcount + new->br_blockcount + |
| 2572 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2573 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2574 | oldlen = startblockval(left.br_startblock) + |
| 2575 | startblockval(new->br_startblock) + |
| 2576 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2577 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2578 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2579 | left.br_startblock = nullstartblock(newlen); |
| 2580 | left.br_blockcount = temp; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2581 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2582 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2583 | xfs_iext_prev(ifp, icur); |
| 2584 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | break; |
| 2586 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2587 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | /* |
| 2589 | * New allocation is contiguous with a delayed allocation |
| 2590 | * on the left. |
| 2591 | * Merge the new allocation with the left neighbor. |
| 2592 | */ |
| 2593 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2594 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2595 | oldlen = startblockval(left.br_startblock) + |
| 2596 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2597 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2598 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2599 | left.br_blockcount = temp; |
| 2600 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | 41d196f | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 2601 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2602 | xfs_iext_prev(ifp, icur); |
| 2603 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | break; |
| 2605 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2606 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | /* |
| 2608 | * New allocation is contiguous with a delayed allocation |
| 2609 | * on the right. |
| 2610 | * Merge the new allocation with the right neighbor. |
| 2611 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2613 | oldlen = startblockval(new->br_startblock) + |
| 2614 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2615 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2616 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2617 | right.br_startoff = new->br_startoff; |
| 2618 | right.br_startblock = nullstartblock(newlen); |
| 2619 | right.br_blockcount = temp; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2620 | xfs_iext_update_extent(ip, state, icur, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | break; |
| 2622 | |
| 2623 | case 0: |
| 2624 | /* |
| 2625 | * New allocation is not contiguous with another |
| 2626 | * delayed allocation. |
| 2627 | * Insert a new entry. |
| 2628 | */ |
| 2629 | oldlen = newlen = 0; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2630 | xfs_iext_insert(ip, icur, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | break; |
| 2632 | } |
| 2633 | if (oldlen != newlen) { |
| 2634 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2635 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2636 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | /* |
| 2638 | * Nothing to do for disk quota accounting here. |
| 2639 | */ |
| 2640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2644 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | */ |
| 2646 | STATIC int /* error */ |
| 2647 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2648 | struct xfs_trans *tp, |
| 2649 | struct xfs_inode *ip, |
| 2650 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2651 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2652 | struct xfs_btree_cur **curp, |
| 2653 | struct xfs_bmbt_irec *new, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2654 | int *logflagsp, |
| 2655 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2657 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2658 | struct xfs_mount *mp = ip->i_mount; |
| 2659 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2663 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2664 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2665 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2666 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2668 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2669 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2670 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2671 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | /* |
| 2674 | * Check and set flags if this segment has a left neighbor. |
| 2675 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2676 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2677 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2678 | if (isnullstartblock(left.br_startblock)) |
| 2679 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | /* |
| 2683 | * Check and set flags if this segment has a current value. |
| 2684 | * Not true if we're inserting into the "hole" at eof. |
| 2685 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2686 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2687 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2688 | if (isnullstartblock(right.br_startblock)) |
| 2689 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | /* |
| 2693 | * We're inserting a real allocation between "left" and "right". |
| 2694 | * Set the contiguity flags. Don't let extents get too large. |
| 2695 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2696 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2697 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2698 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2699 | left.br_state == new->br_state && |
| 2700 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2701 | state |= BMAP_LEFT_CONTIG; |
| 2702 | |
| 2703 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2704 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2705 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2706 | new->br_state == right.br_state && |
| 2707 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2708 | (!(state & BMAP_LEFT_CONTIG) || |
| 2709 | left.br_blockcount + new->br_blockcount + |
| 2710 | right.br_blockcount <= MAXEXTLEN)) |
| 2711 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2713 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | /* |
| 2715 | * Select which case we're in here, and implement it. |
| 2716 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2717 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2718 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | /* |
| 2720 | * New allocation is contiguous with real allocations on the |
| 2721 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2722 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2724 | left.br_blockcount += new->br_blockcount + right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2725 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2726 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2727 | xfs_iext_prev(ifp, icur); |
| 2728 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2729 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2730 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2731 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2732 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2733 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2734 | } else { |
| 2735 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2736 | error = xfs_bmbt_lookup_eq(cur, &right, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2737 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2738 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2739 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2740 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2741 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2742 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2743 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2744 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2745 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2746 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2747 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2748 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2749 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2750 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2752 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2754 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | /* |
| 2756 | * New allocation is contiguous with a real allocation |
| 2757 | * on the left. |
| 2758 | * Merge the new allocation with the left neighbor. |
| 2759 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2760 | old = left; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2761 | left.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 1d2e008 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2762 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2763 | xfs_iext_prev(ifp, icur); |
| 2764 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2765 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2766 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2767 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2768 | } else { |
| 2769 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2770 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2771 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2772 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2773 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2774 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2775 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2776 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2778 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2780 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | /* |
| 2782 | * New allocation is contiguous with a real allocation |
| 2783 | * on the right. |
| 2784 | * Merge the new allocation with the right neighbor. |
| 2785 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2786 | old = right; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2787 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2788 | right.br_startoff = new->br_startoff; |
| 2789 | right.br_startblock = new->br_startblock; |
| 2790 | right.br_blockcount += new->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2791 | xfs_iext_update_extent(ip, state, icur, &right); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2792 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2793 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2794 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2795 | } else { |
| 2796 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2797 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2798 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2799 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2800 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2801 | error = xfs_bmbt_update(cur, &right); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2802 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2803 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2805 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | |
| 2807 | case 0: |
| 2808 | /* |
| 2809 | * New allocation is not contiguous with another |
| 2810 | * real allocation. |
| 2811 | * Insert a new entry. |
| 2812 | */ |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2813 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2814 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2815 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2816 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2817 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2818 | } else { |
| 2819 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2820 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2821 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2822 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2823 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2824 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2825 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2826 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2827 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2829 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2831 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2832 | /* add reverse mapping unless caller opted out */ |
| 2833 | if (!(flags & XFS_BMAPI_NORMAP)) { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 2834 | error = xfs_rmap_map_extent(tp, ip, whichfork, new); |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2835 | if (error) |
| 2836 | goto done; |
| 2837 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2838 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2839 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2840 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2841 | int tmp_logflags; /* partial log flag return val */ |
| 2842 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2843 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 2844 | error = xfs_bmap_extents_to_btree(tp, ip, curp, 0, |
| 2845 | &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2846 | *logflagsp |= tmp_logflags; |
| 2847 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2848 | if (error) |
| 2849 | goto done; |
| 2850 | } |
| 2851 | |
| 2852 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2853 | if (cur) |
| 2854 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2855 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2856 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2857 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2858 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2859 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2862 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2863 | * Functions used in the extent read, allocate and remove paths |
| 2864 | */ |
| 2865 | |
| 2866 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2867 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2868 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2869 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2870 | xfs_bmap_extsize_align( |
| 2871 | xfs_mount_t *mp, |
| 2872 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2873 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2874 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2875 | int rt, /* is this a realtime inode? */ |
| 2876 | int eof, /* is extent at end-of-file? */ |
| 2877 | int delay, /* creating delalloc extent? */ |
| 2878 | int convert, /* overwriting unwritten extent? */ |
| 2879 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2880 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2881 | { |
| 2882 | xfs_fileoff_t orig_off; /* original offset */ |
| 2883 | xfs_extlen_t orig_alen; /* original length */ |
| 2884 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2885 | xfs_fileoff_t nexto; /* next file offset */ |
| 2886 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2887 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2888 | xfs_extlen_t align_alen; /* temp for length */ |
| 2889 | xfs_extlen_t temp; /* temp for calculations */ |
| 2890 | |
| 2891 | if (convert) |
| 2892 | return 0; |
| 2893 | |
| 2894 | orig_off = align_off = *offp; |
| 2895 | orig_alen = align_alen = *lenp; |
| 2896 | orig_end = orig_off + orig_alen; |
| 2897 | |
| 2898 | /* |
| 2899 | * If this request overlaps an existing extent, then don't |
| 2900 | * attempt to perform any additional alignment. |
| 2901 | */ |
| 2902 | if (!delay && !eof && |
| 2903 | (orig_off >= gotp->br_startoff) && |
| 2904 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2905 | return 0; |
| 2906 | } |
| 2907 | |
| 2908 | /* |
| 2909 | * If the file offset is unaligned vs. the extent size |
| 2910 | * we need to align it. This will be possible unless |
| 2911 | * the file was previously written with a kernel that didn't |
| 2912 | * perform this alignment, or if a truncate shot us in the |
| 2913 | * foot. |
| 2914 | */ |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 2915 | div_u64_rem(orig_off, extsz, &temp); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2916 | if (temp) { |
| 2917 | align_alen += temp; |
| 2918 | align_off -= temp; |
| 2919 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2920 | |
| 2921 | /* Same adjustment for the end of the requested area. */ |
| 2922 | temp = (align_alen % extsz); |
| 2923 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2924 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2925 | |
| 2926 | /* |
| 2927 | * For large extent hint sizes, the aligned extent might be larger than |
| 2928 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 2929 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 2930 | * short allocation just fine, so it is safe to do this. We only want to |
| 2931 | * do it when we are forced to, though, because it means more allocation |
| 2932 | * operations are required. |
| 2933 | */ |
| 2934 | while (align_alen > MAXEXTLEN) |
| 2935 | align_alen -= extsz; |
| 2936 | ASSERT(align_alen <= MAXEXTLEN); |
| 2937 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2938 | /* |
| 2939 | * If the previous block overlaps with this proposed allocation |
| 2940 | * then move the start forward without adjusting the length. |
| 2941 | */ |
| 2942 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2943 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2944 | prevo = prevp->br_startoff; |
| 2945 | else |
| 2946 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2947 | } else |
| 2948 | prevo = 0; |
| 2949 | if (align_off != orig_off && align_off < prevo) |
| 2950 | align_off = prevo; |
| 2951 | /* |
| 2952 | * If the next block overlaps with this proposed allocation |
| 2953 | * then move the start back without adjusting the length, |
| 2954 | * but not before offset 0. |
| 2955 | * This may of course make the start overlap previous block, |
| 2956 | * and if we hit the offset 0 limit then the next block |
| 2957 | * can still overlap too. |
| 2958 | */ |
| 2959 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2960 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2961 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2962 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2963 | else |
| 2964 | nexto = gotp->br_startoff; |
| 2965 | } else |
| 2966 | nexto = NULLFILEOFF; |
| 2967 | if (!eof && |
| 2968 | align_off + align_alen != orig_end && |
| 2969 | align_off + align_alen > nexto) |
| 2970 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2971 | /* |
| 2972 | * If we're now overlapping the next or previous extent that |
| 2973 | * means we can't fit an extsz piece in this hole. Just move |
| 2974 | * the start forward to the first valid spot and set |
| 2975 | * the length so we hit the end. |
| 2976 | */ |
| 2977 | if (align_off != orig_off && align_off < prevo) |
| 2978 | align_off = prevo; |
| 2979 | if (align_off + align_alen != orig_end && |
| 2980 | align_off + align_alen > nexto && |
| 2981 | nexto != NULLFILEOFF) { |
| 2982 | ASSERT(nexto > prevo); |
| 2983 | align_alen = nexto - align_off; |
| 2984 | } |
| 2985 | |
| 2986 | /* |
| 2987 | * If realtime, and the result isn't a multiple of the realtime |
| 2988 | * extent size we need to remove blocks until it is. |
| 2989 | */ |
| 2990 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 2991 | /* |
| 2992 | * We're not covering the original request, or |
| 2993 | * we won't be able to once we fix the length. |
| 2994 | */ |
| 2995 | if (orig_off < align_off || |
| 2996 | orig_end > align_off + align_alen || |
| 2997 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2998 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2999 | /* |
| 3000 | * Try to fix it by moving the start up. |
| 3001 | */ |
| 3002 | if (align_off + temp <= orig_off) { |
| 3003 | align_alen -= temp; |
| 3004 | align_off += temp; |
| 3005 | } |
| 3006 | /* |
| 3007 | * Try to fix it by moving the end in. |
| 3008 | */ |
| 3009 | else if (align_off + align_alen - temp >= orig_end) |
| 3010 | align_alen -= temp; |
| 3011 | /* |
| 3012 | * Set the start to the minimum then trim the length. |
| 3013 | */ |
| 3014 | else { |
| 3015 | align_alen -= orig_off - align_off; |
| 3016 | align_off = orig_off; |
| 3017 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3018 | } |
| 3019 | /* |
| 3020 | * Result doesn't cover the request, fail it. |
| 3021 | */ |
| 3022 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3023 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3024 | } else { |
| 3025 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3026 | /* see MAXEXTLEN handling above */ |
| 3027 | ASSERT(orig_end <= align_off + align_alen || |
| 3028 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | #ifdef DEBUG |
| 3032 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3033 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3034 | if (prevp->br_startoff != NULLFILEOFF) |
| 3035 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3036 | #endif |
| 3037 | |
| 3038 | *lenp = align_alen; |
| 3039 | *offp = align_off; |
| 3040 | return 0; |
| 3041 | } |
| 3042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3044 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3045 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3046 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3047 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | { |
| 3049 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3051 | xfs_mount_t *mp; /* mount point structure */ |
| 3052 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3053 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | |
| 3055 | #define ISVALID(x,y) \ |
| 3056 | (rt ? \ |
| 3057 | (x) < mp->m_sb.sb_rblocks : \ |
| 3058 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3059 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3060 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | mp = ap->ip->i_mount; |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3063 | nullfb = ap->tp->t_firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3064 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3065 | xfs_alloc_is_userdata(ap->datatype); |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3066 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, |
| 3067 | ap->tp->t_firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | /* |
| 3069 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3070 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3072 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3073 | !isnullstartblock(ap->prev.br_startblock) && |
| 3074 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3075 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3076 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | /* |
| 3078 | * Adjust for the gap between prevp and us. |
| 3079 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3080 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3081 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3083 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3084 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | } |
| 3086 | /* |
| 3087 | * If not at eof, then compare the two neighbor blocks. |
| 3088 | * Figure out whether either one gives us a good starting point, |
| 3089 | * and pick the better one. |
| 3090 | */ |
| 3091 | else if (!ap->eof) { |
| 3092 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3093 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3094 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3095 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3096 | |
| 3097 | /* |
| 3098 | * If there's a previous (left) block, select a requested |
| 3099 | * start block based on it. |
| 3100 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3101 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3102 | !isnullstartblock(ap->prev.br_startblock) && |
| 3103 | (prevbno = ap->prev.br_startblock + |
| 3104 | ap->prev.br_blockcount) && |
| 3105 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | /* |
| 3107 | * Calculate gap to end of previous block. |
| 3108 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3109 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3110 | (ap->prev.br_startoff + |
| 3111 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | /* |
| 3113 | * Figure the startblock based on the previous block's |
| 3114 | * end and the gap size. |
| 3115 | * Heuristic! |
| 3116 | * If the gap is large relative to the piece we're |
| 3117 | * allocating, or using it gives us an invalid block |
| 3118 | * number, then just use the end of the previous block. |
| 3119 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3120 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3122 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | prevbno += adjust; |
| 3124 | else |
| 3125 | prevdiff += adjust; |
| 3126 | /* |
| 3127 | * If the firstblock forbids it, can't use it, |
| 3128 | * must use default. |
| 3129 | */ |
| 3130 | if (!rt && !nullfb && |
| 3131 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3132 | prevbno = NULLFSBLOCK; |
| 3133 | } |
| 3134 | /* |
| 3135 | * No previous block or can't follow it, just default. |
| 3136 | */ |
| 3137 | else |
| 3138 | prevbno = NULLFSBLOCK; |
| 3139 | /* |
| 3140 | * If there's a following (right) block, select a requested |
| 3141 | * start block based on it. |
| 3142 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3143 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | /* |
| 3145 | * Calculate gap to start of next block. |
| 3146 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3147 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | /* |
| 3149 | * Figure the startblock based on the next block's |
| 3150 | * start and the gap size. |
| 3151 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3152 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | /* |
| 3154 | * Heuristic! |
| 3155 | * If the gap is large relative to the piece we're |
| 3156 | * allocating, or using it gives us an invalid block |
| 3157 | * number, then just use the start of the next block |
| 3158 | * offset by our length. |
| 3159 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3160 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3162 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3163 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3164 | gotbno -= ap->length; |
| 3165 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | } else |
| 3167 | gotdiff += adjust; |
| 3168 | /* |
| 3169 | * If the firstblock forbids it, can't use it, |
| 3170 | * must use default. |
| 3171 | */ |
| 3172 | if (!rt && !nullfb && |
| 3173 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3174 | gotbno = NULLFSBLOCK; |
| 3175 | } |
| 3176 | /* |
| 3177 | * No next block, just default. |
| 3178 | */ |
| 3179 | else |
| 3180 | gotbno = NULLFSBLOCK; |
| 3181 | /* |
| 3182 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3183 | * 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] | 3184 | */ |
| 3185 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3186 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3188 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3190 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3192 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3193 | } |
| 3194 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3195 | static int |
| 3196 | xfs_bmap_longest_free_extent( |
| 3197 | struct xfs_trans *tp, |
| 3198 | xfs_agnumber_t ag, |
| 3199 | xfs_extlen_t *blen, |
| 3200 | int *notinit) |
| 3201 | { |
| 3202 | struct xfs_mount *mp = tp->t_mountp; |
| 3203 | struct xfs_perag *pag; |
| 3204 | xfs_extlen_t longest; |
| 3205 | int error = 0; |
| 3206 | |
| 3207 | pag = xfs_perag_get(mp, ag); |
| 3208 | if (!pag->pagf_init) { |
| 3209 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3210 | if (error) |
| 3211 | goto out; |
| 3212 | |
| 3213 | if (!pag->pagf_init) { |
| 3214 | *notinit = 1; |
| 3215 | goto out; |
| 3216 | } |
| 3217 | } |
| 3218 | |
Eric Sandeen | a1f6941 | 2018-04-06 10:09:42 -0700 | [diff] [blame] | 3219 | longest = xfs_alloc_longest_free_extent(pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3220 | xfs_alloc_min_freelist(mp, pag), |
| 3221 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3222 | if (*blen < longest) |
| 3223 | *blen = longest; |
| 3224 | |
| 3225 | out: |
| 3226 | xfs_perag_put(pag); |
| 3227 | return error; |
| 3228 | } |
| 3229 | |
| 3230 | static void |
| 3231 | xfs_bmap_select_minlen( |
| 3232 | struct xfs_bmalloca *ap, |
| 3233 | struct xfs_alloc_arg *args, |
| 3234 | xfs_extlen_t *blen, |
| 3235 | int notinit) |
| 3236 | { |
| 3237 | if (notinit || *blen < ap->minlen) { |
| 3238 | /* |
| 3239 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3240 | * there is space for this request. |
| 3241 | */ |
| 3242 | args->minlen = ap->minlen; |
| 3243 | } else if (*blen < args->maxlen) { |
| 3244 | /* |
| 3245 | * If the best seen length is less than the request length, |
| 3246 | * use the best as the minimum. |
| 3247 | */ |
| 3248 | args->minlen = *blen; |
| 3249 | } else { |
| 3250 | /* |
| 3251 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3252 | * as the minimum. |
| 3253 | */ |
| 3254 | args->minlen = args->maxlen; |
| 3255 | } |
| 3256 | } |
| 3257 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3258 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3259 | xfs_bmap_btalloc_nullfb( |
| 3260 | struct xfs_bmalloca *ap, |
| 3261 | struct xfs_alloc_arg *args, |
| 3262 | xfs_extlen_t *blen) |
| 3263 | { |
| 3264 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3265 | xfs_agnumber_t ag, startag; |
| 3266 | int notinit = 0; |
| 3267 | int error; |
| 3268 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3269 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3270 | args->total = ap->total; |
| 3271 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3272 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3273 | if (startag == NULLAGNUMBER) |
| 3274 | startag = ag = 0; |
| 3275 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3276 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3277 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3278 | ¬init); |
| 3279 | if (error) |
| 3280 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3281 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3282 | if (++ag == mp->m_sb.sb_agcount) |
| 3283 | ag = 0; |
| 3284 | if (ag == startag) |
| 3285 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3286 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3287 | |
| 3288 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3289 | return 0; |
| 3290 | } |
| 3291 | |
| 3292 | STATIC int |
| 3293 | xfs_bmap_btalloc_filestreams( |
| 3294 | struct xfs_bmalloca *ap, |
| 3295 | struct xfs_alloc_arg *args, |
| 3296 | xfs_extlen_t *blen) |
| 3297 | { |
| 3298 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3299 | xfs_agnumber_t ag; |
| 3300 | int notinit = 0; |
| 3301 | int error; |
| 3302 | |
| 3303 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3304 | args->total = ap->total; |
| 3305 | |
| 3306 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3307 | if (ag == NULLAGNUMBER) |
| 3308 | ag = 0; |
| 3309 | |
| 3310 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3311 | if (error) |
| 3312 | return error; |
| 3313 | |
| 3314 | if (*blen < args->maxlen) { |
| 3315 | error = xfs_filestream_new_ag(ap, &ag); |
| 3316 | if (error) |
| 3317 | return error; |
| 3318 | |
| 3319 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3320 | ¬init); |
| 3321 | if (error) |
| 3322 | return error; |
| 3323 | |
| 3324 | } |
| 3325 | |
| 3326 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3327 | |
| 3328 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3329 | * Set the failure fallback case to look in the selected AG as stream |
| 3330 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3331 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3332 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3333 | return 0; |
| 3334 | } |
| 3335 | |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3336 | /* Update all inode and quota accounting for the allocation we just did. */ |
| 3337 | static void |
| 3338 | xfs_bmap_btalloc_accounting( |
| 3339 | struct xfs_bmalloca *ap, |
| 3340 | struct xfs_alloc_arg *args) |
| 3341 | { |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3342 | if (ap->flags & XFS_BMAPI_COWFORK) { |
| 3343 | /* |
| 3344 | * COW fork blocks are in-core only and thus are treated as |
| 3345 | * in-core quota reservation (like delalloc blocks) even when |
| 3346 | * converted to real blocks. The quota reservation is not |
| 3347 | * accounted to disk until blocks are remapped to the data |
| 3348 | * fork. So if these blocks were previously delalloc, we |
| 3349 | * already have quota reservation and there's nothing to do |
| 3350 | * yet. |
| 3351 | */ |
| 3352 | if (ap->wasdel) |
| 3353 | return; |
| 3354 | |
| 3355 | /* |
| 3356 | * Otherwise, we've allocated blocks in a hole. The transaction |
| 3357 | * has acquired in-core quota reservation for this extent. |
| 3358 | * Rather than account these as real blocks, however, we reduce |
| 3359 | * the transaction quota reservation based on the allocation. |
| 3360 | * This essentially transfers the transaction quota reservation |
| 3361 | * to that of a delalloc extent. |
| 3362 | */ |
| 3363 | ap->ip->i_delayed_blks += args->len; |
| 3364 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS, |
| 3365 | -(long)args->len); |
| 3366 | return; |
| 3367 | } |
| 3368 | |
| 3369 | /* data/attr fork only */ |
| 3370 | ap->ip->i_d.di_nblocks += args->len; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3371 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3372 | if (ap->wasdel) |
| 3373 | ap->ip->i_delayed_blks -= args->len; |
| 3374 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 3375 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT, |
| 3376 | args->len); |
| 3377 | } |
| 3378 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3379 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3380 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3381 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3382 | { |
| 3383 | xfs_mount_t *mp; /* mount point structure */ |
| 3384 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3385 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3386 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3387 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3388 | xfs_alloc_arg_t args; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3389 | xfs_fileoff_t orig_offset; |
| 3390 | xfs_extlen_t orig_length; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3391 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3392 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3393 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3394 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3395 | int tryagain; |
| 3396 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3397 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3398 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3399 | ASSERT(ap->length); |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3400 | orig_offset = ap->offset; |
| 3401 | orig_length = ap->length; |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3402 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3403 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3404 | |
| 3405 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3406 | stripe_align = 0; |
| 3407 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3408 | stripe_align = mp->m_swidth; |
| 3409 | else if (mp->m_dalign) |
| 3410 | stripe_align = mp->m_dalign; |
| 3411 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3412 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3413 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3414 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3415 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3416 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3417 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3418 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3419 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3420 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3421 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3422 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3423 | |
| 3424 | |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3425 | nullfb = ap->tp->t_firstblock == NULLFSBLOCK; |
| 3426 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, |
| 3427 | ap->tp->t_firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3428 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3429 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3430 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3431 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3432 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3433 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3434 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3435 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3436 | } |
| 3437 | } else |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3438 | ap->blkno = ap->tp->t_firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3439 | |
| 3440 | xfs_bmap_adjacent(ap); |
| 3441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3443 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | * it's in the right allocation group. |
| 3445 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3446 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | ; |
| 3448 | else |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3449 | ap->blkno = ap->tp->t_firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | * Normal allocation, done through xfs_alloc_vextent. |
| 3452 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3453 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3454 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3455 | args.tp = ap->tp; |
| 3456 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3457 | args.fsbno = ap->blkno; |
Darrick J. Wong | 7280fed | 2018-12-12 08:46:23 -0800 | [diff] [blame] | 3458 | args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3459 | |
| 3460 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 9bb54cb | 2018-06-07 07:54:02 -0700 | [diff] [blame] | 3461 | args.maxlen = min(ap->length, mp->m_ag_max_usable); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3462 | blen = 0; |
| 3463 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3464 | /* |
| 3465 | * Search for an allocation group with a single extent large |
| 3466 | * enough for the request. If one isn't found, then adjust |
| 3467 | * the minimum allocation size to the largest space found. |
| 3468 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3469 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3470 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3471 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3472 | else |
| 3473 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3474 | if (error) |
| 3475 | return error; |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3476 | } else if (ap->tp->t_flags & XFS_TRANS_LOWMODE) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3477 | if (xfs_inode_is_filestream(ap->ip)) |
| 3478 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3479 | else |
| 3480 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3481 | args.total = args.minlen = ap->minlen; |
| 3482 | } else { |
| 3483 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3484 | args.total = ap->total; |
| 3485 | args.minlen = ap->minlen; |
| 3486 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3487 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3488 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3489 | args.prod = align; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3490 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3491 | if (args.mod) |
| 3492 | args.mod = args.prod - args.mod; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3493 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3494 | args.prod = 1; |
| 3495 | args.mod = 0; |
| 3496 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3497 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3498 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3499 | if (args.mod) |
| 3500 | args.mod = args.prod - args.mod; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3501 | } |
| 3502 | /* |
| 3503 | * If we are not low on available data blocks, and the |
| 3504 | * underlying logical volume manager is a stripe, and |
| 3505 | * the file offset is zero then try to allocate data |
| 3506 | * blocks on stripe unit boundary. |
| 3507 | * NOTE: ap->aeof is only set if the allocation length |
| 3508 | * is >= the stripe unit and the allocation offset is |
| 3509 | * at the end of file. |
| 3510 | */ |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3511 | if (!(ap->tp->t_flags & XFS_TRANS_LOWMODE) && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3512 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3513 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3514 | atype = args.type; |
| 3515 | isaligned = 1; |
| 3516 | /* |
| 3517 | * Adjust for alignment |
| 3518 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3519 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3520 | args.minlen = blen - args.alignment; |
| 3521 | args.minalignslop = 0; |
| 3522 | } else { |
| 3523 | /* |
| 3524 | * First try an exact bno allocation. |
| 3525 | * If it fails then do a near or start bno |
| 3526 | * allocation with alignment turned on. |
| 3527 | */ |
| 3528 | atype = args.type; |
| 3529 | tryagain = 1; |
| 3530 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3531 | args.alignment = 1; |
| 3532 | /* |
| 3533 | * Compute the minlen+alignment for the |
| 3534 | * next case. Set slop so that the value |
| 3535 | * of minlen+alignment+slop doesn't go up |
| 3536 | * between the calls. |
| 3537 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3538 | if (blen > stripe_align && blen <= args.maxlen) |
| 3539 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3540 | else |
| 3541 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3542 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3543 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3544 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3545 | args.minlen - 1; |
| 3546 | else |
| 3547 | args.minalignslop = 0; |
| 3548 | } |
| 3549 | } else { |
| 3550 | args.alignment = 1; |
| 3551 | args.minalignslop = 0; |
| 3552 | } |
| 3553 | args.minleft = ap->minleft; |
| 3554 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3555 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3556 | args.datatype = ap->datatype; |
| 3557 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3558 | args.ip = ap->ip; |
| 3559 | |
| 3560 | error = xfs_alloc_vextent(&args); |
| 3561 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3562 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3563 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3564 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3565 | /* |
| 3566 | * Exact allocation failed. Now try with alignment |
| 3567 | * turned on. |
| 3568 | */ |
| 3569 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3570 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3571 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3572 | args.minlen = nextminlen; |
| 3573 | args.minalignslop = 0; |
| 3574 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3575 | if ((error = xfs_alloc_vextent(&args))) |
| 3576 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3577 | } |
| 3578 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3579 | /* |
| 3580 | * allocation failed, so turn off alignment and |
| 3581 | * try again. |
| 3582 | */ |
| 3583 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3584 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3585 | args.alignment = 0; |
| 3586 | if ((error = xfs_alloc_vextent(&args))) |
| 3587 | return error; |
| 3588 | } |
| 3589 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3590 | args.minlen > ap->minlen) { |
| 3591 | args.minlen = ap->minlen; |
| 3592 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3593 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3594 | if ((error = xfs_alloc_vextent(&args))) |
| 3595 | return error; |
| 3596 | } |
| 3597 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3598 | args.fsbno = 0; |
| 3599 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3600 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3601 | if ((error = xfs_alloc_vextent(&args))) |
| 3602 | return error; |
Brian Foster | 1214f1c | 2018-08-01 07:20:31 -0700 | [diff] [blame] | 3603 | ap->tp->t_flags |= XFS_TRANS_LOWMODE; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3604 | } |
| 3605 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3606 | /* |
| 3607 | * check the allocation happened at the same or higher AG than |
| 3608 | * the first block that was allocated. |
| 3609 | */ |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3610 | ASSERT(ap->tp->t_firstblock == NULLFSBLOCK || |
| 3611 | XFS_FSB_TO_AGNO(mp, ap->tp->t_firstblock) <= |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3612 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3613 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3614 | ap->blkno = args.fsbno; |
Brian Foster | 94c07b4 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 3615 | if (ap->tp->t_firstblock == NULLFSBLOCK) |
| 3616 | ap->tp->t_firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3617 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3618 | ap->length = args.len; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3619 | /* |
| 3620 | * If the extent size hint is active, we tried to round the |
| 3621 | * caller's allocation request offset down to extsz and the |
| 3622 | * length up to another extsz boundary. If we found a free |
| 3623 | * extent we mapped it in starting at this new offset. If the |
| 3624 | * newly mapped space isn't long enough to cover any of the |
| 3625 | * range of offsets that was originally requested, move the |
| 3626 | * mapping up so that we can fill as much of the caller's |
| 3627 | * original request as possible. Free space is apparently |
| 3628 | * very fragmented so we're unlikely to be able to satisfy the |
| 3629 | * hints anyway. |
| 3630 | */ |
| 3631 | if (ap->length <= orig_length) |
| 3632 | ap->offset = orig_offset; |
| 3633 | else if (ap->offset + ap->length < orig_offset + orig_length) |
| 3634 | ap->offset = orig_offset + orig_length - ap->length; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3635 | xfs_bmap_btalloc_accounting(ap, &args); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3636 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3637 | ap->blkno = NULLFSBLOCK; |
| 3638 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3639 | } |
| 3640 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3641 | } |
| 3642 | |
| 3643 | /* |
| 3644 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3645 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3646 | */ |
| 3647 | STATIC int |
| 3648 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3649 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3650 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3651 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3652 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3653 | return xfs_bmap_rtalloc(ap); |
| 3654 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3655 | } |
| 3656 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3657 | /* Trim extent to fit a logical block range. */ |
| 3658 | void |
| 3659 | xfs_trim_extent( |
| 3660 | struct xfs_bmbt_irec *irec, |
| 3661 | xfs_fileoff_t bno, |
| 3662 | xfs_filblks_t len) |
| 3663 | { |
| 3664 | xfs_fileoff_t distance; |
| 3665 | xfs_fileoff_t end = bno + len; |
| 3666 | |
| 3667 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3668 | irec->br_startoff >= end) { |
| 3669 | irec->br_blockcount = 0; |
| 3670 | return; |
| 3671 | } |
| 3672 | |
| 3673 | if (irec->br_startoff < bno) { |
| 3674 | distance = bno - irec->br_startoff; |
| 3675 | if (isnullstartblock(irec->br_startblock)) |
| 3676 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3677 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3678 | irec->br_startblock != HOLESTARTBLOCK) |
| 3679 | irec->br_startblock += distance; |
| 3680 | irec->br_startoff += distance; |
| 3681 | irec->br_blockcount -= distance; |
| 3682 | } |
| 3683 | |
| 3684 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3685 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3686 | irec->br_blockcount -= distance; |
| 3687 | } |
| 3688 | } |
| 3689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3690 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3691 | * Trim the returned map to the required bounds |
| 3692 | */ |
| 3693 | STATIC void |
| 3694 | xfs_bmapi_trim_map( |
| 3695 | struct xfs_bmbt_irec *mval, |
| 3696 | struct xfs_bmbt_irec *got, |
| 3697 | xfs_fileoff_t *bno, |
| 3698 | xfs_filblks_t len, |
| 3699 | xfs_fileoff_t obno, |
| 3700 | xfs_fileoff_t end, |
| 3701 | int n, |
| 3702 | int flags) |
| 3703 | { |
| 3704 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3705 | got->br_startoff + got->br_blockcount <= obno) { |
| 3706 | *mval = *got; |
| 3707 | if (isnullstartblock(got->br_startblock)) |
| 3708 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3709 | return; |
| 3710 | } |
| 3711 | |
| 3712 | if (obno > *bno) |
| 3713 | *bno = obno; |
| 3714 | ASSERT((*bno >= obno) || (n == 0)); |
| 3715 | ASSERT(*bno < end); |
| 3716 | mval->br_startoff = *bno; |
| 3717 | if (isnullstartblock(got->br_startblock)) |
| 3718 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3719 | else |
| 3720 | mval->br_startblock = got->br_startblock + |
| 3721 | (*bno - got->br_startoff); |
| 3722 | /* |
| 3723 | * Return the minimum of what we got and what we asked for for |
| 3724 | * the length. We can use the len variable here because it is |
| 3725 | * modified below and we could have been there before coming |
| 3726 | * here if the first part of the allocation didn't overlap what |
| 3727 | * was asked for. |
| 3728 | */ |
| 3729 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3730 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3731 | mval->br_state = got->br_state; |
| 3732 | ASSERT(mval->br_blockcount <= len); |
| 3733 | return; |
| 3734 | } |
| 3735 | |
| 3736 | /* |
| 3737 | * Update and validate the extent map to return |
| 3738 | */ |
| 3739 | STATIC void |
| 3740 | xfs_bmapi_update_map( |
| 3741 | struct xfs_bmbt_irec **map, |
| 3742 | xfs_fileoff_t *bno, |
| 3743 | xfs_filblks_t *len, |
| 3744 | xfs_fileoff_t obno, |
| 3745 | xfs_fileoff_t end, |
| 3746 | int *n, |
| 3747 | int flags) |
| 3748 | { |
| 3749 | xfs_bmbt_irec_t *mval = *map; |
| 3750 | |
| 3751 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3752 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3753 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3754 | (mval->br_startoff < obno)); |
| 3755 | |
| 3756 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3757 | *len = end - *bno; |
| 3758 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3759 | /* update previous map with new information */ |
| 3760 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3761 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3762 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3763 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3764 | mval[-1].br_state = mval->br_state; |
| 3765 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3766 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3767 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3768 | mval->br_startblock == mval[-1].br_startblock + |
| 3769 | mval[-1].br_blockcount && |
Christoph Hellwig | c3a2f9f | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 3770 | mval[-1].br_state == mval->br_state) { |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3771 | ASSERT(mval->br_startoff == |
| 3772 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3773 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3774 | } else if (*n > 0 && |
| 3775 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3776 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3777 | mval->br_startoff == |
| 3778 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3779 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3780 | mval[-1].br_state = mval->br_state; |
| 3781 | } else if (!((*n == 0) && |
| 3782 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3783 | obno))) { |
| 3784 | mval++; |
| 3785 | (*n)++; |
| 3786 | } |
| 3787 | *map = mval; |
| 3788 | } |
| 3789 | |
| 3790 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3791 | * Map file blocks to filesystem blocks without allocation. |
| 3792 | */ |
| 3793 | int |
| 3794 | xfs_bmapi_read( |
| 3795 | struct xfs_inode *ip, |
| 3796 | xfs_fileoff_t bno, |
| 3797 | xfs_filblks_t len, |
| 3798 | struct xfs_bmbt_irec *mval, |
| 3799 | int *nmap, |
| 3800 | int flags) |
| 3801 | { |
| 3802 | struct xfs_mount *mp = ip->i_mount; |
| 3803 | struct xfs_ifork *ifp; |
| 3804 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3805 | xfs_fileoff_t obno; |
| 3806 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3807 | struct xfs_iext_cursor icur; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3808 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3809 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3810 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3811 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3812 | |
| 3813 | ASSERT(*nmap >= 1); |
| 3814 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Christoph Hellwig | c3a2f9f | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 3815 | XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3816 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3817 | |
| 3818 | if (unlikely(XFS_TEST_ERROR( |
| 3819 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3820 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 3821 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3822 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3823 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
| 3826 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3827 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3828 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3829 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3830 | |
| 3831 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3832 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3833 | /* No CoW fork? Return a hole. */ |
| 3834 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 3835 | mval->br_startoff = bno; |
| 3836 | mval->br_startblock = HOLESTARTBLOCK; |
| 3837 | mval->br_blockcount = len; |
| 3838 | mval->br_state = XFS_EXT_NORM; |
| 3839 | *nmap = 1; |
| 3840 | return 0; |
| 3841 | } |
| 3842 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3843 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3844 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3845 | if (error) |
| 3846 | return error; |
| 3847 | } |
| 3848 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3849 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3850 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3851 | end = bno + len; |
| 3852 | obno = bno; |
| 3853 | |
| 3854 | while (bno < end && n < *nmap) { |
| 3855 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3856 | if (eof) |
| 3857 | got.br_startoff = end; |
| 3858 | if (got.br_startoff > bno) { |
| 3859 | /* Reading in a hole. */ |
| 3860 | mval->br_startoff = bno; |
| 3861 | mval->br_startblock = HOLESTARTBLOCK; |
| 3862 | mval->br_blockcount = |
| 3863 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3864 | mval->br_state = XFS_EXT_NORM; |
| 3865 | bno += mval->br_blockcount; |
| 3866 | len -= mval->br_blockcount; |
| 3867 | mval++; |
| 3868 | n++; |
| 3869 | continue; |
| 3870 | } |
| 3871 | |
| 3872 | /* set up the extent map to return. */ |
| 3873 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3874 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3875 | |
| 3876 | /* If we're done, stop now. */ |
| 3877 | if (bno >= end || n >= *nmap) |
| 3878 | break; |
| 3879 | |
| 3880 | /* Else go on to the next record. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3881 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3882 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3883 | } |
| 3884 | *nmap = n; |
| 3885 | return 0; |
| 3886 | } |
| 3887 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3888 | /* |
| 3889 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3890 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3891 | * |
| 3892 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3893 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3894 | * to the extent record that was inserted to the inode fork. |
| 3895 | * |
| 3896 | * Note that the allocated extent may have been merged with contiguous extents |
| 3897 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3898 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3899 | * look up the updated record in the inode fork. |
| 3900 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3901 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3902 | xfs_bmapi_reserve_delalloc( |
| 3903 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3904 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3905 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3906 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3907 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3908 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3909 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3910 | int eof) |
| 3911 | { |
| 3912 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3913 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3914 | xfs_extlen_t alen; |
| 3915 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3916 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3917 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3918 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3919 | /* |
| 3920 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3921 | * tag the inode before we return. |
| 3922 | */ |
| 3923 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3924 | if (!eof) |
| 3925 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3926 | if (prealloc && alen >= len) |
| 3927 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3928 | |
| 3929 | /* Figure out the extent size, adjust alen */ |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3930 | if (whichfork == XFS_COW_FORK) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3931 | struct xfs_bmbt_irec prev; |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3932 | xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3933 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3934 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3935 | prev.br_startoff = NULLFILEOFF; |
| 3936 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3937 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3938 | 1, 0, &aoff, &alen); |
| 3939 | ASSERT(!error); |
| 3940 | } |
| 3941 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3942 | /* |
| 3943 | * Make a transaction-less quota reservation for delayed allocation |
| 3944 | * blocks. This number gets adjusted later. We return if we haven't |
| 3945 | * allocated blocks already inside this loop. |
| 3946 | */ |
| 3947 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3948 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3949 | if (error) |
| 3950 | return error; |
| 3951 | |
| 3952 | /* |
| 3953 | * Split changing sb for alen and indlen since they could be coming |
| 3954 | * from different places. |
| 3955 | */ |
| 3956 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 3957 | ASSERT(indlen > 0); |
| 3958 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3959 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3960 | if (error) |
| 3961 | goto out_unreserve_quota; |
| 3962 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3963 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3964 | if (error) |
| 3965 | goto out_unreserve_blocks; |
| 3966 | |
| 3967 | |
| 3968 | ip->i_delayed_blks += alen; |
| 3969 | |
| 3970 | got->br_startoff = aoff; |
| 3971 | got->br_startblock = nullstartblock(indlen); |
| 3972 | got->br_blockcount = alen; |
| 3973 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3974 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3975 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3976 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3977 | /* |
| 3978 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 3979 | * preallocation can occur at the start or end of the extent, even when |
| 3980 | * prealloc == 0, so we must also check the aligned offset and length. |
| 3981 | */ |
| 3982 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 3983 | xfs_inode_set_eofblocks_tag(ip); |
| 3984 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 3985 | xfs_inode_set_cowblocks_tag(ip); |
| 3986 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3987 | return 0; |
| 3988 | |
| 3989 | out_unreserve_blocks: |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3990 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3991 | out_unreserve_quota: |
| 3992 | if (XFS_IS_QUOTA_ON(mp)) |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3993 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 3994 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3995 | return error; |
| 3996 | } |
| 3997 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 3998 | static int |
| 3999 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4000 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4001 | { |
| 4002 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4003 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4004 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4005 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4006 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4007 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4008 | ASSERT(bma->length > 0); |
| 4009 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4010 | /* |
| 4011 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4012 | * for in this bmap call but that wouldn't be as good. |
| 4013 | */ |
| 4014 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4015 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4016 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4017 | xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4018 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4019 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4020 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4021 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4022 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4026 | * Set the data type being allocated. For the data fork, the first data |
| 4027 | * in the file is treated differently to all other allocations. For the |
| 4028 | * attribute fork, we only need to ensure the allocated range is not on |
| 4029 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4030 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4031 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4032 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4033 | if (whichfork == XFS_DATA_FORK) { |
| 4034 | if (bma->offset == 0) |
| 4035 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4036 | else |
| 4037 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4038 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4039 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4040 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4043 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4044 | |
| 4045 | /* |
| 4046 | * Only want to do the alignment at the eof if it is userdata and |
| 4047 | * allocation length is larger than a stripe unit. |
| 4048 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4049 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4050 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4051 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4052 | if (error) |
| 4053 | return error; |
| 4054 | } |
| 4055 | |
| 4056 | error = xfs_bmap_alloc(bma); |
| 4057 | if (error) |
| 4058 | return error; |
| 4059 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4060 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4061 | return 0; |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 4062 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4063 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4064 | /* |
| 4065 | * Bump the number of extents we've allocated |
| 4066 | * in this call. |
| 4067 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4068 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4069 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4070 | if (bma->cur) |
| 4071 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4072 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4073 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4074 | bma->got.br_startoff = bma->offset; |
| 4075 | bma->got.br_startblock = bma->blkno; |
| 4076 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4077 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4078 | |
| 4079 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4080 | * In the data fork, a wasdelay extent has been initialized, so |
| 4081 | * shouldn't be flagged as unwritten. |
| 4082 | * |
| 4083 | * For the cow fork, however, we convert delalloc reservations |
| 4084 | * (extents allocated for speculative preallocation) to |
| 4085 | * allocated unwritten extents, and only convert the unwritten |
| 4086 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4087 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4088 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 4089 | (bma->flags & XFS_BMAPI_PREALLOC)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4090 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4091 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4092 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4093 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4094 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4095 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4096 | whichfork, &bma->icur, &bma->cur, &bma->got, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4097 | &bma->logflags, bma->flags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4098 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4099 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4100 | if (error) |
| 4101 | return error; |
| 4102 | |
| 4103 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4104 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4105 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4106 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4107 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4108 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4109 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4110 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4111 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4112 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4113 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4114 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4115 | return 0; |
| 4116 | } |
| 4117 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4118 | STATIC int |
| 4119 | xfs_bmapi_convert_unwritten( |
| 4120 | struct xfs_bmalloca *bma, |
| 4121 | struct xfs_bmbt_irec *mval, |
| 4122 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4123 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4124 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4125 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4126 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4127 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4128 | int error; |
| 4129 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4130 | /* check if we need to do unwritten->real conversion */ |
| 4131 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4132 | (flags & XFS_BMAPI_PREALLOC)) |
| 4133 | return 0; |
| 4134 | |
| 4135 | /* check if we need to do real->unwritten conversion */ |
| 4136 | if (mval->br_state == XFS_EXT_NORM && |
| 4137 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4138 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4139 | return 0; |
| 4140 | |
| 4141 | /* |
| 4142 | * Modify (by adding) the state flag, if writing. |
| 4143 | */ |
| 4144 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4145 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4146 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4147 | bma->ip, whichfork); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4148 | } |
| 4149 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4150 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4151 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4152 | /* |
| 4153 | * Before insertion into the bmbt, zero the range being converted |
| 4154 | * if required. |
| 4155 | */ |
| 4156 | if (flags & XFS_BMAPI_ZERO) { |
| 4157 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4158 | mval->br_blockcount); |
| 4159 | if (error) |
| 4160 | return error; |
| 4161 | } |
| 4162 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4163 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4164 | &bma->icur, &bma->cur, mval, &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4165 | /* |
| 4166 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4167 | * path because the conversion might not have done so (e.g., if the |
| 4168 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4169 | * in the transaction for the sake of fsync(), even if nothing has |
| 4170 | * changed, because fsync() will not force the log for this transaction |
| 4171 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4172 | * |
| 4173 | * Note: If we're only converting cow fork extents, there aren't |
| 4174 | * 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] | 4175 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4176 | if (whichfork != XFS_COW_FORK) |
| 4177 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4178 | if (error) |
| 4179 | return error; |
| 4180 | |
| 4181 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4182 | * Update our extent pointer, given that |
| 4183 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4184 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4185 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4186 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4187 | |
| 4188 | /* |
| 4189 | * We may have combined previously unwritten space with written space, |
| 4190 | * so generate another request. |
| 4191 | */ |
| 4192 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4193 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4194 | return 0; |
| 4195 | } |
| 4196 | |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4197 | static inline xfs_extlen_t |
| 4198 | xfs_bmapi_minleft( |
| 4199 | struct xfs_trans *tp, |
| 4200 | struct xfs_inode *ip, |
| 4201 | int fork) |
| 4202 | { |
| 4203 | if (tp && tp->t_firstblock != NULLFSBLOCK) |
| 4204 | return 0; |
| 4205 | if (XFS_IFORK_FORMAT(ip, fork) != XFS_DINODE_FMT_BTREE) |
| 4206 | return 1; |
| 4207 | return be16_to_cpu(XFS_IFORK_PTR(ip, fork)->if_broot->bb_level) + 1; |
| 4208 | } |
| 4209 | |
| 4210 | /* |
| 4211 | * Log whatever the flags say, even if error. Otherwise we might miss detecting |
| 4212 | * a case where the data is changed, there's an error, and it's not logged so we |
| 4213 | * don't shutdown when we should. Don't bother logging extents/btree changes if |
| 4214 | * we converted to the other format. |
| 4215 | */ |
| 4216 | static void |
| 4217 | xfs_bmapi_finish( |
| 4218 | struct xfs_bmalloca *bma, |
| 4219 | int whichfork, |
| 4220 | int error) |
| 4221 | { |
| 4222 | if ((bma->logflags & xfs_ilog_fext(whichfork)) && |
| 4223 | XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 4224 | bma->logflags &= ~xfs_ilog_fext(whichfork); |
| 4225 | else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) && |
| 4226 | XFS_IFORK_FORMAT(bma->ip, whichfork) != XFS_DINODE_FMT_BTREE) |
| 4227 | bma->logflags &= ~xfs_ilog_fbroot(whichfork); |
| 4228 | |
| 4229 | if (bma->logflags) |
| 4230 | xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags); |
| 4231 | if (bma->cur) |
| 4232 | xfs_btree_del_cursor(bma->cur, error); |
| 4233 | } |
| 4234 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4235 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4236 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4237 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4238 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4239 | * locking problems. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4241 | int |
| 4242 | xfs_bmapi_write( |
| 4243 | struct xfs_trans *tp, /* transaction pointer */ |
| 4244 | struct xfs_inode *ip, /* incore inode */ |
| 4245 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4246 | xfs_filblks_t len, /* length to map in file */ |
| 4247 | int flags, /* XFS_BMAPI_... */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4248 | xfs_extlen_t total, /* total blocks needed */ |
| 4249 | struct xfs_bmbt_irec *mval, /* output: map values */ |
Brian Foster | 6e702a5 | 2018-07-11 22:26:12 -0700 | [diff] [blame] | 4250 | int *nmap) /* i/o: mval size/count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4252 | struct xfs_mount *mp = ip->i_mount; |
| 4253 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4254 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4255 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4256 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4257 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4258 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4259 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4260 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4263 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4264 | int orig_flags; /* original flags arg value */ |
| 4265 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4266 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4267 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | |
| 4269 | orig_bno = bno; |
| 4270 | orig_len = len; |
| 4271 | orig_flags = flags; |
| 4272 | orig_mval = mval; |
| 4273 | orig_nmap = *nmap; |
| 4274 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4275 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4278 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 4279 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4280 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4281 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4282 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4283 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4284 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4285 | /* zeroing is for currently only for data extents, not metadata */ |
| 4286 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4287 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4288 | /* |
| 4289 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4290 | * but it makes no sense to do both at once. This would result in |
| 4291 | * zeroing the unwritten extent twice, but it still being an |
| 4292 | * unwritten extent.... |
| 4293 | */ |
| 4294 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4295 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | if (unlikely(XFS_TEST_ERROR( |
| 4298 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4299 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4300 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4301 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4302 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4306 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4309 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4310 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4311 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4312 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4313 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4314 | if (error) |
| 4315 | goto error0; |
| 4316 | } |
| 4317 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4318 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4319 | eof = true; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4320 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4321 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4322 | bma.tp = tp; |
| 4323 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4324 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4325 | bma.datatype = 0; |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4326 | bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4327 | |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4328 | n = 0; |
| 4329 | end = bno + len; |
| 4330 | obno = bno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4332 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4333 | |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4334 | /* in hole or beyond EOF? */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4335 | if (eof || bma.got.br_startoff > bno) { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4336 | /* |
| 4337 | * CoW fork conversions should /never/ hit EOF or |
| 4338 | * holes. There should always be something for us |
| 4339 | * to work on. |
| 4340 | */ |
| 4341 | ASSERT(!((flags & XFS_BMAPI_CONVERT) && |
| 4342 | (flags & XFS_BMAPI_COWFORK))); |
| 4343 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4344 | need_alloc = true; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4345 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4346 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4347 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4348 | |
| 4349 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | * First, deal with the hole before the allocated space |
| 4351 | * that we found, if any. |
| 4352 | */ |
Christoph Hellwig | 26b91c7 | 2019-02-18 09:38:48 -0800 | [diff] [blame] | 4353 | if (need_alloc || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4354 | bma.eof = eof; |
| 4355 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4356 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4357 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4358 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4359 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4360 | /* |
| 4361 | * There's a 32/64 bit type mismatch between the |
| 4362 | * allocation length request (which can be 64 bits in |
| 4363 | * length) and the bma length request, which is |
| 4364 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4365 | * check for 32-bit overflows and handle them here. |
| 4366 | */ |
| 4367 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4368 | bma.length = MAXEXTLEN; |
| 4369 | else |
| 4370 | bma.length = len; |
| 4371 | |
| 4372 | ASSERT(len > 0); |
| 4373 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4374 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | if (error) |
| 4376 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4377 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4378 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4379 | |
| 4380 | /* |
| 4381 | * If this is a CoW allocation, record the data in |
| 4382 | * the refcount btree for orphan recovery. |
| 4383 | */ |
| 4384 | if (whichfork == XFS_COW_FORK) { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 4385 | error = xfs_refcount_alloc_cow_extent(tp, |
| 4386 | bma.blkno, bma.length); |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4387 | if (error) |
| 4388 | goto error0; |
| 4389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4391 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4392 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4393 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4394 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4396 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4397 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4398 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4399 | continue; |
| 4400 | if (error) |
| 4401 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4403 | /* update the extent map to return */ |
| 4404 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | /* |
| 4407 | * If we're done, stop now. Stop when we've allocated |
| 4408 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4409 | * the transaction may get too big. |
| 4410 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4411 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4413 | |
| 4414 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4415 | bma.prev = bma.got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4416 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4417 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4420 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 4421 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, |
| 4422 | whichfork); |
| 4423 | if (error) |
| 4424 | goto error0; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4427 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4428 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4429 | xfs_bmapi_finish(&bma, whichfork, 0); |
| 4430 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4431 | orig_nmap, *nmap); |
| 4432 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | error0: |
Christoph Hellwig | c8b5467 | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4434 | xfs_bmapi_finish(&bma, whichfork, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | return error; |
| 4436 | } |
| 4437 | |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4438 | /* |
| 4439 | * Convert an existing delalloc extent to real blocks based on file offset. This |
| 4440 | * attempts to allocate the entire delalloc extent and may require multiple |
| 4441 | * invocations to allocate the target offset if a large enough physical extent |
| 4442 | * is not available. |
| 4443 | */ |
| 4444 | int |
| 4445 | xfs_bmapi_convert_delalloc( |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4446 | struct xfs_inode *ip, |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4447 | int whichfork, |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4448 | xfs_fileoff_t offset_fsb, |
| 4449 | struct xfs_bmbt_irec *imap, |
| 4450 | unsigned int *seq) |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4451 | { |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4452 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4453 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4454 | struct xfs_bmalloca bma = { NULL }; |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4455 | struct xfs_trans *tp; |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4456 | int error; |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4457 | |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4458 | /* |
| 4459 | * Space for the extent and indirect blocks was reserved when the |
| 4460 | * delalloc extent was created so there's no need to do so here. |
| 4461 | */ |
| 4462 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, |
| 4463 | XFS_TRANS_RESERVE, &tp); |
| 4464 | if (error) |
| 4465 | return error; |
| 4466 | |
| 4467 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 4468 | xfs_trans_ijoin(tp, ip, 0); |
| 4469 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4470 | if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) || |
| 4471 | bma.got.br_startoff > offset_fsb) { |
| 4472 | /* |
| 4473 | * No extent found in the range we are trying to convert. This |
| 4474 | * should only happen for the COW fork, where another thread |
| 4475 | * might have moved the extent to the data fork in the meantime. |
| 4476 | */ |
| 4477 | WARN_ON_ONCE(whichfork != XFS_COW_FORK); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4478 | error = -EAGAIN; |
| 4479 | goto out_trans_cancel; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4480 | } |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4481 | |
| 4482 | /* |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4483 | * If we find a real extent here we raced with another thread converting |
| 4484 | * the extent. Just return the real extent at this offset. |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4485 | */ |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4486 | if (!isnullstartblock(bma.got.br_startblock)) { |
| 4487 | *imap = bma.got; |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4488 | *seq = READ_ONCE(ifp->if_seq); |
| 4489 | goto out_trans_cancel; |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | bma.tp = tp; |
| 4493 | bma.ip = ip; |
| 4494 | bma.wasdel = true; |
| 4495 | bma.offset = bma.got.br_startoff; |
| 4496 | bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN); |
| 4497 | bma.total = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK); |
| 4498 | bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); |
| 4499 | if (whichfork == XFS_COW_FORK) |
| 4500 | bma.flags = XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC; |
| 4501 | |
| 4502 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
| 4503 | bma.prev.br_startoff = NULLFILEOFF; |
| 4504 | |
| 4505 | error = xfs_bmapi_allocate(&bma); |
| 4506 | if (error) |
| 4507 | goto out_finish; |
| 4508 | |
| 4509 | error = -ENOSPC; |
| 4510 | if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK)) |
| 4511 | goto out_finish; |
| 4512 | error = -EFSCORRUPTED; |
| 4513 | if (WARN_ON_ONCE(!bma.got.br_startblock && !XFS_IS_REALTIME_INODE(ip))) |
| 4514 | goto out_finish; |
| 4515 | |
Christoph Hellwig | 125851a | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4516 | XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length)); |
| 4517 | XFS_STATS_INC(mp, xs_xstrat_quick); |
| 4518 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4519 | ASSERT(!isnullstartblock(bma.got.br_startblock)); |
| 4520 | *imap = bma.got; |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4521 | *seq = READ_ONCE(ifp->if_seq); |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4522 | |
| 4523 | if (whichfork == XFS_COW_FORK) { |
| 4524 | error = xfs_refcount_alloc_cow_extent(tp, bma.blkno, |
| 4525 | bma.length); |
| 4526 | if (error) |
| 4527 | goto out_finish; |
| 4528 | } |
| 4529 | |
| 4530 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, |
| 4531 | whichfork); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4532 | if (error) |
| 4533 | goto out_finish; |
| 4534 | |
| 4535 | xfs_bmapi_finish(&bma, whichfork, 0); |
| 4536 | error = xfs_trans_commit(tp); |
| 4537 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 4538 | return error; |
| 4539 | |
Christoph Hellwig | d8ae82e | 2019-02-15 08:02:48 -0800 | [diff] [blame] | 4540 | out_finish: |
| 4541 | xfs_bmapi_finish(&bma, whichfork, error); |
Christoph Hellwig | 491ce61 | 2019-02-15 08:02:49 -0800 | [diff] [blame] | 4542 | out_trans_cancel: |
| 4543 | xfs_trans_cancel(tp); |
| 4544 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Brian Foster | 627209f | 2019-02-01 09:14:23 -0800 | [diff] [blame] | 4545 | return error; |
| 4546 | } |
| 4547 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4548 | int |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4549 | xfs_bmapi_remap( |
| 4550 | struct xfs_trans *tp, |
| 4551 | struct xfs_inode *ip, |
| 4552 | xfs_fileoff_t bno, |
| 4553 | xfs_filblks_t len, |
| 4554 | xfs_fsblock_t startblock, |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4555 | int flags) |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4556 | { |
| 4557 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4558 | struct xfs_ifork *ifp; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4559 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4560 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4561 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4562 | int whichfork = xfs_bmapi_whichfork(flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4563 | int logflags = 0, error; |
| 4564 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4565 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4566 | ASSERT(len > 0); |
| 4567 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4568 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4569 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC | |
| 4570 | XFS_BMAPI_NORMAP))); |
| 4571 | ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) != |
| 4572 | (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4573 | |
| 4574 | if (unlikely(XFS_TEST_ERROR( |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4575 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4576 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4577 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4578 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4579 | return -EFSCORRUPTED; |
| 4580 | } |
| 4581 | |
| 4582 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4583 | return -EIO; |
| 4584 | |
| 4585 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4586 | error = xfs_iread_extents(tp, ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4587 | if (error) |
| 4588 | return error; |
| 4589 | } |
| 4590 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4591 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4592 | /* make sure we only reflink into a hole. */ |
| 4593 | ASSERT(got.br_startoff > bno); |
| 4594 | ASSERT(got.br_startoff - bno >= len); |
| 4595 | } |
| 4596 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4597 | ip->i_d.di_nblocks += len; |
| 4598 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4599 | |
| 4600 | if (ifp->if_flags & XFS_IFBROOT) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4601 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4602 | cur->bc_private.b.flags = 0; |
| 4603 | } |
| 4604 | |
| 4605 | got.br_startoff = bno; |
| 4606 | got.br_startblock = startblock; |
| 4607 | got.br_blockcount = len; |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4608 | if (flags & XFS_BMAPI_PREALLOC) |
| 4609 | got.br_state = XFS_EXT_UNWRITTEN; |
| 4610 | else |
| 4611 | got.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4612 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4613 | error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 4614 | &cur, &got, &logflags, flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4615 | if (error) |
| 4616 | goto error0; |
| 4617 | |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 4618 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4619 | |
| 4620 | error0: |
| 4621 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4622 | logflags &= ~XFS_ILOG_DEXT; |
| 4623 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4624 | logflags &= ~XFS_ILOG_DBROOT; |
| 4625 | |
| 4626 | if (logflags) |
| 4627 | xfs_trans_log_inode(tp, ip, logflags); |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 4628 | if (cur) |
| 4629 | xfs_btree_del_cursor(cur, error); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4630 | return error; |
| 4631 | } |
| 4632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4634 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4635 | * indlen reservation must be shared across the two new extents that are left |
| 4636 | * behind. |
| 4637 | * |
| 4638 | * Given the original reservation and the worst case indlen for the two new |
| 4639 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4640 | * reservation fairly across the two new extents. If necessary, steal available |
| 4641 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4642 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4643 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4644 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4645 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4646 | xfs_bmap_split_indlen( |
| 4647 | xfs_filblks_t ores, /* original res. */ |
| 4648 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4649 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4650 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4651 | { |
| 4652 | xfs_filblks_t len1 = *indlen1; |
| 4653 | xfs_filblks_t len2 = *indlen2; |
| 4654 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4655 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4656 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4657 | |
| 4658 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4659 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4660 | * indlen for both new extents. |
| 4661 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4662 | if (ores < nres && avail) |
| 4663 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4664 | ores += stolen; |
| 4665 | |
| 4666 | /* nothing else to do if we've satisfied the new reservation */ |
| 4667 | if (ores >= nres) |
| 4668 | return stolen; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4669 | |
| 4670 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4671 | * We can't meet the total required reservation for the two extents. |
| 4672 | * Calculate the percent of the overall shortage between both extents |
| 4673 | * and apply this percentage to each of the requested indlen values. |
| 4674 | * This distributes the shortage fairly and reduces the chances that one |
| 4675 | * of the two extents is left with nothing when extents are repeatedly |
| 4676 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4677 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4678 | resfactor = (ores * 100); |
| 4679 | do_div(resfactor, nres); |
| 4680 | len1 *= resfactor; |
| 4681 | do_div(len1, 100); |
| 4682 | len2 *= resfactor; |
| 4683 | do_div(len2, 100); |
| 4684 | ASSERT(len1 + len2 <= ores); |
| 4685 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4686 | |
| 4687 | /* |
| 4688 | * Hand out the remainder to each extent. If one of the two reservations |
| 4689 | * is zero, we want to make sure that one gets a block first. The loop |
| 4690 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4691 | * is zero. |
| 4692 | */ |
| 4693 | ores -= (len1 + len2); |
| 4694 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4695 | if (ores && !len2 && *indlen2) { |
| 4696 | len2++; |
| 4697 | ores--; |
| 4698 | } |
| 4699 | while (ores) { |
| 4700 | if (len1 < *indlen1) { |
| 4701 | len1++; |
| 4702 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4703 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4704 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4705 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4706 | if (len2 < *indlen2) { |
| 4707 | len2++; |
| 4708 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | *indlen1 = len1; |
| 4713 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4714 | |
| 4715 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4716 | } |
| 4717 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4718 | int |
| 4719 | xfs_bmap_del_extent_delay( |
| 4720 | struct xfs_inode *ip, |
| 4721 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4722 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4723 | struct xfs_bmbt_irec *got, |
| 4724 | struct xfs_bmbt_irec *del) |
| 4725 | { |
| 4726 | struct xfs_mount *mp = ip->i_mount; |
| 4727 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4728 | struct xfs_bmbt_irec new; |
| 4729 | int64_t da_old, da_new, da_diff = 0; |
| 4730 | xfs_fileoff_t del_endoff, got_endoff; |
| 4731 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4732 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4733 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4734 | bool isrt; |
| 4735 | |
| 4736 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4737 | |
| 4738 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4739 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4740 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4741 | da_old = startblockval(got->br_startblock); |
| 4742 | da_new = 0; |
| 4743 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4744 | ASSERT(del->br_blockcount > 0); |
| 4745 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4746 | ASSERT(got_endoff >= del_endoff); |
| 4747 | |
| 4748 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4749 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4750 | |
| 4751 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4752 | xfs_mod_frextents(mp, rtexts); |
| 4753 | } |
| 4754 | |
| 4755 | /* |
| 4756 | * Update the inode delalloc counter now and wait to update the |
| 4757 | * sb counters as we might have to borrow some blocks for the |
| 4758 | * indirect block accounting. |
| 4759 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4760 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4761 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4762 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4763 | if (error) |
| 4764 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4765 | ip->i_delayed_blks -= del->br_blockcount; |
| 4766 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4767 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4768 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4769 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4770 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4771 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4772 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4773 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4774 | /* |
| 4775 | * Matches the whole extent. Delete the entry. |
| 4776 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4777 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4778 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4779 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4780 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4781 | /* |
| 4782 | * Deleting the first part of the extent. |
| 4783 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4784 | got->br_startoff = del_endoff; |
| 4785 | got->br_blockcount -= del->br_blockcount; |
| 4786 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4787 | got->br_blockcount), da_old); |
| 4788 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4789 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4790 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4791 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4792 | /* |
| 4793 | * Deleting the last part of the extent. |
| 4794 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4795 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4796 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4797 | got->br_blockcount), da_old); |
| 4798 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4799 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4800 | break; |
| 4801 | case 0: |
| 4802 | /* |
| 4803 | * Deleting the middle of the extent. |
| 4804 | * |
| 4805 | * Distribute the original indlen reservation across the two new |
| 4806 | * extents. Steal blocks from the deleted extent if necessary. |
| 4807 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4808 | * Warn if either of the new indlen reservations is zero as this |
| 4809 | * can lead to delalloc problems. |
| 4810 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4811 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4812 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4813 | |
| 4814 | new.br_blockcount = got_endoff - del_endoff; |
| 4815 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4816 | |
| 4817 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4818 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4819 | del->br_blockcount); |
| 4820 | |
| 4821 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4822 | |
| 4823 | new.br_startoff = del_endoff; |
| 4824 | new.br_state = got->br_state; |
| 4825 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4826 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4827 | xfs_iext_update_extent(ip, state, icur, got); |
| 4828 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4829 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4830 | |
| 4831 | da_new = got_indlen + new_indlen - stolen; |
| 4832 | del->br_blockcount -= stolen; |
| 4833 | break; |
| 4834 | } |
| 4835 | |
| 4836 | ASSERT(da_old >= da_new); |
| 4837 | da_diff = da_old - da_new; |
| 4838 | if (!isrt) |
| 4839 | da_diff += del->br_blockcount; |
| 4840 | if (da_diff) |
| 4841 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4842 | return error; |
| 4843 | } |
| 4844 | |
| 4845 | void |
| 4846 | xfs_bmap_del_extent_cow( |
| 4847 | struct xfs_inode *ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4848 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4849 | struct xfs_bmbt_irec *got, |
| 4850 | struct xfs_bmbt_irec *del) |
| 4851 | { |
| 4852 | struct xfs_mount *mp = ip->i_mount; |
| 4853 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4854 | struct xfs_bmbt_irec new; |
| 4855 | xfs_fileoff_t del_endoff, got_endoff; |
| 4856 | int state = BMAP_COWFORK; |
| 4857 | |
| 4858 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4859 | |
| 4860 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4861 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4862 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4863 | ASSERT(del->br_blockcount > 0); |
| 4864 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4865 | ASSERT(got_endoff >= del_endoff); |
| 4866 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4867 | |
| 4868 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4869 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4870 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4871 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4872 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4873 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4874 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4875 | /* |
| 4876 | * Matches the whole extent. Delete the entry. |
| 4877 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4878 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4879 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4880 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4881 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4882 | /* |
| 4883 | * Deleting the first part of the extent. |
| 4884 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4885 | got->br_startoff = del_endoff; |
| 4886 | got->br_blockcount -= del->br_blockcount; |
| 4887 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4888 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4889 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4890 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4891 | /* |
| 4892 | * Deleting the last part of the extent. |
| 4893 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4894 | got->br_blockcount -= 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; |
| 4897 | case 0: |
| 4898 | /* |
| 4899 | * Deleting the middle of the extent. |
| 4900 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4901 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4902 | |
| 4903 | new.br_startoff = del_endoff; |
| 4904 | new.br_blockcount = got_endoff - del_endoff; |
| 4905 | new.br_state = got->br_state; |
| 4906 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4907 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4908 | xfs_iext_update_extent(ip, state, icur, got); |
| 4909 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4910 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4911 | break; |
| 4912 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 4913 | ip->i_delayed_blks -= del->br_blockcount; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4914 | } |
| 4915 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4916 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4917 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4918 | * after removing space. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4919 | */ |
| 4920 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4921 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4922 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4923 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4924 | struct xfs_iext_cursor *icur, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4925 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4926 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4927 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4928 | int whichfork, /* data or attr fork */ |
| 4929 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4930 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4931 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4932 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4933 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4934 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4935 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4936 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4937 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4938 | int i; /* temp state */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 4939 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4940 | xfs_mount_t *mp; /* mount structure */ |
| 4941 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4942 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4943 | /* REFERENCED */ |
| 4944 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4945 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4946 | struct xfs_bmbt_irec old; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4947 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4948 | mp = ip->i_mount; |
| 4949 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4950 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4951 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4952 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4953 | xfs_iext_get_extent(ifp, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4954 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4955 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4956 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4957 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4958 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4959 | qfield = 0; |
| 4960 | error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4961 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4962 | /* |
| 4963 | * If it's the case where the directory code is running with no block |
| 4964 | * reservation, and the deleted block is in the middle of its extent, |
| 4965 | * and the resulting insert of an extent would cause transformation to |
| 4966 | * btree format, then reject it. The calling code will then swap blocks |
| 4967 | * around instead. We have to do this now, rather than waiting for the |
| 4968 | * conversion to btree format, since the transaction will be dirty then. |
| 4969 | */ |
| 4970 | if (tp->t_blk_res == 0 && |
| 4971 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 4972 | XFS_IFORK_NEXTENTS(ip, whichfork) >= |
| 4973 | XFS_IFORK_MAXEXT(ip, whichfork) && |
| 4974 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 4975 | return -ENOSPC; |
| 4976 | |
| 4977 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4978 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4979 | xfs_fsblock_t bno; |
| 4980 | xfs_filblks_t len; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 4981 | xfs_extlen_t mod; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4982 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 4983 | bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize, |
| 4984 | &mod); |
| 4985 | ASSERT(mod == 0); |
| 4986 | len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize, |
| 4987 | &mod); |
| 4988 | ASSERT(mod == 0); |
| 4989 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4990 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4991 | if (error) |
| 4992 | goto done; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4993 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4994 | nblks = len * mp->m_sb.sb_rextsize; |
| 4995 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4996 | } else { |
| 4997 | do_fx = 1; |
| 4998 | nblks = del->br_blockcount; |
| 4999 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 5000 | } |
| 5001 | |
| 5002 | del_endblock = del->br_startblock + del->br_blockcount; |
| 5003 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5004 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5005 | if (error) |
| 5006 | goto done; |
| 5007 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5008 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 5009 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5010 | if (got.br_startoff == del->br_startoff) |
| 5011 | state |= BMAP_LEFT_FILLING; |
| 5012 | if (got_endoff == del_endoff) |
| 5013 | state |= BMAP_RIGHT_FILLING; |
| 5014 | |
| 5015 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 5016 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5017 | /* |
| 5018 | * Matches the whole extent. Delete the entry. |
| 5019 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5020 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5021 | xfs_iext_prev(ifp, icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5022 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5023 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5024 | flags |= XFS_ILOG_CORE; |
| 5025 | if (!cur) { |
| 5026 | flags |= xfs_ilog_fext(whichfork); |
| 5027 | break; |
| 5028 | } |
| 5029 | if ((error = xfs_btree_delete(cur, &i))) |
| 5030 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5031 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5032 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5033 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5034 | /* |
| 5035 | * Deleting the first part of the extent. |
| 5036 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5037 | got.br_startoff = del_endoff; |
| 5038 | got.br_startblock = del_endblock; |
| 5039 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5040 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5041 | if (!cur) { |
| 5042 | flags |= xfs_ilog_fext(whichfork); |
| 5043 | break; |
| 5044 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5045 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5046 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5047 | goto done; |
| 5048 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5049 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5050 | /* |
| 5051 | * Deleting the last part of the extent. |
| 5052 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5053 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5054 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5055 | if (!cur) { |
| 5056 | flags |= xfs_ilog_fext(whichfork); |
| 5057 | break; |
| 5058 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5059 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5060 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5061 | goto done; |
| 5062 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5063 | case 0: |
| 5064 | /* |
| 5065 | * Deleting the middle of the extent. |
| 5066 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5067 | old = got; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5068 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5069 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5070 | xfs_iext_update_extent(ip, state, icur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5071 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5072 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5073 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5074 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5075 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5076 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5077 | flags |= XFS_ILOG_CORE; |
| 5078 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5079 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5080 | if (error) |
| 5081 | goto done; |
| 5082 | error = xfs_btree_increment(cur, 0, &i); |
| 5083 | if (error) |
| 5084 | goto done; |
| 5085 | cur->bc_rec.b = new; |
| 5086 | error = xfs_btree_insert(cur, &i); |
| 5087 | if (error && error != -ENOSPC) |
| 5088 | goto done; |
| 5089 | /* |
| 5090 | * If get no-space back from btree insert, it tried a |
| 5091 | * split, and we have a zero block reservation. Fix up |
| 5092 | * our state and return the error. |
| 5093 | */ |
| 5094 | if (error == -ENOSPC) { |
| 5095 | /* |
| 5096 | * Reset the cursor, don't trust it after any |
| 5097 | * insert operation. |
| 5098 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5099 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5100 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5101 | goto done; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5102 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5103 | /* |
| 5104 | * Update the btree record back |
| 5105 | * to the original value. |
| 5106 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5107 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5108 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5109 | goto done; |
| 5110 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5111 | * Reset the extent record back |
| 5112 | * to the original value. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5113 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5114 | xfs_iext_update_extent(ip, state, icur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5115 | flags = 0; |
| 5116 | error = -ENOSPC; |
| 5117 | goto done; |
| 5118 | } |
| 5119 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5120 | } else |
| 5121 | flags |= xfs_ilog_fext(whichfork); |
| 5122 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5123 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5124 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5125 | xfs_iext_insert(ip, icur, &new, state); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5126 | break; |
| 5127 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5128 | |
| 5129 | /* remove reverse mapping */ |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5130 | error = xfs_rmap_unmap_extent(tp, ip, whichfork, del); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5131 | if (error) |
| 5132 | goto done; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5133 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5134 | /* |
| 5135 | * If we need to, add to list of extents to delete. |
| 5136 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5137 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5138 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5139 | error = xfs_refcount_decrease_extent(tp, del); |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5140 | if (error) |
| 5141 | goto done; |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5142 | } else { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5143 | __xfs_bmap_add_free(tp, del->br_startblock, |
Brian Foster | 4e52933 | 2018-05-10 09:35:42 -0700 | [diff] [blame] | 5144 | del->br_blockcount, NULL, |
| 5145 | (bflags & XFS_BMAPI_NODISCARD) || |
| 5146 | del->br_state == XFS_EXT_UNWRITTEN); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5147 | } |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5148 | } |
| 5149 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5150 | /* |
| 5151 | * Adjust inode # blocks in the file. |
| 5152 | */ |
| 5153 | if (nblks) |
| 5154 | ip->i_d.di_nblocks -= nblks; |
| 5155 | /* |
| 5156 | * Adjust quota data. |
| 5157 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5158 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5159 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5160 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5161 | done: |
| 5162 | *logflagsp = flags; |
| 5163 | return error; |
| 5164 | } |
| 5165 | |
| 5166 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | * Unmap (remove) blocks from a file. |
| 5168 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5169 | * that value. If not all extents in the block range can be removed then |
| 5170 | * *done is set. |
| 5171 | */ |
| 5172 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5173 | __xfs_bunmapi( |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5174 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5176 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5177 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | int flags, /* misc flags */ |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 5179 | xfs_extnum_t nexts) /* number of extents max */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | { |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5181 | struct xfs_btree_cur *cur; /* bmap btree cursor */ |
| 5182 | struct xfs_bmbt_irec del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | int error; /* error return value */ |
| 5184 | xfs_extnum_t extno; /* extent number in list */ |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5185 | struct xfs_bmbt_irec got; /* current extent record */ |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 5186 | struct xfs_ifork *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | int isrt; /* freeing in rt area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5188 | int logflags; /* transaction logging flags */ |
| 5189 | xfs_extlen_t mod; /* rt extent offset */ |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5190 | struct xfs_mount *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | int tmp_logflags; /* partial logging flags */ |
| 5192 | int wasdel; /* was a delayed alloc extent */ |
| 5193 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5195 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5196 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5197 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5198 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5199 | struct xfs_iext_cursor icur; |
| 5200 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5202 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5203 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5204 | whichfork = xfs_bmapi_whichfork(flags); |
| 5205 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5206 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5207 | if (unlikely( |
| 5208 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5209 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5210 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5211 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5212 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | } |
| 5214 | mp = ip->i_mount; |
| 5215 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5216 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5217 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5218 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 | ASSERT(len > 0); |
| 5220 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5221 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5222 | /* |
| 5223 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5224 | * blowing out the transaction with a mix of EFIs and reflink |
| 5225 | * adjustments. |
| 5226 | */ |
Darrick J. Wong | 8c57b88 | 2017-12-10 18:03:53 -0800 | [diff] [blame] | 5227 | if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5228 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5229 | else |
| 5230 | max_len = len; |
| 5231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5233 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5234 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5235 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5236 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | return 0; |
| 5238 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5239 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5240 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5241 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5242 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5243 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5244 | *rlen = 0; |
| 5245 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5247 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | logflags = 0; |
| 5250 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5251 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5252 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | cur->bc_private.b.flags = 0; |
| 5254 | } else |
| 5255 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5256 | |
| 5257 | if (isrt) { |
| 5258 | /* |
| 5259 | * Synchronize by locking the bitmap inode. |
| 5260 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5261 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5262 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5263 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5264 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5265 | } |
| 5266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | extno = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5268 | while (end != (xfs_fileoff_t)-1 && end >= start && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5269 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5271 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | * Just back up to the previous extent, if so. |
| 5273 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5274 | if (got.br_startoff > end && |
| 5275 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5276 | done = true; |
| 5277 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5278 | } |
| 5279 | /* |
| 5280 | * Is the last block of this extent before the range |
| 5281 | * we're supposed to delete? If so, we're done. |
| 5282 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5283 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5285 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | break; |
| 5287 | /* |
| 5288 | * Then deal with the (possibly delayed) allocated space |
| 5289 | * we found. |
| 5290 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5292 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5293 | |
| 5294 | /* |
| 5295 | * Make sure we don't touch multiple AGF headers out of order |
| 5296 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5297 | */ |
| 5298 | if (!wasdel) { |
| 5299 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5300 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5301 | break; |
| 5302 | prev_agno = agno; |
| 5303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | if (got.br_startoff < start) { |
| 5305 | del.br_startoff = start; |
| 5306 | del.br_blockcount -= start - got.br_startoff; |
| 5307 | if (!wasdel) |
| 5308 | del.br_startblock += start - got.br_startoff; |
| 5309 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5310 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5311 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5312 | |
| 5313 | /* How much can we safely unmap? */ |
| 5314 | if (max_len < del.br_blockcount) { |
| 5315 | del.br_startoff += del.br_blockcount - max_len; |
| 5316 | if (!wasdel) |
| 5317 | del.br_startblock += del.br_blockcount - max_len; |
| 5318 | del.br_blockcount = max_len; |
| 5319 | } |
| 5320 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5321 | if (!isrt) |
| 5322 | goto delete; |
| 5323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | sum = del.br_startblock + del.br_blockcount; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5325 | div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod); |
| 5326 | if (mod) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | /* |
| 5328 | * Realtime extent not lined up at the end. |
| 5329 | * The extent could have been split into written |
| 5330 | * and unwritten pieces, or we could just be |
| 5331 | * unmapping part of it. But we can't really |
| 5332 | * get rid of part of a realtime extent. |
| 5333 | */ |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 5334 | if (del.br_state == XFS_EXT_UNWRITTEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | /* |
| 5336 | * This piece is unwritten, or we're not |
| 5337 | * using unwritten extents. Skip over it. |
| 5338 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5339 | ASSERT(end >= mod); |
| 5340 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | del.br_blockcount : mod; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5342 | if (end < got.br_startoff && |
| 5343 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5344 | done = true; |
| 5345 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | } |
| 5347 | continue; |
| 5348 | } |
| 5349 | /* |
| 5350 | * It's written, turn it unwritten. |
| 5351 | * This is better than zeroing it. |
| 5352 | */ |
| 5353 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5354 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | /* |
| 5356 | * If this spans a realtime extent boundary, |
| 5357 | * chop it back to the start of the one we end at. |
| 5358 | */ |
| 5359 | if (del.br_blockcount > mod) { |
| 5360 | del.br_startoff += del.br_blockcount - mod; |
| 5361 | del.br_startblock += del.br_blockcount - mod; |
| 5362 | del.br_blockcount = mod; |
| 5363 | } |
| 5364 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5365 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5366 | whichfork, &icur, &cur, &del, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5367 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | if (error) |
| 5369 | goto error0; |
| 5370 | goto nodelete; |
| 5371 | } |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5372 | div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod); |
| 5373 | if (mod) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | /* |
| 5375 | * Realtime extent is lined up at the end but not |
| 5376 | * at the front. We'll get rid of full extents if |
| 5377 | * we can. |
| 5378 | */ |
| 5379 | mod = mp->m_sb.sb_rextsize - mod; |
| 5380 | if (del.br_blockcount > mod) { |
| 5381 | del.br_blockcount -= mod; |
| 5382 | del.br_startoff += mod; |
| 5383 | del.br_startblock += mod; |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 5384 | } else if (del.br_startoff == start && |
| 5385 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5386 | tp->t_blk_res == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | /* |
| 5388 | * Can't make it unwritten. There isn't |
| 5389 | * a full extent here so just skip it. |
| 5390 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5391 | ASSERT(end >= del.br_blockcount); |
| 5392 | end -= del.br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5393 | if (got.br_startoff > end && |
| 5394 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5395 | done = true; |
| 5396 | break; |
| 5397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | continue; |
| 5399 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5400 | struct xfs_bmbt_irec prev; |
| 5401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | /* |
| 5403 | * This one is already unwritten. |
| 5404 | * It must have a written left neighbor. |
| 5405 | * Unwrite the killed part of that one and |
| 5406 | * try again. |
| 5407 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5408 | if (!xfs_iext_prev_extent(ifp, &icur, &prev)) |
| 5409 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5411 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | ASSERT(del.br_startblock == |
| 5413 | prev.br_startblock + prev.br_blockcount); |
| 5414 | if (prev.br_startoff < start) { |
| 5415 | mod = start - prev.br_startoff; |
| 5416 | prev.br_blockcount -= mod; |
| 5417 | prev.br_startblock += mod; |
| 5418 | prev.br_startoff = start; |
| 5419 | } |
| 5420 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5421 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5422 | ip, whichfork, &icur, &cur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5423 | &prev, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | if (error) |
| 5425 | goto error0; |
| 5426 | goto nodelete; |
| 5427 | } else { |
| 5428 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5429 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5430 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5431 | ip, whichfork, &icur, &cur, |
Brian Foster | 92f9da3 | 2018-07-11 22:26:28 -0700 | [diff] [blame] | 5432 | &del, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5433 | if (error) |
| 5434 | goto error0; |
| 5435 | goto nodelete; |
| 5436 | } |
| 5437 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5438 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5439 | delete: |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5440 | if (wasdel) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5441 | error = xfs_bmap_del_extent_delay(ip, whichfork, &icur, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5442 | &got, &del); |
| 5443 | } else { |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 5444 | error = xfs_bmap_del_extent_real(ip, tp, &icur, cur, |
| 5445 | &del, &tmp_logflags, whichfork, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5446 | flags); |
| 5447 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5448 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | if (error) |
| 5451 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5452 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5453 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5454 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | /* |
| 5457 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5459 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5460 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
| 5461 | (got.br_startoff > end && |
| 5462 | !xfs_iext_prev_extent(ifp, &icur, &got))) { |
| 5463 | done = true; |
| 5464 | break; |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | extno++; |
| 5467 | } |
| 5468 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5469 | if (done || end == (xfs_fileoff_t)-1 || end < start) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5470 | *rlen = 0; |
| 5471 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5472 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | /* |
| 5475 | * Convert to a btree if necessary. |
| 5476 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5477 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 5479 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 5480 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | logflags |= tmp_logflags; |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 5482 | } else { |
| 5483 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | } |
Christoph Hellwig | b101e33 | 2019-02-15 08:02:47 -0800 | [diff] [blame] | 5486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | error0: |
| 5488 | /* |
| 5489 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5490 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5492 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5494 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5495 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5497 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5498 | /* |
| 5499 | * Log inode even in the error case, if the transaction |
| 5500 | * is dirty we'll need to shut down the filesystem. |
| 5501 | */ |
| 5502 | if (logflags) |
| 5503 | xfs_trans_log_inode(tp, ip, logflags); |
| 5504 | if (cur) { |
Brian Foster | cf612de | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 5505 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | cur->bc_private.b.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5507 | xfs_btree_del_cursor(cur, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | } |
| 5509 | return error; |
| 5510 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5511 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5512 | /* Unmap a range of a file. */ |
| 5513 | int |
| 5514 | xfs_bunmapi( |
| 5515 | xfs_trans_t *tp, |
| 5516 | struct xfs_inode *ip, |
| 5517 | xfs_fileoff_t bno, |
| 5518 | xfs_filblks_t len, |
| 5519 | int flags, |
| 5520 | xfs_extnum_t nexts, |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5521 | int *done) |
| 5522 | { |
| 5523 | int error; |
| 5524 | |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 5525 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts); |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5526 | *done = (len == 0); |
| 5527 | return error; |
| 5528 | } |
| 5529 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5530 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5531 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5532 | * extent that precedes the target hole of the shift. |
| 5533 | */ |
| 5534 | STATIC bool |
| 5535 | xfs_bmse_can_merge( |
| 5536 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5537 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5538 | xfs_fileoff_t shift) /* shift fsb */ |
| 5539 | { |
| 5540 | xfs_fileoff_t startoff; |
| 5541 | |
| 5542 | startoff = got->br_startoff - shift; |
| 5543 | |
| 5544 | /* |
| 5545 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5546 | * the preceding extent. |
| 5547 | */ |
| 5548 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5549 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5550 | (left->br_state != got->br_state) || |
| 5551 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5552 | return false; |
| 5553 | |
| 5554 | return true; |
| 5555 | } |
| 5556 | |
| 5557 | /* |
| 5558 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5559 | * hole in the file. If an extent shift would result in the extent being fully |
| 5560 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5561 | * the preceding extent rather than do the shift. |
| 5562 | * |
| 5563 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5564 | * with the provided extents via xfs_bmse_can_merge(). |
| 5565 | */ |
| 5566 | STATIC int |
| 5567 | xfs_bmse_merge( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5568 | struct xfs_trans *tp, |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5569 | struct xfs_inode *ip, |
| 5570 | int whichfork, |
| 5571 | xfs_fileoff_t shift, /* shift fsb */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5572 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5573 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5574 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5575 | struct xfs_btree_cur *cur, |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5576 | int *logflags) /* output */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5577 | { |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5578 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5579 | xfs_filblks_t blockcount; |
| 5580 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5581 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5582 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5583 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5584 | |
| 5585 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5586 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5587 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5588 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5589 | new = *left; |
| 5590 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5591 | |
| 5592 | /* |
| 5593 | * Update the on-disk extent count, the btree if necessary and log the |
| 5594 | * inode. |
| 5595 | */ |
| 5596 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5597 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5598 | *logflags |= XFS_ILOG_CORE; |
| 5599 | if (!cur) { |
| 5600 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5601 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5602 | } |
| 5603 | |
| 5604 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5605 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5606 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5607 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5608 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5609 | |
| 5610 | error = xfs_btree_delete(cur, &i); |
| 5611 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5612 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5613 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5614 | |
| 5615 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5616 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5617 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5618 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5619 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5620 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5621 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5622 | if (error) |
| 5623 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5624 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5625 | done: |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5626 | xfs_iext_remove(ip, icur, 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5627 | xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur); |
| 5628 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5629 | &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5630 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5631 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5632 | error = xfs_rmap_unmap_extent(tp, ip, whichfork, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5633 | if (error) |
| 5634 | return error; |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5635 | memcpy(&new, got, sizeof(new)); |
| 5636 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5637 | return xfs_rmap_map_extent(tp, ip, whichfork, &new); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5638 | } |
| 5639 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5640 | static int |
| 5641 | xfs_bmap_shift_update_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5642 | struct xfs_trans *tp, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5643 | struct xfs_inode *ip, |
| 5644 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5645 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5646 | struct xfs_bmbt_irec *got, |
| 5647 | struct xfs_btree_cur *cur, |
| 5648 | int *logflags, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5649 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5650 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5651 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5652 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5653 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5654 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5655 | *logflags |= XFS_ILOG_CORE; |
| 5656 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5657 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5658 | |
| 5659 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5660 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5661 | if (error) |
| 5662 | return error; |
| 5663 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
| 5664 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5665 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5666 | if (error) |
| 5667 | return error; |
| 5668 | } else { |
| 5669 | *logflags |= XFS_ILOG_DEXT; |
| 5670 | } |
| 5671 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5672 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5673 | got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5674 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5675 | /* update reverse mapping */ |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5676 | error = xfs_rmap_unmap_extent(tp, ip, whichfork, &prev); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5677 | if (error) |
| 5678 | return error; |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5679 | return xfs_rmap_map_extent(tp, ip, whichfork, got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5680 | } |
| 5681 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5682 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5683 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5684 | struct xfs_trans *tp, |
| 5685 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5686 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5687 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 333f950 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5688 | bool *done) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5689 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5690 | int whichfork = XFS_DATA_FORK; |
| 5691 | struct xfs_mount *mp = ip->i_mount; |
| 5692 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5693 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5694 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5695 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5696 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5697 | int error = 0; |
| 5698 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5699 | |
| 5700 | if (unlikely(XFS_TEST_ERROR( |
| 5701 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5702 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5703 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5704 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5705 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5706 | } |
| 5707 | |
| 5708 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5709 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5710 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5711 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5712 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5713 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5714 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5715 | if (error) |
| 5716 | return error; |
| 5717 | } |
| 5718 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5719 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5720 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5721 | cur->bc_private.b.flags = 0; |
| 5722 | } |
| 5723 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5724 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5725 | *done = true; |
| 5726 | goto del_cursor; |
| 5727 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5728 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5729 | del_cursor); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5730 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5731 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5732 | if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5733 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5734 | error = -EINVAL; |
| 5735 | goto del_cursor; |
| 5736 | } |
| 5737 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5738 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5739 | error = xfs_bmse_merge(tp, ip, whichfork, |
| 5740 | offset_shift_fsb, &icur, &got, &prev, |
| 5741 | cur, &logflags); |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5742 | if (error) |
| 5743 | goto del_cursor; |
| 5744 | goto done; |
| 5745 | } |
| 5746 | } else { |
| 5747 | if (got.br_startoff < offset_shift_fsb) { |
| 5748 | error = -EINVAL; |
| 5749 | goto del_cursor; |
| 5750 | } |
| 5751 | } |
| 5752 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5753 | error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got, |
| 5754 | cur, &logflags, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5755 | if (error) |
| 5756 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5757 | |
Christoph Hellwig | 4263036 | 2017-11-03 10:34:41 -0700 | [diff] [blame] | 5758 | done: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5759 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
| 5760 | *done = true; |
| 5761 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5762 | } |
| 5763 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5764 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5765 | del_cursor: |
| 5766 | if (cur) |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5767 | xfs_btree_del_cursor(cur, error); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5768 | if (logflags) |
| 5769 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5770 | return error; |
| 5771 | } |
| 5772 | |
Darrick J. Wong | f62cb48 | 2018-06-21 23:26:57 -0700 | [diff] [blame] | 5773 | /* Make sure we won't be right-shifting an extent past the maximum bound. */ |
| 5774 | int |
| 5775 | xfs_bmap_can_insert_extents( |
| 5776 | struct xfs_inode *ip, |
| 5777 | xfs_fileoff_t off, |
| 5778 | xfs_fileoff_t shift) |
| 5779 | { |
| 5780 | struct xfs_bmbt_irec got; |
| 5781 | int is_empty; |
| 5782 | int error = 0; |
| 5783 | |
| 5784 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5785 | |
| 5786 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 5787 | return -EIO; |
| 5788 | |
| 5789 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 5790 | error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty); |
| 5791 | if (!error && !is_empty && got.br_startoff >= off && |
| 5792 | ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff) |
| 5793 | error = -EINVAL; |
| 5794 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 5795 | |
| 5796 | return error; |
| 5797 | } |
| 5798 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5799 | int |
| 5800 | xfs_bmap_insert_extents( |
| 5801 | struct xfs_trans *tp, |
| 5802 | struct xfs_inode *ip, |
| 5803 | xfs_fileoff_t *next_fsb, |
| 5804 | xfs_fileoff_t offset_shift_fsb, |
| 5805 | bool *done, |
Brian Foster | 333f950 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5806 | xfs_fileoff_t stop_fsb) |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5807 | { |
| 5808 | int whichfork = XFS_DATA_FORK; |
| 5809 | struct xfs_mount *mp = ip->i_mount; |
| 5810 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5811 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5812 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5813 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5814 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5815 | int error = 0; |
| 5816 | int logflags = 0; |
| 5817 | |
| 5818 | if (unlikely(XFS_TEST_ERROR( |
| 5819 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5820 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5821 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
| 5822 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 5823 | return -EFSCORRUPTED; |
| 5824 | } |
| 5825 | |
| 5826 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5827 | return -EIO; |
| 5828 | |
| 5829 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5830 | |
| 5831 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5832 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5833 | if (error) |
| 5834 | return error; |
| 5835 | } |
| 5836 | |
| 5837 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5838 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5839 | cur->bc_private.b.flags = 0; |
| 5840 | } |
| 5841 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5842 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5843 | xfs_iext_last(ifp, &icur); |
| 5844 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5845 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5846 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5847 | goto del_cursor; |
| 5848 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5849 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5850 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5851 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5852 | goto del_cursor; |
| 5853 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5854 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5855 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5856 | del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5857 | |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5858 | if (stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5859 | error = -EIO; |
| 5860 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5861 | } |
| 5862 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5863 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5864 | if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5865 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5866 | error = -EINVAL; |
| 5867 | goto del_cursor; |
| 5868 | } |
| 5869 | |
| 5870 | /* |
| 5871 | * Unlike a left shift (which involves a hole punch), a right |
| 5872 | * shift does not modify extent neighbors in any way. We should |
| 5873 | * never find mergeable extents in this scenario. Check anyways |
| 5874 | * and warn if we encounter two extents that could be one. |
| 5875 | */ |
| 5876 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5877 | WARN_ON_ONCE(1); |
| 5878 | } |
| 5879 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 5880 | error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got, |
| 5881 | cur, &logflags, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5882 | if (error) |
| 5883 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5884 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5885 | if (!xfs_iext_prev_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5886 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5887 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5888 | goto del_cursor; |
| 5889 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5890 | |
| 5891 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5892 | del_cursor: |
| 5893 | if (cur) |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 5894 | xfs_btree_del_cursor(cur, error); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5895 | if (logflags) |
| 5896 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5897 | return error; |
| 5898 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5899 | |
| 5900 | /* |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5901 | * Splits an extent into two extents at split_fsb block such that it is the |
| 5902 | * first block of the current_ext. @ext is a target extent to be split. |
| 5903 | * @split_fsb is a block where the extents is split. If split_fsb lies in a |
| 5904 | * hole or the first block of extents, just return 0. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5905 | */ |
| 5906 | STATIC int |
| 5907 | xfs_bmap_split_extent_at( |
| 5908 | struct xfs_trans *tp, |
| 5909 | struct xfs_inode *ip, |
Brian Foster | 4b77a08 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 5910 | xfs_fileoff_t split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5911 | { |
| 5912 | int whichfork = XFS_DATA_FORK; |
| 5913 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5914 | struct xfs_bmbt_irec got; |
| 5915 | struct xfs_bmbt_irec new; /* split extent */ |
| 5916 | struct xfs_mount *mp = ip->i_mount; |
| 5917 | struct xfs_ifork *ifp; |
| 5918 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5919 | struct xfs_iext_cursor icur; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5920 | int error = 0; |
| 5921 | int logflags = 0; |
| 5922 | int i = 0; |
| 5923 | |
| 5924 | if (unlikely(XFS_TEST_ERROR( |
| 5925 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5926 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5927 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5928 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5929 | XFS_ERRLEVEL_LOW, mp); |
| 5930 | return -EFSCORRUPTED; |
| 5931 | } |
| 5932 | |
| 5933 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5934 | return -EIO; |
| 5935 | |
| 5936 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5937 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5938 | /* Read in all the extents */ |
| 5939 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5940 | if (error) |
| 5941 | return error; |
| 5942 | } |
| 5943 | |
| 5944 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5945 | * 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] | 5946 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5947 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5948 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5949 | return 0; |
| 5950 | |
| 5951 | gotblkcnt = split_fsb - got.br_startoff; |
| 5952 | new.br_startoff = split_fsb; |
| 5953 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5954 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5955 | new.br_state = got.br_state; |
| 5956 | |
| 5957 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5958 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5959 | cur->bc_private.b.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5960 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5961 | if (error) |
| 5962 | goto del_cursor; |
| 5963 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5964 | } |
| 5965 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5966 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5967 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, |
| 5968 | &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5969 | |
| 5970 | logflags = XFS_ILOG_CORE; |
| 5971 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5972 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5973 | if (error) |
| 5974 | goto del_cursor; |
| 5975 | } else |
| 5976 | logflags |= XFS_ILOG_DEXT; |
| 5977 | |
| 5978 | /* Add new extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5979 | xfs_iext_next(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5980 | xfs_iext_insert(ip, &icur, &new, 0); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5981 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5982 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5983 | |
| 5984 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5985 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5986 | if (error) |
| 5987 | goto del_cursor; |
| 5988 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5989 | error = xfs_btree_insert(cur, &i); |
| 5990 | if (error) |
| 5991 | goto del_cursor; |
| 5992 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5993 | } |
| 5994 | |
| 5995 | /* |
| 5996 | * Convert to a btree if necessary. |
| 5997 | */ |
| 5998 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 5999 | int tmp_logflags; /* partial log flag return val */ |
| 6000 | |
| 6001 | ASSERT(cur == NULL); |
Brian Foster | 280253d | 2018-07-11 22:26:29 -0700 | [diff] [blame] | 6002 | error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, |
| 6003 | &tmp_logflags, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6004 | logflags |= tmp_logflags; |
| 6005 | } |
| 6006 | |
| 6007 | del_cursor: |
| 6008 | if (cur) { |
| 6009 | cur->bc_private.b.allocated = 0; |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 6010 | xfs_btree_del_cursor(cur, error); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6011 | } |
| 6012 | |
| 6013 | if (logflags) |
| 6014 | xfs_trans_log_inode(tp, ip, logflags); |
| 6015 | return error; |
| 6016 | } |
| 6017 | |
| 6018 | int |
| 6019 | xfs_bmap_split_extent( |
| 6020 | struct xfs_inode *ip, |
| 6021 | xfs_fileoff_t split_fsb) |
| 6022 | { |
| 6023 | struct xfs_mount *mp = ip->i_mount; |
| 6024 | struct xfs_trans *tp; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6025 | int error; |
| 6026 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6027 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6028 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6029 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6030 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6031 | |
| 6032 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6033 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6034 | |
Brian Foster | 4b77a08 | 2018-07-11 22:26:27 -0700 | [diff] [blame] | 6035 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6036 | if (error) |
| 6037 | goto out; |
| 6038 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6039 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6040 | |
| 6041 | out: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6042 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6043 | return error; |
| 6044 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6045 | |
| 6046 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6047 | static bool |
| 6048 | xfs_bmap_is_update_needed( |
| 6049 | struct xfs_bmbt_irec *bmap) |
| 6050 | { |
| 6051 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6052 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6053 | } |
| 6054 | |
| 6055 | /* Record a bmap intent. */ |
| 6056 | static int |
| 6057 | __xfs_bmap_add( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6058 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6059 | enum xfs_bmap_intent_type type, |
| 6060 | struct xfs_inode *ip, |
| 6061 | int whichfork, |
| 6062 | struct xfs_bmbt_irec *bmap) |
| 6063 | { |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6064 | struct xfs_bmap_intent *bi; |
| 6065 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6066 | trace_xfs_bmap_defer(tp->t_mountp, |
| 6067 | XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock), |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6068 | type, |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6069 | XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock), |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6070 | ip->i_ino, whichfork, |
| 6071 | bmap->br_startoff, |
| 6072 | bmap->br_blockcount, |
| 6073 | bmap->br_state); |
| 6074 | |
| 6075 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6076 | INIT_LIST_HEAD(&bi->bi_list); |
| 6077 | bi->bi_type = type; |
| 6078 | bi->bi_owner = ip; |
| 6079 | bi->bi_whichfork = whichfork; |
| 6080 | bi->bi_bmap = *bmap; |
| 6081 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6082 | 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] | 6083 | return 0; |
| 6084 | } |
| 6085 | |
| 6086 | /* Map an extent into a file. */ |
| 6087 | int |
| 6088 | xfs_bmap_map_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6089 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6090 | struct xfs_inode *ip, |
| 6091 | struct xfs_bmbt_irec *PREV) |
| 6092 | { |
| 6093 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6094 | return 0; |
| 6095 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6096 | return __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] | 6097 | } |
| 6098 | |
| 6099 | /* Unmap an extent out of a file. */ |
| 6100 | int |
| 6101 | xfs_bmap_unmap_extent( |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6102 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6103 | struct xfs_inode *ip, |
| 6104 | struct xfs_bmbt_irec *PREV) |
| 6105 | { |
| 6106 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6107 | return 0; |
| 6108 | |
Brian Foster | 0f37d17 | 2018-08-01 07:20:34 -0700 | [diff] [blame] | 6109 | return __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] | 6110 | } |
| 6111 | |
| 6112 | /* |
| 6113 | * Process one of the deferred bmap operations. We pass back the |
| 6114 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6115 | */ |
| 6116 | int |
| 6117 | xfs_bmap_finish_one( |
| 6118 | struct xfs_trans *tp, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6119 | struct xfs_inode *ip, |
| 6120 | enum xfs_bmap_intent_type type, |
| 6121 | int whichfork, |
| 6122 | xfs_fileoff_t startoff, |
| 6123 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6124 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6125 | xfs_exntst_t state) |
| 6126 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6127 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6128 | |
Brian Foster | 3728379 | 2018-07-11 22:26:23 -0700 | [diff] [blame] | 6129 | ASSERT(tp->t_firstblock == NULLFSBLOCK); |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6130 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6131 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6132 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6133 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6134 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6135 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6136 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6137 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6138 | |
| 6139 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6140 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6141 | return -EIO; |
| 6142 | |
| 6143 | switch (type) { |
| 6144 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6145 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Brian Foster | ff3edf2 | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 6146 | startblock, 0); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6147 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6148 | break; |
| 6149 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6150 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 6151 | XFS_BMAPI_REMAP, 1); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6152 | break; |
| 6153 | default: |
| 6154 | ASSERT(0); |
| 6155 | error = -EFSCORRUPTED; |
| 6156 | } |
| 6157 | |
| 6158 | return error; |
| 6159 | } |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6160 | |
| 6161 | /* Check that an inode's extent does not have invalid flags or bad ranges. */ |
| 6162 | xfs_failaddr_t |
| 6163 | xfs_bmap_validate_extent( |
| 6164 | struct xfs_inode *ip, |
| 6165 | int whichfork, |
| 6166 | struct xfs_bmbt_irec *irec) |
| 6167 | { |
| 6168 | struct xfs_mount *mp = ip->i_mount; |
| 6169 | xfs_fsblock_t endfsb; |
| 6170 | bool isrt; |
| 6171 | |
| 6172 | isrt = XFS_IS_REALTIME_INODE(ip); |
| 6173 | endfsb = irec->br_startblock + irec->br_blockcount - 1; |
| 6174 | if (isrt) { |
| 6175 | if (!xfs_verify_rtbno(mp, irec->br_startblock)) |
| 6176 | return __this_address; |
| 6177 | if (!xfs_verify_rtbno(mp, endfsb)) |
| 6178 | return __this_address; |
| 6179 | } else { |
| 6180 | if (!xfs_verify_fsbno(mp, irec->br_startblock)) |
| 6181 | return __this_address; |
| 6182 | if (!xfs_verify_fsbno(mp, endfsb)) |
| 6183 | return __this_address; |
| 6184 | if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) != |
| 6185 | XFS_FSB_TO_AGNO(mp, endfsb)) |
| 6186 | return __this_address; |
| 6187 | } |
Christoph Hellwig | daa79ba | 2018-10-18 17:18:58 +1100 | [diff] [blame] | 6188 | if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK) |
| 6189 | return __this_address; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6190 | return NULL; |
| 6191 | } |