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 */ |
| 329 | xfs_ifork_t *ifp; /* fork structure */ |
| 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( |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 536 | struct xfs_mount *mp, |
| 537 | struct xfs_defer_ops *dfops, |
| 538 | xfs_fsblock_t bno, |
| 539 | xfs_filblks_t len, |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 540 | struct xfs_owner_info *oinfo, |
| 541 | bool skip_discard) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 542 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 543 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 544 | #ifdef DEBUG |
| 545 | xfs_agnumber_t agno; |
| 546 | xfs_agblock_t agbno; |
| 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 |
| 567 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 568 | new->xefi_skip_discard = skip_discard; |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 569 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
| 570 | XFS_FSB_TO_AGBNO(mp, bno), len); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 571 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Inode fork format manipulation functions |
| 576 | */ |
| 577 | |
| 578 | /* |
| 579 | * Transform a btree format file with only one leaf node, where the |
| 580 | * extents list will fit in the inode, into an extents format file. |
| 581 | * Since the file extents are already in-core, all we have to do is |
| 582 | * give up the space for the btree root and pitch the leaf block. |
| 583 | */ |
| 584 | STATIC int /* error */ |
| 585 | xfs_bmap_btree_to_extents( |
| 586 | xfs_trans_t *tp, /* transaction pointer */ |
| 587 | xfs_inode_t *ip, /* incore inode pointer */ |
| 588 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 589 | int *logflagsp, /* inode logging flags */ |
| 590 | int whichfork) /* data or attr fork */ |
| 591 | { |
| 592 | /* REFERENCED */ |
| 593 | struct xfs_btree_block *cblock;/* child btree block */ |
| 594 | xfs_fsblock_t cbno; /* child block number */ |
| 595 | xfs_buf_t *cbp; /* child block's buffer */ |
| 596 | int error; /* error return value */ |
| 597 | xfs_ifork_t *ifp; /* inode fork data */ |
| 598 | xfs_mount_t *mp; /* mount point structure */ |
| 599 | __be64 *pp; /* ptr to block address */ |
| 600 | struct xfs_btree_block *rblock;/* root btree block */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 601 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 602 | |
| 603 | mp = ip->i_mount; |
| 604 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 605 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 606 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 607 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 608 | rblock = ifp->if_broot; |
| 609 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 610 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 611 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 612 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 613 | cbno = be64_to_cpu(*pp); |
| 614 | *logflagsp = 0; |
| 615 | #ifdef DEBUG |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 616 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, |
| 617 | xfs_btree_check_lptr(cur, cbno, 1)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 618 | #endif |
| 619 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 620 | &xfs_bmbt_buf_ops); |
| 621 | if (error) |
| 622 | return error; |
| 623 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 624 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 625 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 626 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
Brian Foster | 42b394a | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 627 | xfs_bmap_add_free(mp, cur->bc_tp->t_dfops, cbno, 1, &oinfo); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 628 | ip->i_d.di_nblocks--; |
| 629 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 630 | xfs_trans_binval(tp, cbp); |
| 631 | if (cur->bc_bufs[0] == cbp) |
| 632 | cur->bc_bufs[0] = NULL; |
| 633 | xfs_iroot_realloc(ip, -1, whichfork); |
| 634 | ASSERT(ifp->if_broot == NULL); |
| 635 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 636 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 637 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Convert an extents-format file into a btree-format file. |
| 643 | * The new file will have a root block (in the inode) and a single child block. |
| 644 | */ |
| 645 | STATIC int /* error */ |
| 646 | xfs_bmap_extents_to_btree( |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 647 | struct xfs_trans *tp, /* transaction pointer */ |
| 648 | struct xfs_inode *ip, /* incore inode pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 649 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 650 | struct xfs_btree_cur **curp, /* cursor returned to caller */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 651 | int wasdel, /* converting a delayed alloc */ |
| 652 | int *logflagsp, /* inode logging flags */ |
| 653 | int whichfork) /* data or attr fork */ |
| 654 | { |
| 655 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 656 | struct xfs_buf *abp; /* buffer for ablock */ |
| 657 | struct xfs_alloc_arg args; /* allocation arguments */ |
| 658 | struct xfs_bmbt_rec *arp; /* child record pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 659 | struct xfs_btree_block *block; /* btree root block */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 660 | struct xfs_btree_cur *cur; /* bmap btree cursor */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 661 | int error; /* error return value */ |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 662 | struct xfs_ifork *ifp; /* inode fork pointer */ |
| 663 | struct xfs_bmbt_key *kp; /* root block key pointer */ |
| 664 | struct xfs_mount *mp; /* mount structure */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 665 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 666 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 667 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 668 | xfs_extnum_t cnt = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 669 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 670 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 671 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 672 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 673 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 674 | |
| 675 | /* |
| 676 | * Make space in the inode incore. |
| 677 | */ |
| 678 | xfs_iroot_realloc(ip, 1, whichfork); |
| 679 | ifp->if_flags |= XFS_IFBROOT; |
| 680 | |
| 681 | /* |
| 682 | * Fill in the root. |
| 683 | */ |
| 684 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 685 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 686 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 687 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 688 | /* |
| 689 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 690 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 691 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 692 | cur->bc_private.b.firstblock = *firstblock; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 693 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 694 | /* |
| 695 | * Convert to a btree with two levels, one record in root. |
| 696 | */ |
| 697 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 698 | memset(&args, 0, sizeof(args)); |
| 699 | args.tp = tp; |
| 700 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 701 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 702 | args.firstblock = *firstblock; |
| 703 | if (*firstblock == NULLFSBLOCK) { |
| 704 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 705 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 706 | } else if (tp->t_dfops->dop_low) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 707 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 708 | args.fsbno = *firstblock; |
| 709 | } else { |
| 710 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 711 | args.fsbno = *firstblock; |
| 712 | } |
| 713 | args.minlen = args.maxlen = args.prod = 1; |
| 714 | args.wasdel = wasdel; |
| 715 | *logflagsp = 0; |
| 716 | if ((error = xfs_alloc_vextent(&args))) { |
| 717 | xfs_iroot_realloc(ip, -1, whichfork); |
Eric Sandeen | 2c4306f | 2018-04-16 23:07:27 -0700 | [diff] [blame] | 718 | ASSERT(ifp->if_broot == NULL); |
| 719 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 720 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 721 | return error; |
| 722 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 723 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 724 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
| 725 | xfs_iroot_realloc(ip, -1, whichfork); |
Eric Sandeen | 2c4306f | 2018-04-16 23:07:27 -0700 | [diff] [blame] | 726 | ASSERT(ifp->if_broot == NULL); |
| 727 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 728 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 729 | return -ENOSPC; |
| 730 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 731 | /* |
| 732 | * Allocation can't fail, the space was reserved. |
| 733 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 734 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 735 | args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 736 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 737 | cur->bc_private.b.allocated++; |
| 738 | ip->i_d.di_nblocks++; |
| 739 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 740 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 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; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * Convert a local file to an extents file. |
| 784 | * This code is out of bounds for data forks of regular files, |
| 785 | * since the file data needs to get logged so things will stay consistent. |
| 786 | * (The bmap-level manipulations are ok, though). |
| 787 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 788 | void |
| 789 | xfs_bmap_local_to_extents_empty( |
| 790 | struct xfs_inode *ip, |
| 791 | int whichfork) |
| 792 | { |
| 793 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 794 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 795 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 796 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 797 | ASSERT(ifp->if_bytes == 0); |
| 798 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 799 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 800 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 801 | ifp->if_flags &= ~XFS_IFINLINE; |
| 802 | ifp->if_flags |= XFS_IFEXTENTS; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 803 | ifp->if_u1.if_root = NULL; |
| 804 | ifp->if_height = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 805 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 806 | } |
| 807 | |
| 808 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 809 | STATIC int /* error */ |
| 810 | xfs_bmap_local_to_extents( |
| 811 | xfs_trans_t *tp, /* transaction pointer */ |
| 812 | xfs_inode_t *ip, /* incore inode pointer */ |
| 813 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 814 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 815 | int *logflagsp, /* inode logging flags */ |
| 816 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 817 | void (*init_fn)(struct xfs_trans *tp, |
| 818 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 819 | struct xfs_inode *ip, |
| 820 | struct xfs_ifork *ifp)) |
| 821 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 822 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 823 | int flags; /* logging flags returned */ |
| 824 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 825 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 826 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 827 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 828 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 829 | |
| 830 | /* |
| 831 | * We don't want to deal with the case of keeping inode data inline yet. |
| 832 | * So sending the data fork of a regular inode is invalid. |
| 833 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 834 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 835 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 836 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 837 | |
| 838 | if (!ifp->if_bytes) { |
| 839 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 840 | flags = XFS_ILOG_CORE; |
| 841 | goto done; |
| 842 | } |
| 843 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 844 | flags = 0; |
| 845 | error = 0; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 846 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 847 | memset(&args, 0, sizeof(args)); |
| 848 | args.tp = tp; |
| 849 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 850 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 851 | args.firstblock = *firstblock; |
| 852 | /* |
| 853 | * Allocate a block. We know we need only one, since the |
| 854 | * file currently fits in an inode. |
| 855 | */ |
| 856 | if (*firstblock == NULLFSBLOCK) { |
| 857 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 858 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 859 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 860 | args.fsbno = *firstblock; |
| 861 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 862 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 863 | args.total = total; |
| 864 | args.minlen = args.maxlen = args.prod = 1; |
| 865 | error = xfs_alloc_vextent(&args); |
| 866 | if (error) |
| 867 | goto done; |
| 868 | |
| 869 | /* Can't fail, the space was reserved. */ |
| 870 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 871 | ASSERT(args.len == 1); |
| 872 | *firstblock = args.fsbno; |
| 873 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 874 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 875 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 876 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 877 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 878 | * The callout is responsible for logging because the remote format |
| 879 | * might differ from the local format and thus we don't know how much to |
| 880 | * log here. Note that init_fn must also set the buffer log item type |
| 881 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 882 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 883 | init_fn(tp, bp, ip, ifp); |
| 884 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 885 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 886 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 887 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 888 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 889 | |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 890 | ifp->if_u1.if_root = NULL; |
| 891 | ifp->if_height = 0; |
| 892 | |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 893 | rec.br_startoff = 0; |
| 894 | rec.br_startblock = args.fsbno; |
| 895 | rec.br_blockcount = 1; |
| 896 | rec.br_state = XFS_EXT_NORM; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 897 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 898 | xfs_iext_insert(ip, &icur, &rec, 0); |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 899 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 900 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 901 | ip->i_d.di_nblocks = 1; |
| 902 | xfs_trans_mod_dquot_byino(tp, ip, |
| 903 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 904 | flags |= xfs_ilog_fext(whichfork); |
| 905 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 906 | done: |
| 907 | *logflagsp = flags; |
| 908 | return error; |
| 909 | } |
| 910 | |
| 911 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 913 | */ |
| 914 | STATIC int /* error */ |
| 915 | xfs_bmap_add_attrfork_btree( |
| 916 | xfs_trans_t *tp, /* transaction pointer */ |
| 917 | xfs_inode_t *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | int *flags) /* inode logging flags */ |
| 919 | { |
| 920 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 921 | int error; /* error return value */ |
| 922 | xfs_mount_t *mp; /* file system mount struct */ |
| 923 | int stat; /* newroot status */ |
| 924 | |
| 925 | mp = ip->i_mount; |
| 926 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 927 | *flags |= XFS_ILOG_DBROOT; |
| 928 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 929 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 930 | cur->bc_private.b.firstblock = tp->t_firstblock; |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 931 | error = xfs_bmbt_lookup_first(cur, &stat); |
| 932 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 934 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 935 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 936 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | goto error0; |
| 938 | if (stat == 0) { |
| 939 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 940 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 942 | tp->t_firstblock = cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | cur->bc_private.b.allocated = 0; |
| 944 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 945 | } |
| 946 | return 0; |
| 947 | error0: |
| 948 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 949 | return error; |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 954 | */ |
| 955 | STATIC int /* error */ |
| 956 | xfs_bmap_add_attrfork_extents( |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 957 | struct xfs_trans *tp, /* transaction pointer */ |
| 958 | struct xfs_inode *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | int *flags) /* inode logging flags */ |
| 960 | { |
| 961 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 962 | int error; /* error return value */ |
| 963 | |
| 964 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 965 | return 0; |
| 966 | cur = NULL; |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 967 | error = xfs_bmap_extents_to_btree(tp, ip, &tp->t_firstblock, &cur, 0, |
| 968 | flags, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | if (cur) { |
| 970 | cur->bc_private.b.allocated = 0; |
| 971 | xfs_btree_del_cursor(cur, |
| 972 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 973 | } |
| 974 | return error; |
| 975 | } |
| 976 | |
| 977 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 978 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 979 | * different data fork content type needs a different callout to do the |
| 980 | * conversion. Some are basic and only require special block initialisation |
| 981 | * callouts for the data formating, others (directories) are so specialised they |
| 982 | * handle everything themselves. |
| 983 | * |
| 984 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 985 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 986 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | */ |
| 988 | STATIC int /* error */ |
| 989 | xfs_bmap_add_attrfork_local( |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 990 | struct xfs_trans *tp, /* transaction pointer */ |
| 991 | struct xfs_inode *ip, /* incore inode pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | int *flags) /* inode logging flags */ |
| 993 | { |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 994 | struct xfs_da_args dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 997 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 998 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 999 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1001 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | dargs.dp = ip; |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 1003 | dargs.firstblock = &tp->t_firstblock; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1004 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | dargs.whichfork = XFS_DATA_FORK; |
| 1006 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1007 | return xfs_dir2_sf_to_block(&dargs); |
| 1008 | } |
| 1009 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1010 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 1011 | return xfs_bmap_local_to_extents(tp, ip, &tp->t_firstblock, 1, |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1012 | flags, XFS_DATA_FORK, |
| 1013 | xfs_symlink_local_to_remote); |
| 1014 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1015 | /* should only be called for types that support local format data */ |
| 1016 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1017 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1021 | * Convert inode from non-attributed to attributed. |
| 1022 | * Must not be in a transaction, ip must not be locked. |
| 1023 | */ |
| 1024 | int /* error code */ |
| 1025 | xfs_bmap_add_attrfork( |
| 1026 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1027 | int size, /* space new attribute needs */ |
| 1028 | int rsvd) /* xact may use reserved blks */ |
| 1029 | { |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1030 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1031 | xfs_mount_t *mp; /* mount structure */ |
| 1032 | xfs_trans_t *tp; /* transaction pointer */ |
| 1033 | int blks; /* space reservation */ |
| 1034 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1035 | int logflags; /* logging flags */ |
| 1036 | int error; /* error return value */ |
| 1037 | |
| 1038 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1039 | |
| 1040 | mp = ip->i_mount; |
| 1041 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1042 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1043 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1044 | |
| 1045 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1046 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1047 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1048 | return error; |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 1049 | xfs_defer_init(tp, &dfops, &tp->t_firstblock); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1050 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1051 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1052 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1053 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1054 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1055 | if (error) |
| 1056 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1057 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1058 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1059 | if (ip->i_d.di_anextents != 0) { |
| 1060 | error = -EFSCORRUPTED; |
| 1061 | goto trans_cancel; |
| 1062 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1063 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1064 | /* |
| 1065 | * For inodes coming from pre-6.2 filesystems. |
| 1066 | */ |
| 1067 | ASSERT(ip->i_d.di_aformat == 0); |
| 1068 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1069 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1070 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1071 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1072 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1073 | |
| 1074 | switch (ip->i_d.di_format) { |
| 1075 | case XFS_DINODE_FMT_DEV: |
| 1076 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1077 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1078 | case XFS_DINODE_FMT_LOCAL: |
| 1079 | case XFS_DINODE_FMT_EXTENTS: |
| 1080 | case XFS_DINODE_FMT_BTREE: |
| 1081 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1082 | if (!ip->i_d.di_forkoff) |
| 1083 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1084 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1085 | version = 2; |
| 1086 | break; |
| 1087 | default: |
| 1088 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1089 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1090 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | ASSERT(ip->i_afp == NULL); |
| 1094 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1095 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1096 | logflags = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1097 | switch (ip->i_d.di_format) { |
| 1098 | case XFS_DINODE_FMT_LOCAL: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 1099 | error = xfs_bmap_add_attrfork_local(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1100 | break; |
| 1101 | case XFS_DINODE_FMT_EXTENTS: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 1102 | error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1103 | break; |
| 1104 | case XFS_DINODE_FMT_BTREE: |
Brian Foster | 825d75cd | 2018-07-11 22:26:21 -0700 | [diff] [blame^] | 1105 | error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1106 | break; |
| 1107 | default: |
| 1108 | error = 0; |
| 1109 | break; |
| 1110 | } |
| 1111 | if (logflags) |
| 1112 | xfs_trans_log_inode(tp, ip, logflags); |
| 1113 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1114 | goto bmap_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1115 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1116 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1117 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1118 | |
| 1119 | spin_lock(&mp->m_sb_lock); |
| 1120 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1121 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1122 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1123 | } |
| 1124 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1125 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1126 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1127 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1128 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1129 | if (log_sb) |
| 1130 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1131 | } |
| 1132 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 1133 | error = xfs_defer_finish(&tp, &dfops); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1134 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1135 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1136 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1137 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1138 | return error; |
| 1139 | |
| 1140 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1141 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1142 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1143 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1144 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1145 | return error; |
| 1146 | } |
| 1147 | |
| 1148 | /* |
| 1149 | * Internal and external extent tree search functions. |
| 1150 | */ |
| 1151 | |
| 1152 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1153 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1154 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1155 | int |
| 1156 | xfs_iread_extents( |
| 1157 | struct xfs_trans *tp, |
| 1158 | struct xfs_inode *ip, |
| 1159 | int whichfork) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1160 | { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1161 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | e8e0e17 | 2017-10-19 11:06:29 -0700 | [diff] [blame] | 1162 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1163 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1164 | xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
| 1165 | struct xfs_btree_block *block = ifp->if_broot; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1166 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1167 | struct xfs_bmbt_irec new; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1168 | xfs_fsblock_t bno; |
| 1169 | struct xfs_buf *bp; |
| 1170 | xfs_extnum_t i, j; |
| 1171 | int level; |
| 1172 | __be64 *pp; |
| 1173 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1174 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1175 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1176 | |
| 1177 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 1178 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 1179 | return -EFSCORRUPTED; |
| 1180 | } |
| 1181 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1182 | /* |
| 1183 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1184 | */ |
| 1185 | level = be16_to_cpu(block->bb_level); |
| 1186 | ASSERT(level > 0); |
| 1187 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1188 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1189 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1190 | /* |
| 1191 | * Go down the tree until leaf level is reached, following the first |
| 1192 | * pointer (leftmost) at each level. |
| 1193 | */ |
| 1194 | while (level-- > 0) { |
| 1195 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1196 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1197 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1198 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1199 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1200 | if (level == 0) |
| 1201 | break; |
| 1202 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1203 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1204 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 1205 | xfs_verify_fsbno(mp, bno), out_brelse); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1206 | xfs_trans_brelse(tp, bp); |
| 1207 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1208 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1209 | /* |
| 1210 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1211 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1212 | i = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1213 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1214 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1215 | /* |
| 1216 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1217 | */ |
| 1218 | for (;;) { |
| 1219 | xfs_bmbt_rec_t *frp; |
| 1220 | xfs_fsblock_t nextbno; |
| 1221 | xfs_extnum_t num_recs; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1222 | |
| 1223 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1224 | if (unlikely(i + num_recs > nextents)) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1225 | xfs_warn(ip->i_mount, |
| 1226 | "corrupt dinode %Lu, (btree extents).", |
| 1227 | (unsigned long long) ip->i_ino); |
Darrick J. Wong | 90a58f9 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1228 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
| 1229 | __func__, block, sizeof(*block), |
| 1230 | __this_address); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1231 | error = -EFSCORRUPTED; |
| 1232 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1233 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1234 | /* |
| 1235 | * Read-ahead the next leaf block, if any. |
| 1236 | */ |
| 1237 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1238 | if (nextbno != NULLFSBLOCK) |
| 1239 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1240 | &xfs_bmbt_buf_ops); |
| 1241 | /* |
| 1242 | * Copy records into the extent records. |
| 1243 | */ |
| 1244 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1245 | for (j = 0; j < num_recs; j++, frp++, i++) { |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1246 | xfs_failaddr_t fa; |
| 1247 | |
Christoph Hellwig | dac9c9b | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1248 | xfs_bmbt_disk_get_all(frp, &new); |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1249 | fa = xfs_bmap_validate_extent(ip, whichfork, &new); |
| 1250 | if (fa) { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1251 | error = -EFSCORRUPTED; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1252 | xfs_inode_verifier_error(ip, error, |
| 1253 | "xfs_iread_extents(2)", |
| 1254 | frp, sizeof(*frp), fa); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1255 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1256 | } |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1257 | xfs_iext_insert(ip, &icur, &new, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1258 | trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); |
| 1259 | xfs_iext_next(ifp, &icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1260 | } |
| 1261 | xfs_trans_brelse(tp, bp); |
| 1262 | bno = nextbno; |
| 1263 | /* |
| 1264 | * If we've reached the end, stop. |
| 1265 | */ |
| 1266 | if (bno == NULLFSBLOCK) |
| 1267 | break; |
| 1268 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1269 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1270 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1271 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1272 | block = XFS_BUF_TO_BLOCK(bp); |
| 1273 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1274 | |
| 1275 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) { |
| 1276 | error = -EFSCORRUPTED; |
| 1277 | goto out; |
| 1278 | } |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1279 | ASSERT(i == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1280 | |
| 1281 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1282 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1283 | |
| 1284 | out_brelse: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1285 | xfs_trans_brelse(tp, bp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1286 | out: |
| 1287 | xfs_iext_destroy(ifp); |
| 1288 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1289 | } |
| 1290 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1291 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1292 | * Returns the relative block number of the first unused block(s) in the given |
| 1293 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1294 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1295 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1296 | */ |
| 1297 | int /* error */ |
| 1298 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1299 | struct xfs_trans *tp, /* transaction pointer */ |
| 1300 | struct xfs_inode *ip, /* incore inode */ |
| 1301 | xfs_extlen_t len, /* size of hole to find */ |
| 1302 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1303 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1304 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1305 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1306 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1307 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1308 | xfs_fileoff_t lastaddr = 0; |
| 1309 | xfs_fileoff_t lowest, max; |
| 1310 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1311 | |
| 1312 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1313 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1314 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1315 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1316 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1317 | *first_unused = 0; |
| 1318 | return 0; |
| 1319 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1320 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1321 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1322 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1323 | if (error) |
| 1324 | return error; |
| 1325 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1326 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1327 | lowest = max = *first_unused; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1328 | for_each_xfs_iext(ifp, &icur, &got) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1329 | /* |
| 1330 | * See if the hole before this extent will work. |
| 1331 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1332 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1333 | got.br_startoff - max >= len) |
| 1334 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1335 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1336 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1337 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1338 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1339 | *first_unused = max; |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1344 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1345 | * last_block (input value) in the file. |
| 1346 | * This is not based on i_size, it is based on the extent records. |
| 1347 | * Returns 0 for local files, as they do not have extent records. |
| 1348 | */ |
| 1349 | int /* error */ |
| 1350 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1351 | struct xfs_trans *tp, /* transaction pointer */ |
| 1352 | struct xfs_inode *ip, /* incore inode */ |
| 1353 | xfs_fileoff_t *last_block, /* last block */ |
| 1354 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1355 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1356 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1357 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1358 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1359 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1360 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1361 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1362 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1363 | *last_block = 0; |
| 1364 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1365 | case XFS_DINODE_FMT_BTREE: |
| 1366 | case XFS_DINODE_FMT_EXTENTS: |
| 1367 | break; |
| 1368 | default: |
| 1369 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1370 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1371 | |
| 1372 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1373 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1374 | if (error) |
| 1375 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1376 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1377 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1378 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1379 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1380 | return 0; |
| 1381 | } |
| 1382 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1383 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1384 | xfs_bmap_last_extent( |
| 1385 | struct xfs_trans *tp, |
| 1386 | struct xfs_inode *ip, |
| 1387 | int whichfork, |
| 1388 | struct xfs_bmbt_irec *rec, |
| 1389 | int *is_empty) |
| 1390 | { |
| 1391 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1392 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1393 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1394 | |
| 1395 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1396 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1397 | if (error) |
| 1398 | return error; |
| 1399 | } |
| 1400 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1401 | xfs_iext_last(ifp, &icur); |
| 1402 | if (!xfs_iext_get_extent(ifp, &icur, rec)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1403 | *is_empty = 1; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1404 | else |
| 1405 | *is_empty = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1406 | return 0; |
| 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * Check the last inode extent to determine whether this allocation will result |
| 1411 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1412 | * blocks at the end of the file which do not start at the previous data block, |
| 1413 | * we will try to align the new blocks at stripe unit boundaries. |
| 1414 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1415 | * 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] | 1416 | * at, or past the EOF. |
| 1417 | */ |
| 1418 | STATIC int |
| 1419 | xfs_bmap_isaeof( |
| 1420 | struct xfs_bmalloca *bma, |
| 1421 | int whichfork) |
| 1422 | { |
| 1423 | struct xfs_bmbt_irec rec; |
| 1424 | int is_empty; |
| 1425 | int error; |
| 1426 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1427 | bma->aeof = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1428 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1429 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1430 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1431 | return error; |
| 1432 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1433 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1434 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1435 | return 0; |
| 1436 | } |
| 1437 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1438 | /* |
| 1439 | * Check if we are allocation or past the last extent, or at least into |
| 1440 | * the last delayed allocated extent. |
| 1441 | */ |
| 1442 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1443 | (bma->offset >= rec.br_startoff && |
| 1444 | isnullstartblock(rec.br_startblock)); |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1449 | * Returns the file-relative block number of the first block past eof in |
| 1450 | * the file. This is not based on i_size, it is based on the extent records. |
| 1451 | * Returns 0 for local files, as they do not have extent records. |
| 1452 | */ |
| 1453 | int |
| 1454 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1455 | struct xfs_inode *ip, |
| 1456 | xfs_fileoff_t *last_block, |
| 1457 | int whichfork) |
| 1458 | { |
| 1459 | struct xfs_bmbt_irec rec; |
| 1460 | int is_empty; |
| 1461 | int error; |
| 1462 | |
| 1463 | *last_block = 0; |
| 1464 | |
| 1465 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1466 | return 0; |
| 1467 | |
| 1468 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1469 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1470 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1471 | |
| 1472 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1473 | if (error || is_empty) |
| 1474 | return error; |
| 1475 | |
| 1476 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * Returns whether the selected fork of the inode has exactly one |
| 1482 | * block or not. For the data fork we check this matches di_size, |
| 1483 | * implying the file's range is 0..bsize-1. |
| 1484 | */ |
| 1485 | int /* 1=>1 block, 0=>otherwise */ |
| 1486 | xfs_bmap_one_block( |
| 1487 | xfs_inode_t *ip, /* incore inode */ |
| 1488 | int whichfork) /* data or attr fork */ |
| 1489 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1490 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1491 | int rval; /* return value */ |
| 1492 | xfs_bmbt_irec_t s; /* internal version of extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1493 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1494 | |
| 1495 | #ifndef DEBUG |
| 1496 | if (whichfork == XFS_DATA_FORK) |
| 1497 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1498 | #endif /* !DEBUG */ |
| 1499 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1500 | return 0; |
| 1501 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1502 | return 0; |
| 1503 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1504 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1505 | xfs_iext_first(ifp, &icur); |
| 1506 | xfs_iext_get_extent(ifp, &icur, &s); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1507 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1508 | if (rval && whichfork == XFS_DATA_FORK) |
| 1509 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1510 | return rval; |
| 1511 | } |
| 1512 | |
| 1513 | /* |
| 1514 | * Extent tree manipulation functions used during allocation. |
| 1515 | */ |
| 1516 | |
| 1517 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1518 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | */ |
| 1520 | STATIC int /* error */ |
| 1521 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1522 | struct xfs_bmalloca *bma, |
| 1523 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1525 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1528 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1530 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1531 | /* left is 0, right is 1, prev is 2 */ |
| 1532 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1533 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1534 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1535 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1536 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1538 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1539 | xfs_extnum_t *nextents; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1540 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1542 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1543 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1544 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1545 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1546 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1547 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1548 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1549 | ASSERT(!bma->cur || |
| 1550 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1551 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1552 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | #define LEFT r[0] |
| 1555 | #define RIGHT r[1] |
| 1556 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | |
| 1558 | /* |
| 1559 | * Set up a bunch of variables to make the tests simpler. |
| 1560 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1561 | xfs_iext_get_extent(ifp, &bma->icur, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1563 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1565 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1566 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1567 | da_old = startblockval(PREV.br_startblock); |
| 1568 | da_new = 0; |
| 1569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | /* |
| 1571 | * Set flags determining what part of the previous delayed allocation |
| 1572 | * extent is being replaced by a real allocation. |
| 1573 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1574 | if (PREV.br_startoff == new->br_startoff) |
| 1575 | state |= BMAP_LEFT_FILLING; |
| 1576 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1577 | state |= BMAP_RIGHT_FILLING; |
| 1578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | /* |
| 1580 | * Check and set flags if this segment has a left neighbor. |
| 1581 | * Don't set contiguous if the combined extent would be too large. |
| 1582 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1583 | if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1584 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1585 | if (isnullstartblock(LEFT.br_startblock)) |
| 1586 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1588 | |
| 1589 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1590 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1591 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1592 | LEFT.br_state == new->br_state && |
| 1593 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1594 | state |= BMAP_LEFT_CONTIG; |
| 1595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | /* |
| 1597 | * Check and set flags if this segment has a right neighbor. |
| 1598 | * Don't set contiguous if the combined extent would be too large. |
| 1599 | * Also check for all-three-contiguous being too large. |
| 1600 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1601 | if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1602 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1603 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1604 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1606 | |
| 1607 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1608 | new_endoff == RIGHT.br_startoff && |
| 1609 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1610 | new->br_state == RIGHT.br_state && |
| 1611 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1612 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1613 | BMAP_RIGHT_FILLING)) != |
| 1614 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1615 | BMAP_RIGHT_FILLING) || |
| 1616 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1617 | <= MAXEXTLEN)) |
| 1618 | state |= BMAP_RIGHT_CONTIG; |
| 1619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | error = 0; |
| 1621 | /* |
| 1622 | * Switch out based on the FILLING and CONTIG state bits. |
| 1623 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1624 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1625 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1626 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1627 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | /* |
| 1629 | * Filling in all of a previously delayed allocation extent. |
| 1630 | * The left and right neighbors are both contiguous with new. |
| 1631 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1632 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1633 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1634 | xfs_iext_remove(bma->ip, &bma->icur, state); |
| 1635 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1636 | xfs_iext_prev(ifp, &bma->icur); |
| 1637 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1638 | (*nextents)--; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1639 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1640 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1642 | else { |
| 1643 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1644 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1645 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1647 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1648 | error = xfs_btree_delete(bma->cur, &i); |
| 1649 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1651 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1652 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 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 | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1656 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1657 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | goto done; |
| 1659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | break; |
| 1661 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1662 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | /* |
| 1664 | * Filling in all of a previously delayed allocation extent. |
| 1665 | * The left neighbor is contiguous, the right is not. |
| 1666 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1667 | old = LEFT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1668 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1669 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1670 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1671 | xfs_iext_prev(ifp, &bma->icur); |
| 1672 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1673 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1674 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | rval = XFS_ILOG_DEXT; |
| 1676 | else { |
| 1677 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1678 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1679 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1681 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1682 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1683 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | goto done; |
| 1685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | break; |
| 1687 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1688 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | /* |
| 1690 | * Filling in all of a previously delayed allocation extent. |
| 1691 | * The right neighbor is contiguous, the left is not. |
| 1692 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1693 | PREV.br_startblock = new->br_startblock; |
| 1694 | PREV.br_blockcount += RIGHT.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1695 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1696 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1697 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1698 | xfs_iext_prev(ifp, &bma->icur); |
| 1699 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1700 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1701 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | rval = XFS_ILOG_DEXT; |
| 1703 | else { |
| 1704 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1705 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1706 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1708 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1709 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1710 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | goto done; |
| 1712 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | break; |
| 1714 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1715 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | /* |
| 1717 | * Filling in all of a previously delayed allocation extent. |
| 1718 | * Neither the left nor right neighbors are contiguous with |
| 1719 | * the new one. |
| 1720 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1721 | PREV.br_startblock = new->br_startblock; |
| 1722 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1723 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1724 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1725 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1726 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1728 | else { |
| 1729 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1730 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1731 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1733 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1734 | error = xfs_btree_insert(bma->cur, &i); |
| 1735 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1737 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | break; |
| 1740 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1741 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | /* |
| 1743 | * Filling in the first part of a previous delayed allocation. |
| 1744 | * The left neighbor is contiguous. |
| 1745 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1746 | old = LEFT; |
| 1747 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1748 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1749 | startblockval(PREV.br_startblock)); |
| 1750 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1751 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1752 | |
Christoph Hellwig | bf99971 | 2017-11-03 10:34:38 -0700 | [diff] [blame] | 1753 | PREV.br_blockcount = temp; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1754 | PREV.br_startoff += new->br_blockcount; |
| 1755 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1756 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1757 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1758 | xfs_iext_prev(ifp, &bma->icur); |
| 1759 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1760 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1761 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | rval = XFS_ILOG_DEXT; |
| 1763 | else { |
| 1764 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1765 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1766 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1768 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1769 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1770 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | goto done; |
| 1772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | break; |
| 1774 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1775 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | /* |
| 1777 | * Filling in the first part of a previous delayed allocation. |
| 1778 | * The left neighbor is not contiguous. |
| 1779 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1780 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1781 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1782 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1784 | else { |
| 1785 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1786 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1787 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1789 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1790 | error = xfs_btree_insert(bma->cur, &i); |
| 1791 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1793 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1795 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1796 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1797 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 1798 | bma->firstblock, &bma->cur, 1, |
| 1799 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | rval |= tmp_rval; |
| 1801 | if (error) |
| 1802 | goto done; |
| 1803 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1804 | |
| 1805 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1806 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1807 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1808 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1809 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1810 | PREV.br_startoff = new_endoff; |
| 1811 | PREV.br_blockcount = temp; |
| 1812 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1813 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1814 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1815 | xfs_iext_prev(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | break; |
| 1817 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1818 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | /* |
| 1820 | * Filling in the last part of a previous delayed allocation. |
| 1821 | * The right neighbor is contiguous with the new allocation. |
| 1822 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1823 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1824 | RIGHT.br_startoff = new->br_startoff; |
| 1825 | RIGHT.br_startblock = new->br_startblock; |
| 1826 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1827 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1828 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | rval = XFS_ILOG_DEXT; |
| 1830 | else { |
| 1831 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1832 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1833 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1835 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1836 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1837 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | goto done; |
| 1839 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1840 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1841 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1842 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1843 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1844 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1845 | PREV.br_blockcount = temp; |
| 1846 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1847 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1848 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1849 | xfs_iext_next(ifp, &bma->icur); |
| 1850 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | break; |
| 1852 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1853 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | /* |
| 1855 | * Filling in the last part of a previous delayed allocation. |
| 1856 | * The right neighbor is not contiguous. |
| 1857 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1858 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1859 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1860 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1862 | else { |
| 1863 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1864 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1865 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1867 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1868 | error = xfs_btree_insert(bma->cur, &i); |
| 1869 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1871 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1873 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1874 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1875 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 1876 | bma->firstblock, &bma->cur, 1, &tmp_rval, |
| 1877 | whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | rval |= tmp_rval; |
| 1879 | if (error) |
| 1880 | goto done; |
| 1881 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1882 | |
| 1883 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1884 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1885 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1886 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1887 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1888 | PREV.br_startblock = nullstartblock(da_new); |
| 1889 | PREV.br_blockcount = temp; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1890 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1891 | xfs_iext_next(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | break; |
| 1893 | |
| 1894 | case 0: |
| 1895 | /* |
| 1896 | * Filling in the middle part of a previous delayed allocation. |
| 1897 | * Contiguity is impossible here. |
| 1898 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1899 | * |
| 1900 | * We start with a delayed allocation: |
| 1901 | * |
| 1902 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1903 | * PREV @ idx |
| 1904 | * |
| 1905 | * and we are allocating: |
| 1906 | * +rrrrrrrrrrrrrrrrr+ |
| 1907 | * new |
| 1908 | * |
| 1909 | * and we set it up for insertion as: |
| 1910 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1911 | * new |
| 1912 | * PREV @ idx LEFT RIGHT |
| 1913 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1915 | old = PREV; |
| 1916 | |
| 1917 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1918 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1919 | |
| 1920 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1921 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1922 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1923 | RIGHT.br_blockcount = |
| 1924 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1925 | RIGHT.br_startblock = |
| 1926 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1927 | RIGHT.br_blockcount)); |
| 1928 | |
| 1929 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1930 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1931 | PREV.br_startblock = |
| 1932 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1933 | PREV.br_blockcount)); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1934 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1935 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1936 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1937 | xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); |
| 1938 | xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1939 | (*nextents)++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1940 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1941 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1943 | else { |
| 1944 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1945 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1946 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1948 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1949 | error = xfs_btree_insert(bma->cur, &i); |
| 1950 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1952 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1954 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1955 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1956 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 1957 | bma->firstblock, &bma->cur, 1, |
| 1958 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | rval |= tmp_rval; |
| 1960 | if (error) |
| 1961 | goto done; |
| 1962 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1963 | |
| 1964 | da_new = startblockval(PREV.br_startblock) + |
| 1965 | startblockval(RIGHT.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | break; |
| 1967 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1968 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1969 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1970 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1971 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1972 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1973 | case BMAP_LEFT_CONTIG: |
| 1974 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | /* |
| 1976 | * These cases are all impossible. |
| 1977 | */ |
| 1978 | ASSERT(0); |
| 1979 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1980 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 1981 | /* add reverse mapping unless caller opted out */ |
| 1982 | if (!(bma->flags & XFS_BMAPI_NORMAP)) { |
Brian Foster | 3e3673e | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 1983 | error = xfs_rmap_map_extent(mp, bma->tp->t_dfops, bma->ip, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 1984 | whichfork, new); |
| 1985 | if (error) |
| 1986 | goto done; |
| 1987 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 1988 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1989 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1990 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1991 | int tmp_logflags; /* partial log flag return val */ |
| 1992 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1993 | ASSERT(bma->cur == NULL); |
| 1994 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 1995 | bma->firstblock, &bma->cur, da_old > 0, |
| 1996 | &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1997 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1998 | if (error) |
| 1999 | goto done; |
| 2000 | } |
| 2001 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2002 | if (bma->cur) { |
| 2003 | da_new += bma->cur->bc_private.b.allocated; |
| 2004 | bma->cur->bc_private.b.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2007 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2008 | if (da_new != da_old) { |
| 2009 | ASSERT(state == 0 || da_new < da_old); |
| 2010 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2011 | false); |
| 2012 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2013 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2014 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2016 | if (whichfork != XFS_COW_FORK) |
| 2017 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | return error; |
| 2019 | #undef LEFT |
| 2020 | #undef RIGHT |
| 2021 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2025 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | */ |
| 2027 | STATIC int /* error */ |
| 2028 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2029 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2031 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2032 | struct xfs_iext_cursor *icur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2034 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2035 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2036 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2041 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2044 | /* left is 0, right is 1, prev is 2 */ |
| 2045 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2046 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2047 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2048 | struct xfs_bmbt_irec old; |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 2049 | struct xfs_defer_ops *dfops = tp ? tp->t_dfops : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2051 | *logflagsp = 0; |
| 2052 | |
| 2053 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2054 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2055 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2056 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2057 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2058 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | #define LEFT r[0] |
| 2061 | #define RIGHT r[1] |
| 2062 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | /* |
| 2065 | * Set up a bunch of variables to make the tests simpler. |
| 2066 | */ |
| 2067 | error = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2068 | xfs_iext_get_extent(ifp, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2069 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2071 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2072 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | /* |
| 2075 | * Set flags determining what part of the previous oldext allocation |
| 2076 | * extent is being replaced by a newext allocation. |
| 2077 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2078 | if (PREV.br_startoff == new->br_startoff) |
| 2079 | state |= BMAP_LEFT_FILLING; |
| 2080 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2081 | state |= BMAP_RIGHT_FILLING; |
| 2082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | /* |
| 2084 | * Check and set flags if this segment has a left neighbor. |
| 2085 | * Don't set contiguous if the combined extent would be too large. |
| 2086 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2087 | if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2088 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2089 | if (isnullstartblock(LEFT.br_startblock)) |
| 2090 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2092 | |
| 2093 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2094 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2095 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2096 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2097 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2098 | state |= BMAP_LEFT_CONTIG; |
| 2099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | /* |
| 2101 | * Check and set flags if this segment has a right neighbor. |
| 2102 | * Don't set contiguous if the combined extent would be too large. |
| 2103 | * Also check for all-three-contiguous being too large. |
| 2104 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2105 | if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2106 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2107 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2108 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2110 | |
| 2111 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2112 | new_endoff == RIGHT.br_startoff && |
| 2113 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2114 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2115 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2116 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2117 | BMAP_RIGHT_FILLING)) != |
| 2118 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2119 | BMAP_RIGHT_FILLING) || |
| 2120 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2121 | <= MAXEXTLEN)) |
| 2122 | state |= BMAP_RIGHT_CONTIG; |
| 2123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | /* |
| 2125 | * Switch out based on the FILLING and CONTIG state bits. |
| 2126 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2127 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2128 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2129 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2130 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | /* |
| 2132 | * Setting all of a previous oldext extent to newext. |
| 2133 | * The left and right neighbors are both contiguous with new. |
| 2134 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2135 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2136 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2137 | xfs_iext_remove(ip, icur, state); |
| 2138 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2139 | xfs_iext_prev(ifp, icur); |
| 2140 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2141 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2142 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | if (cur == NULL) |
| 2144 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2145 | else { |
| 2146 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2147 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2148 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2150 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2151 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2153 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2154 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2156 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2157 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2159 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2160 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2162 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2163 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2164 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | goto done; |
| 2166 | } |
| 2167 | break; |
| 2168 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2169 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | /* |
| 2171 | * Setting all of a previous oldext extent to newext. |
| 2172 | * The left neighbor is contiguous, the right is not. |
| 2173 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2174 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2175 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2176 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2177 | xfs_iext_prev(ifp, icur); |
| 2178 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2179 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2180 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | if (cur == NULL) |
| 2182 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2183 | else { |
| 2184 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2185 | error = xfs_bmbt_lookup_eq(cur, &PREV, &i); |
| 2186 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2188 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2189 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2191 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2192 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2194 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2195 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2196 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | goto done; |
| 2198 | } |
| 2199 | break; |
| 2200 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2201 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | /* |
| 2203 | * Setting all of a previous oldext extent to newext. |
| 2204 | * The right neighbor is contiguous, the left is not. |
| 2205 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2206 | PREV.br_blockcount += RIGHT.br_blockcount; |
| 2207 | PREV.br_state = new->br_state; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2208 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2209 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2210 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2211 | xfs_iext_prev(ifp, icur); |
| 2212 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2213 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2214 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2215 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | if (cur == NULL) |
| 2217 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2218 | else { |
| 2219 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2220 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2221 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2223 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2224 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2226 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2227 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2229 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2230 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2231 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | goto done; |
| 2233 | } |
| 2234 | break; |
| 2235 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2236 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | /* |
| 2238 | * Setting all of a previous oldext extent to newext. |
| 2239 | * Neither the left nor right neighbors are contiguous with |
| 2240 | * the new one. |
| 2241 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2242 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2243 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | if (cur == NULL) |
| 2246 | rval = XFS_ILOG_DEXT; |
| 2247 | else { |
| 2248 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2249 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2250 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2252 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2253 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2254 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | goto done; |
| 2256 | } |
| 2257 | break; |
| 2258 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2259 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | /* |
| 2261 | * Setting the first part of a previous oldext extent to newext. |
| 2262 | * The left neighbor is contiguous. |
| 2263 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2264 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2265 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2266 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2267 | PREV.br_startoff += new->br_blockcount; |
| 2268 | PREV.br_startblock += new->br_blockcount; |
| 2269 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2270 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2271 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2272 | xfs_iext_prev(ifp, icur); |
| 2273 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | if (cur == NULL) |
| 2276 | rval = XFS_ILOG_DEXT; |
| 2277 | else { |
| 2278 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2279 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2280 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2282 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2283 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2284 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2286 | error = xfs_btree_decrement(cur, 0, &i); |
| 2287 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2289 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2290 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | goto done; |
| 2292 | } |
| 2293 | break; |
| 2294 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2295 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | /* |
| 2297 | * Setting the first part of a previous oldext extent to newext. |
| 2298 | * The left neighbor is not contiguous. |
| 2299 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2300 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2301 | PREV.br_startoff += new->br_blockcount; |
| 2302 | PREV.br_startblock += new->br_blockcount; |
| 2303 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2304 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2305 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2306 | xfs_iext_insert(ip, icur, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2307 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2308 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | if (cur == NULL) |
| 2310 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2311 | else { |
| 2312 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2313 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2314 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2316 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2317 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2318 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | goto done; |
| 2320 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2321 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2323 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | } |
| 2325 | break; |
| 2326 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2327 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | /* |
| 2329 | * Setting the last part of a previous oldext extent to newext. |
| 2330 | * The right neighbor is contiguous with the new allocation. |
| 2331 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2332 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2333 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2334 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2335 | RIGHT.br_startoff = new->br_startoff; |
| 2336 | RIGHT.br_startblock = new->br_startblock; |
| 2337 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2338 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2339 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2340 | xfs_iext_next(ifp, icur); |
| 2341 | xfs_iext_update_extent(ip, state, icur, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | if (cur == NULL) |
| 2344 | rval = XFS_ILOG_DEXT; |
| 2345 | else { |
| 2346 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2347 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2348 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2350 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2351 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2352 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2354 | error = xfs_btree_increment(cur, 0, &i); |
| 2355 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2357 | error = xfs_bmbt_update(cur, &RIGHT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2358 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | goto done; |
| 2360 | } |
| 2361 | break; |
| 2362 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2363 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | /* |
| 2365 | * Setting the last part of a previous oldext extent to newext. |
| 2366 | * The right neighbor is not contiguous. |
| 2367 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2368 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2369 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2370 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2371 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2372 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2373 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2374 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2375 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2376 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | if (cur == NULL) |
| 2378 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2379 | else { |
| 2380 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2381 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2382 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2384 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2385 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2386 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2388 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2389 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2391 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2392 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2394 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | } |
| 2396 | break; |
| 2397 | |
| 2398 | case 0: |
| 2399 | /* |
| 2400 | * Setting the middle part of a previous oldext extent to |
| 2401 | * newext. Contiguity is impossible here. |
| 2402 | * One extent becomes three extents. |
| 2403 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2404 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2405 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | r[0] = *new; |
| 2408 | r[1].br_startoff = new_endoff; |
| 2409 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2410 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2412 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2413 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2414 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2415 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2416 | xfs_iext_insert(ip, icur, &r[1], state); |
| 2417 | xfs_iext_insert(ip, icur, &r[0], state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2418 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2419 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2420 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | if (cur == NULL) |
| 2422 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2423 | else { |
| 2424 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2425 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2426 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2428 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2430 | error = xfs_bmbt_update(cur, &r[1]); |
| 2431 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | goto done; |
| 2433 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2435 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2437 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2438 | /* |
| 2439 | * Reset the cursor to the position of the new extent |
| 2440 | * we are about to insert as we can't trust it after |
| 2441 | * the previous insert. |
| 2442 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2443 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2444 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2446 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2448 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2450 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | } |
| 2452 | break; |
| 2453 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2454 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2455 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2456 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2457 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2458 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2459 | case BMAP_LEFT_CONTIG: |
| 2460 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | /* |
| 2462 | * These cases are all impossible. |
| 2463 | */ |
| 2464 | ASSERT(0); |
| 2465 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2466 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2467 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2468 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2469 | if (error) |
| 2470 | goto done; |
| 2471 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2472 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2473 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2474 | int tmp_logflags; /* partial log flag return val */ |
| 2475 | |
| 2476 | ASSERT(cur == NULL); |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 2477 | error = xfs_bmap_extents_to_btree(tp, ip, first, &cur, 0, |
| 2478 | &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2479 | *logflagsp |= tmp_logflags; |
| 2480 | if (error) |
| 2481 | goto done; |
| 2482 | } |
| 2483 | |
| 2484 | /* clear out the allocated field, done with it now in any case. */ |
| 2485 | if (cur) { |
| 2486 | cur->bc_private.b.allocated = 0; |
| 2487 | *curp = cur; |
| 2488 | } |
| 2489 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2490 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2492 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | return error; |
| 2494 | #undef LEFT |
| 2495 | #undef RIGHT |
| 2496 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2500 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2502 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | xfs_bmap_add_extent_hole_delay( |
| 2504 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2505 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2506 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2507 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2509 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2511 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2512 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2513 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2514 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2515 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2517 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2518 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | /* |
| 2521 | * Check and set flags if this segment has a left neighbor |
| 2522 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2523 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2524 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2525 | if (isnullstartblock(left.br_startblock)) |
| 2526 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | /* |
| 2530 | * Check and set flags if the current (right) segment exists. |
| 2531 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2532 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2533 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2534 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2535 | if (isnullstartblock(right.br_startblock)) |
| 2536 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | /* |
| 2540 | * Set contiguity flags on the left and right neighbors. |
| 2541 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2542 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2543 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2544 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2545 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2546 | state |= BMAP_LEFT_CONTIG; |
| 2547 | |
| 2548 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2549 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2550 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2551 | (!(state & BMAP_LEFT_CONTIG) || |
| 2552 | (left.br_blockcount + new->br_blockcount + |
| 2553 | right.br_blockcount <= MAXEXTLEN))) |
| 2554 | state |= BMAP_RIGHT_CONTIG; |
| 2555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | /* |
| 2557 | * Switch out based on the contiguity flags. |
| 2558 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2559 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2560 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | /* |
| 2562 | * New allocation is contiguous with delayed allocations |
| 2563 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2564 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | */ |
| 2566 | temp = left.br_blockcount + new->br_blockcount + |
| 2567 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2568 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2569 | oldlen = startblockval(left.br_startblock) + |
| 2570 | startblockval(new->br_startblock) + |
| 2571 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2572 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2573 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2574 | left.br_startblock = nullstartblock(newlen); |
| 2575 | left.br_blockcount = temp; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2576 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2577 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2578 | xfs_iext_prev(ifp, icur); |
| 2579 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | break; |
| 2581 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2582 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | /* |
| 2584 | * New allocation is contiguous with a delayed allocation |
| 2585 | * on the left. |
| 2586 | * Merge the new allocation with the left neighbor. |
| 2587 | */ |
| 2588 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2589 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2590 | oldlen = startblockval(left.br_startblock) + |
| 2591 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2592 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2593 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2594 | left.br_blockcount = temp; |
| 2595 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | 41d196f | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 2596 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2597 | xfs_iext_prev(ifp, icur); |
| 2598 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | break; |
| 2600 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2601 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | /* |
| 2603 | * New allocation is contiguous with a delayed allocation |
| 2604 | * on the right. |
| 2605 | * Merge the new allocation with the right neighbor. |
| 2606 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2608 | oldlen = startblockval(new->br_startblock) + |
| 2609 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2610 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2611 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2612 | right.br_startoff = new->br_startoff; |
| 2613 | right.br_startblock = nullstartblock(newlen); |
| 2614 | right.br_blockcount = temp; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2615 | xfs_iext_update_extent(ip, state, icur, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | break; |
| 2617 | |
| 2618 | case 0: |
| 2619 | /* |
| 2620 | * New allocation is not contiguous with another |
| 2621 | * delayed allocation. |
| 2622 | * Insert a new entry. |
| 2623 | */ |
| 2624 | oldlen = newlen = 0; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2625 | xfs_iext_insert(ip, icur, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | break; |
| 2627 | } |
| 2628 | if (oldlen != newlen) { |
| 2629 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2630 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2631 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | /* |
| 2633 | * Nothing to do for disk quota accounting here. |
| 2634 | */ |
| 2635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2639 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | */ |
| 2641 | STATIC int /* error */ |
| 2642 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2643 | struct xfs_trans *tp, |
| 2644 | struct xfs_inode *ip, |
| 2645 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2646 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2647 | struct xfs_btree_cur **curp, |
| 2648 | struct xfs_bmbt_irec *new, |
| 2649 | xfs_fsblock_t *first, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2650 | int *logflagsp, |
| 2651 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2653 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2654 | struct xfs_mount *mp = ip->i_mount; |
| 2655 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2659 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2660 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2661 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2662 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2664 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2665 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2666 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2667 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | /* |
| 2670 | * Check and set flags if this segment has a left neighbor. |
| 2671 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2672 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2673 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2674 | if (isnullstartblock(left.br_startblock)) |
| 2675 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | /* |
| 2679 | * Check and set flags if this segment has a current value. |
| 2680 | * Not true if we're inserting into the "hole" at eof. |
| 2681 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2682 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2683 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2684 | if (isnullstartblock(right.br_startblock)) |
| 2685 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | /* |
| 2689 | * We're inserting a real allocation between "left" and "right". |
| 2690 | * Set the contiguity flags. Don't let extents get too large. |
| 2691 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2692 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2693 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2694 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2695 | left.br_state == new->br_state && |
| 2696 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2697 | state |= BMAP_LEFT_CONTIG; |
| 2698 | |
| 2699 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2700 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2701 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2702 | new->br_state == right.br_state && |
| 2703 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2704 | (!(state & BMAP_LEFT_CONTIG) || |
| 2705 | left.br_blockcount + new->br_blockcount + |
| 2706 | right.br_blockcount <= MAXEXTLEN)) |
| 2707 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2709 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | /* |
| 2711 | * Select which case we're in here, and implement it. |
| 2712 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2713 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2714 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | /* |
| 2716 | * New allocation is contiguous with real allocations on the |
| 2717 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2718 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2720 | left.br_blockcount += new->br_blockcount + right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2721 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2722 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2723 | xfs_iext_prev(ifp, icur); |
| 2724 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2725 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2726 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2727 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2728 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2729 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2730 | } else { |
| 2731 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2732 | error = xfs_bmbt_lookup_eq(cur, &right, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2733 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2734 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2735 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2736 | error = xfs_btree_delete(cur, &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_decrement(cur, 0, &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 | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2744 | error = xfs_bmbt_update(cur, &left); |
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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2748 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2750 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | /* |
| 2752 | * New allocation is contiguous with a real allocation |
| 2753 | * on the left. |
| 2754 | * Merge the new allocation with the left neighbor. |
| 2755 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2756 | old = left; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2757 | left.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 1d2e008 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2758 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2759 | xfs_iext_prev(ifp, icur); |
| 2760 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2761 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2762 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2763 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2764 | } else { |
| 2765 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2766 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2767 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2768 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2769 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2770 | error = xfs_bmbt_update(cur, &left); |
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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2774 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2776 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | /* |
| 2778 | * New allocation is contiguous with a real allocation |
| 2779 | * on the right. |
| 2780 | * Merge the new allocation with the right neighbor. |
| 2781 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2782 | old = right; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2783 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2784 | right.br_startoff = new->br_startoff; |
| 2785 | right.br_startblock = new->br_startblock; |
| 2786 | right.br_blockcount += new->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2787 | xfs_iext_update_extent(ip, state, icur, &right); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2788 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2789 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2790 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2791 | } else { |
| 2792 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2793 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2794 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2795 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2796 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2797 | error = xfs_bmbt_update(cur, &right); |
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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2801 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | |
| 2803 | case 0: |
| 2804 | /* |
| 2805 | * New allocation is not contiguous with another |
| 2806 | * real allocation. |
| 2807 | * Insert a new entry. |
| 2808 | */ |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2809 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2810 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2811 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2812 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2813 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2814 | } else { |
| 2815 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2816 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2817 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2818 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2819 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2820 | error = xfs_btree_insert(cur, &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 == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2825 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2827 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2828 | /* add reverse mapping unless caller opted out */ |
| 2829 | if (!(flags & XFS_BMAPI_NORMAP)) { |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 2830 | error = xfs_rmap_map_extent(mp, tp->t_dfops, ip, whichfork, |
| 2831 | new); |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2832 | if (error) |
| 2833 | goto done; |
| 2834 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2835 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2836 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2837 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2838 | int tmp_logflags; /* partial log flag return val */ |
| 2839 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2840 | ASSERT(cur == NULL); |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 2841 | error = xfs_bmap_extents_to_btree(tp, ip, first, curp, 0, |
| 2842 | &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2843 | *logflagsp |= tmp_logflags; |
| 2844 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2845 | if (error) |
| 2846 | goto done; |
| 2847 | } |
| 2848 | |
| 2849 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2850 | if (cur) |
| 2851 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2852 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2853 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2854 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2855 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2856 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2859 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2860 | * Functions used in the extent read, allocate and remove paths |
| 2861 | */ |
| 2862 | |
| 2863 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2864 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2865 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2866 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2867 | xfs_bmap_extsize_align( |
| 2868 | xfs_mount_t *mp, |
| 2869 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2870 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2871 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2872 | int rt, /* is this a realtime inode? */ |
| 2873 | int eof, /* is extent at end-of-file? */ |
| 2874 | int delay, /* creating delalloc extent? */ |
| 2875 | int convert, /* overwriting unwritten extent? */ |
| 2876 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2877 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2878 | { |
| 2879 | xfs_fileoff_t orig_off; /* original offset */ |
| 2880 | xfs_extlen_t orig_alen; /* original length */ |
| 2881 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2882 | xfs_fileoff_t nexto; /* next file offset */ |
| 2883 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2884 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2885 | xfs_extlen_t align_alen; /* temp for length */ |
| 2886 | xfs_extlen_t temp; /* temp for calculations */ |
| 2887 | |
| 2888 | if (convert) |
| 2889 | return 0; |
| 2890 | |
| 2891 | orig_off = align_off = *offp; |
| 2892 | orig_alen = align_alen = *lenp; |
| 2893 | orig_end = orig_off + orig_alen; |
| 2894 | |
| 2895 | /* |
| 2896 | * If this request overlaps an existing extent, then don't |
| 2897 | * attempt to perform any additional alignment. |
| 2898 | */ |
| 2899 | if (!delay && !eof && |
| 2900 | (orig_off >= gotp->br_startoff) && |
| 2901 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2902 | return 0; |
| 2903 | } |
| 2904 | |
| 2905 | /* |
| 2906 | * If the file offset is unaligned vs. the extent size |
| 2907 | * we need to align it. This will be possible unless |
| 2908 | * the file was previously written with a kernel that didn't |
| 2909 | * perform this alignment, or if a truncate shot us in the |
| 2910 | * foot. |
| 2911 | */ |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 2912 | div_u64_rem(orig_off, extsz, &temp); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2913 | if (temp) { |
| 2914 | align_alen += temp; |
| 2915 | align_off -= temp; |
| 2916 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2917 | |
| 2918 | /* Same adjustment for the end of the requested area. */ |
| 2919 | temp = (align_alen % extsz); |
| 2920 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2921 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2922 | |
| 2923 | /* |
| 2924 | * For large extent hint sizes, the aligned extent might be larger than |
| 2925 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 2926 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 2927 | * short allocation just fine, so it is safe to do this. We only want to |
| 2928 | * do it when we are forced to, though, because it means more allocation |
| 2929 | * operations are required. |
| 2930 | */ |
| 2931 | while (align_alen > MAXEXTLEN) |
| 2932 | align_alen -= extsz; |
| 2933 | ASSERT(align_alen <= MAXEXTLEN); |
| 2934 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2935 | /* |
| 2936 | * If the previous block overlaps with this proposed allocation |
| 2937 | * then move the start forward without adjusting the length. |
| 2938 | */ |
| 2939 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2940 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2941 | prevo = prevp->br_startoff; |
| 2942 | else |
| 2943 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2944 | } else |
| 2945 | prevo = 0; |
| 2946 | if (align_off != orig_off && align_off < prevo) |
| 2947 | align_off = prevo; |
| 2948 | /* |
| 2949 | * If the next block overlaps with this proposed allocation |
| 2950 | * then move the start back without adjusting the length, |
| 2951 | * but not before offset 0. |
| 2952 | * This may of course make the start overlap previous block, |
| 2953 | * and if we hit the offset 0 limit then the next block |
| 2954 | * can still overlap too. |
| 2955 | */ |
| 2956 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2957 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2958 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2959 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2960 | else |
| 2961 | nexto = gotp->br_startoff; |
| 2962 | } else |
| 2963 | nexto = NULLFILEOFF; |
| 2964 | if (!eof && |
| 2965 | align_off + align_alen != orig_end && |
| 2966 | align_off + align_alen > nexto) |
| 2967 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2968 | /* |
| 2969 | * If we're now overlapping the next or previous extent that |
| 2970 | * means we can't fit an extsz piece in this hole. Just move |
| 2971 | * the start forward to the first valid spot and set |
| 2972 | * the length so we hit the end. |
| 2973 | */ |
| 2974 | if (align_off != orig_off && align_off < prevo) |
| 2975 | align_off = prevo; |
| 2976 | if (align_off + align_alen != orig_end && |
| 2977 | align_off + align_alen > nexto && |
| 2978 | nexto != NULLFILEOFF) { |
| 2979 | ASSERT(nexto > prevo); |
| 2980 | align_alen = nexto - align_off; |
| 2981 | } |
| 2982 | |
| 2983 | /* |
| 2984 | * If realtime, and the result isn't a multiple of the realtime |
| 2985 | * extent size we need to remove blocks until it is. |
| 2986 | */ |
| 2987 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 2988 | /* |
| 2989 | * We're not covering the original request, or |
| 2990 | * we won't be able to once we fix the length. |
| 2991 | */ |
| 2992 | if (orig_off < align_off || |
| 2993 | orig_end > align_off + align_alen || |
| 2994 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2995 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2996 | /* |
| 2997 | * Try to fix it by moving the start up. |
| 2998 | */ |
| 2999 | if (align_off + temp <= orig_off) { |
| 3000 | align_alen -= temp; |
| 3001 | align_off += temp; |
| 3002 | } |
| 3003 | /* |
| 3004 | * Try to fix it by moving the end in. |
| 3005 | */ |
| 3006 | else if (align_off + align_alen - temp >= orig_end) |
| 3007 | align_alen -= temp; |
| 3008 | /* |
| 3009 | * Set the start to the minimum then trim the length. |
| 3010 | */ |
| 3011 | else { |
| 3012 | align_alen -= orig_off - align_off; |
| 3013 | align_off = orig_off; |
| 3014 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3015 | } |
| 3016 | /* |
| 3017 | * Result doesn't cover the request, fail it. |
| 3018 | */ |
| 3019 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3020 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3021 | } else { |
| 3022 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3023 | /* see MAXEXTLEN handling above */ |
| 3024 | ASSERT(orig_end <= align_off + align_alen || |
| 3025 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | #ifdef DEBUG |
| 3029 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3030 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3031 | if (prevp->br_startoff != NULLFILEOFF) |
| 3032 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3033 | #endif |
| 3034 | |
| 3035 | *lenp = align_alen; |
| 3036 | *offp = align_off; |
| 3037 | return 0; |
| 3038 | } |
| 3039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3041 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3042 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3043 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3044 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | { |
| 3046 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3048 | xfs_mount_t *mp; /* mount point structure */ |
| 3049 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3050 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | |
| 3052 | #define ISVALID(x,y) \ |
| 3053 | (rt ? \ |
| 3054 | (x) < mp->m_sb.sb_rblocks : \ |
| 3055 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3056 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3057 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3060 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3061 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3062 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3063 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | /* |
| 3065 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3066 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3068 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3069 | !isnullstartblock(ap->prev.br_startblock) && |
| 3070 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3071 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3072 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | /* |
| 3074 | * Adjust for the gap between prevp and us. |
| 3075 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3076 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3077 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3079 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3080 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | } |
| 3082 | /* |
| 3083 | * If not at eof, then compare the two neighbor blocks. |
| 3084 | * Figure out whether either one gives us a good starting point, |
| 3085 | * and pick the better one. |
| 3086 | */ |
| 3087 | else if (!ap->eof) { |
| 3088 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3089 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3090 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3091 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3092 | |
| 3093 | /* |
| 3094 | * If there's a previous (left) block, select a requested |
| 3095 | * start block based on it. |
| 3096 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3097 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3098 | !isnullstartblock(ap->prev.br_startblock) && |
| 3099 | (prevbno = ap->prev.br_startblock + |
| 3100 | ap->prev.br_blockcount) && |
| 3101 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | /* |
| 3103 | * Calculate gap to end of previous block. |
| 3104 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3105 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3106 | (ap->prev.br_startoff + |
| 3107 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | /* |
| 3109 | * Figure the startblock based on the previous block's |
| 3110 | * end and the gap size. |
| 3111 | * Heuristic! |
| 3112 | * If the gap is large relative to the piece we're |
| 3113 | * allocating, or using it gives us an invalid block |
| 3114 | * number, then just use the end of the previous block. |
| 3115 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3116 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3118 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | prevbno += adjust; |
| 3120 | else |
| 3121 | prevdiff += adjust; |
| 3122 | /* |
| 3123 | * If the firstblock forbids it, can't use it, |
| 3124 | * must use default. |
| 3125 | */ |
| 3126 | if (!rt && !nullfb && |
| 3127 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3128 | prevbno = NULLFSBLOCK; |
| 3129 | } |
| 3130 | /* |
| 3131 | * No previous block or can't follow it, just default. |
| 3132 | */ |
| 3133 | else |
| 3134 | prevbno = NULLFSBLOCK; |
| 3135 | /* |
| 3136 | * If there's a following (right) block, select a requested |
| 3137 | * start block based on it. |
| 3138 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3139 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | /* |
| 3141 | * Calculate gap to start of next block. |
| 3142 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3143 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | /* |
| 3145 | * Figure the startblock based on the next block's |
| 3146 | * start and the gap size. |
| 3147 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3148 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | /* |
| 3150 | * Heuristic! |
| 3151 | * If the gap is large relative to the piece we're |
| 3152 | * allocating, or using it gives us an invalid block |
| 3153 | * number, then just use the start of the next block |
| 3154 | * offset by our length. |
| 3155 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3156 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3158 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3159 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3160 | gotbno -= ap->length; |
| 3161 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | } else |
| 3163 | gotdiff += adjust; |
| 3164 | /* |
| 3165 | * If the firstblock forbids it, can't use it, |
| 3166 | * must use default. |
| 3167 | */ |
| 3168 | if (!rt && !nullfb && |
| 3169 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3170 | gotbno = NULLFSBLOCK; |
| 3171 | } |
| 3172 | /* |
| 3173 | * No next block, just default. |
| 3174 | */ |
| 3175 | else |
| 3176 | gotbno = NULLFSBLOCK; |
| 3177 | /* |
| 3178 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3179 | * 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] | 3180 | */ |
| 3181 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3182 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3184 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3186 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3188 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3189 | } |
| 3190 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3191 | static int |
| 3192 | xfs_bmap_longest_free_extent( |
| 3193 | struct xfs_trans *tp, |
| 3194 | xfs_agnumber_t ag, |
| 3195 | xfs_extlen_t *blen, |
| 3196 | int *notinit) |
| 3197 | { |
| 3198 | struct xfs_mount *mp = tp->t_mountp; |
| 3199 | struct xfs_perag *pag; |
| 3200 | xfs_extlen_t longest; |
| 3201 | int error = 0; |
| 3202 | |
| 3203 | pag = xfs_perag_get(mp, ag); |
| 3204 | if (!pag->pagf_init) { |
| 3205 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3206 | if (error) |
| 3207 | goto out; |
| 3208 | |
| 3209 | if (!pag->pagf_init) { |
| 3210 | *notinit = 1; |
| 3211 | goto out; |
| 3212 | } |
| 3213 | } |
| 3214 | |
Eric Sandeen | a1f6941 | 2018-04-06 10:09:42 -0700 | [diff] [blame] | 3215 | longest = xfs_alloc_longest_free_extent(pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3216 | xfs_alloc_min_freelist(mp, pag), |
| 3217 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3218 | if (*blen < longest) |
| 3219 | *blen = longest; |
| 3220 | |
| 3221 | out: |
| 3222 | xfs_perag_put(pag); |
| 3223 | return error; |
| 3224 | } |
| 3225 | |
| 3226 | static void |
| 3227 | xfs_bmap_select_minlen( |
| 3228 | struct xfs_bmalloca *ap, |
| 3229 | struct xfs_alloc_arg *args, |
| 3230 | xfs_extlen_t *blen, |
| 3231 | int notinit) |
| 3232 | { |
| 3233 | if (notinit || *blen < ap->minlen) { |
| 3234 | /* |
| 3235 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3236 | * there is space for this request. |
| 3237 | */ |
| 3238 | args->minlen = ap->minlen; |
| 3239 | } else if (*blen < args->maxlen) { |
| 3240 | /* |
| 3241 | * If the best seen length is less than the request length, |
| 3242 | * use the best as the minimum. |
| 3243 | */ |
| 3244 | args->minlen = *blen; |
| 3245 | } else { |
| 3246 | /* |
| 3247 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3248 | * as the minimum. |
| 3249 | */ |
| 3250 | args->minlen = args->maxlen; |
| 3251 | } |
| 3252 | } |
| 3253 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3254 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3255 | xfs_bmap_btalloc_nullfb( |
| 3256 | struct xfs_bmalloca *ap, |
| 3257 | struct xfs_alloc_arg *args, |
| 3258 | xfs_extlen_t *blen) |
| 3259 | { |
| 3260 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3261 | xfs_agnumber_t ag, startag; |
| 3262 | int notinit = 0; |
| 3263 | int error; |
| 3264 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3265 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3266 | args->total = ap->total; |
| 3267 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3268 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3269 | if (startag == NULLAGNUMBER) |
| 3270 | startag = ag = 0; |
| 3271 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3272 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3273 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3274 | ¬init); |
| 3275 | if (error) |
| 3276 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3277 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3278 | if (++ag == mp->m_sb.sb_agcount) |
| 3279 | ag = 0; |
| 3280 | if (ag == startag) |
| 3281 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3282 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3283 | |
| 3284 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3285 | return 0; |
| 3286 | } |
| 3287 | |
| 3288 | STATIC int |
| 3289 | xfs_bmap_btalloc_filestreams( |
| 3290 | struct xfs_bmalloca *ap, |
| 3291 | struct xfs_alloc_arg *args, |
| 3292 | xfs_extlen_t *blen) |
| 3293 | { |
| 3294 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3295 | xfs_agnumber_t ag; |
| 3296 | int notinit = 0; |
| 3297 | int error; |
| 3298 | |
| 3299 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3300 | args->total = ap->total; |
| 3301 | |
| 3302 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3303 | if (ag == NULLAGNUMBER) |
| 3304 | ag = 0; |
| 3305 | |
| 3306 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3307 | if (error) |
| 3308 | return error; |
| 3309 | |
| 3310 | if (*blen < args->maxlen) { |
| 3311 | error = xfs_filestream_new_ag(ap, &ag); |
| 3312 | if (error) |
| 3313 | return error; |
| 3314 | |
| 3315 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3316 | ¬init); |
| 3317 | if (error) |
| 3318 | return error; |
| 3319 | |
| 3320 | } |
| 3321 | |
| 3322 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3323 | |
| 3324 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3325 | * Set the failure fallback case to look in the selected AG as stream |
| 3326 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3327 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3328 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3329 | return 0; |
| 3330 | } |
| 3331 | |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3332 | /* Update all inode and quota accounting for the allocation we just did. */ |
| 3333 | static void |
| 3334 | xfs_bmap_btalloc_accounting( |
| 3335 | struct xfs_bmalloca *ap, |
| 3336 | struct xfs_alloc_arg *args) |
| 3337 | { |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3338 | if (ap->flags & XFS_BMAPI_COWFORK) { |
| 3339 | /* |
| 3340 | * COW fork blocks are in-core only and thus are treated as |
| 3341 | * in-core quota reservation (like delalloc blocks) even when |
| 3342 | * converted to real blocks. The quota reservation is not |
| 3343 | * accounted to disk until blocks are remapped to the data |
| 3344 | * fork. So if these blocks were previously delalloc, we |
| 3345 | * already have quota reservation and there's nothing to do |
| 3346 | * yet. |
| 3347 | */ |
| 3348 | if (ap->wasdel) |
| 3349 | return; |
| 3350 | |
| 3351 | /* |
| 3352 | * Otherwise, we've allocated blocks in a hole. The transaction |
| 3353 | * has acquired in-core quota reservation for this extent. |
| 3354 | * Rather than account these as real blocks, however, we reduce |
| 3355 | * the transaction quota reservation based on the allocation. |
| 3356 | * This essentially transfers the transaction quota reservation |
| 3357 | * to that of a delalloc extent. |
| 3358 | */ |
| 3359 | ap->ip->i_delayed_blks += args->len; |
| 3360 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS, |
| 3361 | -(long)args->len); |
| 3362 | return; |
| 3363 | } |
| 3364 | |
| 3365 | /* data/attr fork only */ |
| 3366 | ap->ip->i_d.di_nblocks += args->len; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3367 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3368 | if (ap->wasdel) |
| 3369 | ap->ip->i_delayed_blks -= args->len; |
| 3370 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 3371 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT, |
| 3372 | args->len); |
| 3373 | } |
| 3374 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3375 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3376 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3377 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3378 | { |
| 3379 | xfs_mount_t *mp; /* mount point structure */ |
| 3380 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3381 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3382 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3383 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3384 | xfs_alloc_arg_t args; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3385 | xfs_fileoff_t orig_offset; |
| 3386 | xfs_extlen_t orig_length; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3387 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3388 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3389 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3390 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3391 | int tryagain; |
| 3392 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3393 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3394 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3395 | ASSERT(ap->length); |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3396 | orig_offset = ap->offset; |
| 3397 | orig_length = ap->length; |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3398 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3399 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3400 | |
| 3401 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3402 | stripe_align = 0; |
| 3403 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3404 | stripe_align = mp->m_swidth; |
| 3405 | else if (mp->m_dalign) |
| 3406 | stripe_align = mp->m_dalign; |
| 3407 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3408 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3409 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3410 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3411 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3412 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3413 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3414 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3415 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3416 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3417 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3418 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3419 | |
| 3420 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3421 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3422 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3423 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3424 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3425 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3426 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3427 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3428 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3429 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3430 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3431 | } |
| 3432 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3433 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3434 | |
| 3435 | xfs_bmap_adjacent(ap); |
| 3436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3437 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3438 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | * it's in the right allocation group. |
| 3440 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3441 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | ; |
| 3443 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3444 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | * Normal allocation, done through xfs_alloc_vextent. |
| 3447 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3448 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3449 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3450 | args.tp = ap->tp; |
| 3451 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3452 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3453 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3454 | |
| 3455 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 9bb54cb | 2018-06-07 07:54:02 -0700 | [diff] [blame] | 3456 | args.maxlen = min(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3457 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3458 | blen = 0; |
| 3459 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3460 | /* |
| 3461 | * Search for an allocation group with a single extent large |
| 3462 | * enough for the request. If one isn't found, then adjust |
| 3463 | * the minimum allocation size to the largest space found. |
| 3464 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3465 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3466 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3467 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3468 | else |
| 3469 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3470 | if (error) |
| 3471 | return error; |
Brian Foster | 3e3673e | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 3472 | } else if (ap->tp->t_dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3473 | if (xfs_inode_is_filestream(ap->ip)) |
| 3474 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3475 | else |
| 3476 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3477 | args.total = args.minlen = ap->minlen; |
| 3478 | } else { |
| 3479 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3480 | args.total = ap->total; |
| 3481 | args.minlen = ap->minlen; |
| 3482 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3483 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3484 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3485 | args.prod = align; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3486 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3487 | if (args.mod) |
| 3488 | args.mod = args.prod - args.mod; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3489 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3490 | args.prod = 1; |
| 3491 | args.mod = 0; |
| 3492 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3493 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3494 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3495 | if (args.mod) |
| 3496 | args.mod = args.prod - args.mod; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3497 | } |
| 3498 | /* |
| 3499 | * If we are not low on available data blocks, and the |
| 3500 | * underlying logical volume manager is a stripe, and |
| 3501 | * the file offset is zero then try to allocate data |
| 3502 | * blocks on stripe unit boundary. |
| 3503 | * NOTE: ap->aeof is only set if the allocation length |
| 3504 | * is >= the stripe unit and the allocation offset is |
| 3505 | * at the end of file. |
| 3506 | */ |
Brian Foster | 3e3673e | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 3507 | if (!ap->tp->t_dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3508 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3509 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3510 | atype = args.type; |
| 3511 | isaligned = 1; |
| 3512 | /* |
| 3513 | * Adjust for alignment |
| 3514 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3515 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3516 | args.minlen = blen - args.alignment; |
| 3517 | args.minalignslop = 0; |
| 3518 | } else { |
| 3519 | /* |
| 3520 | * First try an exact bno allocation. |
| 3521 | * If it fails then do a near or start bno |
| 3522 | * allocation with alignment turned on. |
| 3523 | */ |
| 3524 | atype = args.type; |
| 3525 | tryagain = 1; |
| 3526 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3527 | args.alignment = 1; |
| 3528 | /* |
| 3529 | * Compute the minlen+alignment for the |
| 3530 | * next case. Set slop so that the value |
| 3531 | * of minlen+alignment+slop doesn't go up |
| 3532 | * between the calls. |
| 3533 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3534 | if (blen > stripe_align && blen <= args.maxlen) |
| 3535 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3536 | else |
| 3537 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3538 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3539 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3540 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3541 | args.minlen - 1; |
| 3542 | else |
| 3543 | args.minalignslop = 0; |
| 3544 | } |
| 3545 | } else { |
| 3546 | args.alignment = 1; |
| 3547 | args.minalignslop = 0; |
| 3548 | } |
| 3549 | args.minleft = ap->minleft; |
| 3550 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3551 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3552 | args.datatype = ap->datatype; |
| 3553 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3554 | args.ip = ap->ip; |
| 3555 | |
| 3556 | error = xfs_alloc_vextent(&args); |
| 3557 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3558 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3559 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3560 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3561 | /* |
| 3562 | * Exact allocation failed. Now try with alignment |
| 3563 | * turned on. |
| 3564 | */ |
| 3565 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3566 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3567 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3568 | args.minlen = nextminlen; |
| 3569 | args.minalignslop = 0; |
| 3570 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | if ((error = xfs_alloc_vextent(&args))) |
| 3572 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3573 | } |
| 3574 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3575 | /* |
| 3576 | * allocation failed, so turn off alignment and |
| 3577 | * try again. |
| 3578 | */ |
| 3579 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3580 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3581 | args.alignment = 0; |
| 3582 | if ((error = xfs_alloc_vextent(&args))) |
| 3583 | return error; |
| 3584 | } |
| 3585 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3586 | args.minlen > ap->minlen) { |
| 3587 | args.minlen = ap->minlen; |
| 3588 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3589 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3590 | if ((error = xfs_alloc_vextent(&args))) |
| 3591 | return error; |
| 3592 | } |
| 3593 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3594 | args.fsbno = 0; |
| 3595 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3596 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3597 | if ((error = xfs_alloc_vextent(&args))) |
| 3598 | return error; |
Brian Foster | 3e3673e | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 3599 | ap->tp->t_dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3600 | } |
| 3601 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3602 | /* |
| 3603 | * check the allocation happened at the same or higher AG than |
| 3604 | * the first block that was allocated. |
| 3605 | */ |
| 3606 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3607 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) <= |
| 3608 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3609 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3610 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3611 | if (*ap->firstblock == NULLFSBLOCK) |
| 3612 | *ap->firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3613 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3614 | ap->length = args.len; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3615 | /* |
| 3616 | * If the extent size hint is active, we tried to round the |
| 3617 | * caller's allocation request offset down to extsz and the |
| 3618 | * length up to another extsz boundary. If we found a free |
| 3619 | * extent we mapped it in starting at this new offset. If the |
| 3620 | * newly mapped space isn't long enough to cover any of the |
| 3621 | * range of offsets that was originally requested, move the |
| 3622 | * mapping up so that we can fill as much of the caller's |
| 3623 | * original request as possible. Free space is apparently |
| 3624 | * very fragmented so we're unlikely to be able to satisfy the |
| 3625 | * hints anyway. |
| 3626 | */ |
| 3627 | if (ap->length <= orig_length) |
| 3628 | ap->offset = orig_offset; |
| 3629 | else if (ap->offset + ap->length < orig_offset + orig_length) |
| 3630 | ap->offset = orig_offset + orig_length - ap->length; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3631 | xfs_bmap_btalloc_accounting(ap, &args); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3632 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3633 | ap->blkno = NULLFSBLOCK; |
| 3634 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | } |
| 3636 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | /* |
| 3640 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3641 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3642 | */ |
| 3643 | STATIC int |
| 3644 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3645 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3646 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3647 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3648 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3649 | return xfs_bmap_rtalloc(ap); |
| 3650 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | } |
| 3652 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3653 | /* Trim extent to fit a logical block range. */ |
| 3654 | void |
| 3655 | xfs_trim_extent( |
| 3656 | struct xfs_bmbt_irec *irec, |
| 3657 | xfs_fileoff_t bno, |
| 3658 | xfs_filblks_t len) |
| 3659 | { |
| 3660 | xfs_fileoff_t distance; |
| 3661 | xfs_fileoff_t end = bno + len; |
| 3662 | |
| 3663 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3664 | irec->br_startoff >= end) { |
| 3665 | irec->br_blockcount = 0; |
| 3666 | return; |
| 3667 | } |
| 3668 | |
| 3669 | if (irec->br_startoff < bno) { |
| 3670 | distance = bno - irec->br_startoff; |
| 3671 | if (isnullstartblock(irec->br_startblock)) |
| 3672 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3673 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3674 | irec->br_startblock != HOLESTARTBLOCK) |
| 3675 | irec->br_startblock += distance; |
| 3676 | irec->br_startoff += distance; |
| 3677 | irec->br_blockcount -= distance; |
| 3678 | } |
| 3679 | |
| 3680 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3681 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3682 | irec->br_blockcount -= distance; |
| 3683 | } |
| 3684 | } |
| 3685 | |
Brian Foster | 40214d1 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 3686 | /* trim extent to within eof */ |
| 3687 | void |
| 3688 | xfs_trim_extent_eof( |
| 3689 | struct xfs_bmbt_irec *irec, |
| 3690 | struct xfs_inode *ip) |
| 3691 | |
| 3692 | { |
| 3693 | xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount, |
| 3694 | i_size_read(VFS_I(ip)))); |
| 3695 | } |
| 3696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3698 | * Trim the returned map to the required bounds |
| 3699 | */ |
| 3700 | STATIC void |
| 3701 | xfs_bmapi_trim_map( |
| 3702 | struct xfs_bmbt_irec *mval, |
| 3703 | struct xfs_bmbt_irec *got, |
| 3704 | xfs_fileoff_t *bno, |
| 3705 | xfs_filblks_t len, |
| 3706 | xfs_fileoff_t obno, |
| 3707 | xfs_fileoff_t end, |
| 3708 | int n, |
| 3709 | int flags) |
| 3710 | { |
| 3711 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3712 | got->br_startoff + got->br_blockcount <= obno) { |
| 3713 | *mval = *got; |
| 3714 | if (isnullstartblock(got->br_startblock)) |
| 3715 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3716 | return; |
| 3717 | } |
| 3718 | |
| 3719 | if (obno > *bno) |
| 3720 | *bno = obno; |
| 3721 | ASSERT((*bno >= obno) || (n == 0)); |
| 3722 | ASSERT(*bno < end); |
| 3723 | mval->br_startoff = *bno; |
| 3724 | if (isnullstartblock(got->br_startblock)) |
| 3725 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3726 | else |
| 3727 | mval->br_startblock = got->br_startblock + |
| 3728 | (*bno - got->br_startoff); |
| 3729 | /* |
| 3730 | * Return the minimum of what we got and what we asked for for |
| 3731 | * the length. We can use the len variable here because it is |
| 3732 | * modified below and we could have been there before coming |
| 3733 | * here if the first part of the allocation didn't overlap what |
| 3734 | * was asked for. |
| 3735 | */ |
| 3736 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3737 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3738 | mval->br_state = got->br_state; |
| 3739 | ASSERT(mval->br_blockcount <= len); |
| 3740 | return; |
| 3741 | } |
| 3742 | |
| 3743 | /* |
| 3744 | * Update and validate the extent map to return |
| 3745 | */ |
| 3746 | STATIC void |
| 3747 | xfs_bmapi_update_map( |
| 3748 | struct xfs_bmbt_irec **map, |
| 3749 | xfs_fileoff_t *bno, |
| 3750 | xfs_filblks_t *len, |
| 3751 | xfs_fileoff_t obno, |
| 3752 | xfs_fileoff_t end, |
| 3753 | int *n, |
| 3754 | int flags) |
| 3755 | { |
| 3756 | xfs_bmbt_irec_t *mval = *map; |
| 3757 | |
| 3758 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3759 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3760 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3761 | (mval->br_startoff < obno)); |
| 3762 | |
| 3763 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3764 | *len = end - *bno; |
| 3765 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3766 | /* update previous map with new information */ |
| 3767 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3768 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3769 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3770 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3771 | mval[-1].br_state = mval->br_state; |
| 3772 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3773 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3774 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3775 | mval->br_startblock == mval[-1].br_startblock + |
| 3776 | mval[-1].br_blockcount && |
Christoph Hellwig | c3a2f9f | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 3777 | mval[-1].br_state == mval->br_state) { |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3778 | ASSERT(mval->br_startoff == |
| 3779 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3780 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3781 | } else if (*n > 0 && |
| 3782 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3783 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3784 | mval->br_startoff == |
| 3785 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3786 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3787 | mval[-1].br_state = mval->br_state; |
| 3788 | } else if (!((*n == 0) && |
| 3789 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3790 | obno))) { |
| 3791 | mval++; |
| 3792 | (*n)++; |
| 3793 | } |
| 3794 | *map = mval; |
| 3795 | } |
| 3796 | |
| 3797 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3798 | * Map file blocks to filesystem blocks without allocation. |
| 3799 | */ |
| 3800 | int |
| 3801 | xfs_bmapi_read( |
| 3802 | struct xfs_inode *ip, |
| 3803 | xfs_fileoff_t bno, |
| 3804 | xfs_filblks_t len, |
| 3805 | struct xfs_bmbt_irec *mval, |
| 3806 | int *nmap, |
| 3807 | int flags) |
| 3808 | { |
| 3809 | struct xfs_mount *mp = ip->i_mount; |
| 3810 | struct xfs_ifork *ifp; |
| 3811 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3812 | xfs_fileoff_t obno; |
| 3813 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3814 | struct xfs_iext_cursor icur; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3815 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3816 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3817 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3818 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3819 | |
| 3820 | ASSERT(*nmap >= 1); |
| 3821 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Christoph Hellwig | c3a2f9f | 2018-07-11 22:26:01 -0700 | [diff] [blame] | 3822 | XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3823 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3824 | |
| 3825 | if (unlikely(XFS_TEST_ERROR( |
| 3826 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3827 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 3828 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3829 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3830 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3834 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3835 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3836 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3837 | |
| 3838 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3839 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3840 | /* No CoW fork? Return a hole. */ |
| 3841 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 3842 | mval->br_startoff = bno; |
| 3843 | mval->br_startblock = HOLESTARTBLOCK; |
| 3844 | mval->br_blockcount = len; |
| 3845 | mval->br_state = XFS_EXT_NORM; |
| 3846 | *nmap = 1; |
| 3847 | return 0; |
| 3848 | } |
| 3849 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3850 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3851 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3852 | if (error) |
| 3853 | return error; |
| 3854 | } |
| 3855 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3856 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3857 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3858 | end = bno + len; |
| 3859 | obno = bno; |
| 3860 | |
| 3861 | while (bno < end && n < *nmap) { |
| 3862 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3863 | if (eof) |
| 3864 | got.br_startoff = end; |
| 3865 | if (got.br_startoff > bno) { |
| 3866 | /* Reading in a hole. */ |
| 3867 | mval->br_startoff = bno; |
| 3868 | mval->br_startblock = HOLESTARTBLOCK; |
| 3869 | mval->br_blockcount = |
| 3870 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3871 | mval->br_state = XFS_EXT_NORM; |
| 3872 | bno += mval->br_blockcount; |
| 3873 | len -= mval->br_blockcount; |
| 3874 | mval++; |
| 3875 | n++; |
| 3876 | continue; |
| 3877 | } |
| 3878 | |
| 3879 | /* set up the extent map to return. */ |
| 3880 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3881 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3882 | |
| 3883 | /* If we're done, stop now. */ |
| 3884 | if (bno >= end || n >= *nmap) |
| 3885 | break; |
| 3886 | |
| 3887 | /* Else go on to the next record. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3888 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3889 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3890 | } |
| 3891 | *nmap = n; |
| 3892 | return 0; |
| 3893 | } |
| 3894 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3895 | /* |
| 3896 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3897 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3898 | * |
| 3899 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3900 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3901 | * to the extent record that was inserted to the inode fork. |
| 3902 | * |
| 3903 | * Note that the allocated extent may have been merged with contiguous extents |
| 3904 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3905 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3906 | * look up the updated record in the inode fork. |
| 3907 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3908 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3909 | xfs_bmapi_reserve_delalloc( |
| 3910 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3911 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3912 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3913 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3914 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3915 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3916 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3917 | int eof) |
| 3918 | { |
| 3919 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3920 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3921 | xfs_extlen_t alen; |
| 3922 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3923 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3924 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3925 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3926 | /* |
| 3927 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3928 | * tag the inode before we return. |
| 3929 | */ |
| 3930 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3931 | if (!eof) |
| 3932 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3933 | if (prealloc && alen >= len) |
| 3934 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3935 | |
| 3936 | /* Figure out the extent size, adjust alen */ |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3937 | if (whichfork == XFS_COW_FORK) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3938 | struct xfs_bmbt_irec prev; |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3939 | xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3940 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3941 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3942 | prev.br_startoff = NULLFILEOFF; |
| 3943 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3944 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3945 | 1, 0, &aoff, &alen); |
| 3946 | ASSERT(!error); |
| 3947 | } |
| 3948 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3949 | /* |
| 3950 | * Make a transaction-less quota reservation for delayed allocation |
| 3951 | * blocks. This number gets adjusted later. We return if we haven't |
| 3952 | * allocated blocks already inside this loop. |
| 3953 | */ |
| 3954 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3955 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3956 | if (error) |
| 3957 | return error; |
| 3958 | |
| 3959 | /* |
| 3960 | * Split changing sb for alen and indlen since they could be coming |
| 3961 | * from different places. |
| 3962 | */ |
| 3963 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 3964 | ASSERT(indlen > 0); |
| 3965 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3966 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3967 | if (error) |
| 3968 | goto out_unreserve_quota; |
| 3969 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3970 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3971 | if (error) |
| 3972 | goto out_unreserve_blocks; |
| 3973 | |
| 3974 | |
| 3975 | ip->i_delayed_blks += alen; |
| 3976 | |
| 3977 | got->br_startoff = aoff; |
| 3978 | got->br_startblock = nullstartblock(indlen); |
| 3979 | got->br_blockcount = alen; |
| 3980 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3981 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3982 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3983 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3984 | /* |
| 3985 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 3986 | * preallocation can occur at the start or end of the extent, even when |
| 3987 | * prealloc == 0, so we must also check the aligned offset and length. |
| 3988 | */ |
| 3989 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 3990 | xfs_inode_set_eofblocks_tag(ip); |
| 3991 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 3992 | xfs_inode_set_cowblocks_tag(ip); |
| 3993 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3994 | return 0; |
| 3995 | |
| 3996 | out_unreserve_blocks: |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3997 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3998 | out_unreserve_quota: |
| 3999 | if (XFS_IS_QUOTA_ON(mp)) |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4000 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4001 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4002 | return error; |
| 4003 | } |
| 4004 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4005 | static int |
| 4006 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4007 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4008 | { |
| 4009 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4010 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4011 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4012 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4013 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4014 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4015 | ASSERT(bma->length > 0); |
| 4016 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4017 | /* |
| 4018 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4019 | * for in this bmap call but that wouldn't be as good. |
| 4020 | */ |
| 4021 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4022 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4023 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4024 | xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4025 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4026 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4027 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4028 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4029 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4033 | * Set the data type being allocated. For the data fork, the first data |
| 4034 | * in the file is treated differently to all other allocations. For the |
| 4035 | * attribute fork, we only need to ensure the allocated range is not on |
| 4036 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4037 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4038 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4039 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4040 | if (whichfork == XFS_DATA_FORK) { |
| 4041 | if (bma->offset == 0) |
| 4042 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4043 | else |
| 4044 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4045 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4046 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4047 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4050 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4051 | |
| 4052 | /* |
| 4053 | * Only want to do the alignment at the eof if it is userdata and |
| 4054 | * allocation length is larger than a stripe unit. |
| 4055 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4056 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4057 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4058 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4059 | if (error) |
| 4060 | return error; |
| 4061 | } |
| 4062 | |
| 4063 | error = xfs_bmap_alloc(bma); |
| 4064 | if (error) |
| 4065 | return error; |
| 4066 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4067 | if (bma->cur) |
| 4068 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4069 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4070 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4071 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4072 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4073 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4074 | } |
| 4075 | /* |
| 4076 | * Bump the number of extents we've allocated |
| 4077 | * in this call. |
| 4078 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4079 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4080 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4081 | if (bma->cur) |
| 4082 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4083 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4084 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4085 | bma->got.br_startoff = bma->offset; |
| 4086 | bma->got.br_startblock = bma->blkno; |
| 4087 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4088 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4089 | |
| 4090 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4091 | * In the data fork, a wasdelay extent has been initialized, so |
| 4092 | * shouldn't be flagged as unwritten. |
| 4093 | * |
| 4094 | * For the cow fork, however, we convert delalloc reservations |
| 4095 | * (extents allocated for speculative preallocation) to |
| 4096 | * allocated unwritten extents, and only convert the unwritten |
| 4097 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4098 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4099 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4100 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4101 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4102 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4103 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4104 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4105 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4106 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4107 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4108 | whichfork, &bma->icur, &bma->cur, &bma->got, |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 4109 | bma->firstblock, &bma->logflags, bma->flags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4110 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4111 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4112 | if (error) |
| 4113 | return error; |
| 4114 | |
| 4115 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4116 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4117 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4118 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4119 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4120 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4121 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4122 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4123 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4124 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4125 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4126 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4127 | return 0; |
| 4128 | } |
| 4129 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4130 | STATIC int |
| 4131 | xfs_bmapi_convert_unwritten( |
| 4132 | struct xfs_bmalloca *bma, |
| 4133 | struct xfs_bmbt_irec *mval, |
| 4134 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4135 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4136 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4137 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4138 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4139 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4140 | int error; |
| 4141 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4142 | /* check if we need to do unwritten->real conversion */ |
| 4143 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4144 | (flags & XFS_BMAPI_PREALLOC)) |
| 4145 | return 0; |
| 4146 | |
| 4147 | /* check if we need to do real->unwritten conversion */ |
| 4148 | if (mval->br_state == XFS_EXT_NORM && |
| 4149 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4150 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4151 | return 0; |
| 4152 | |
| 4153 | /* |
| 4154 | * Modify (by adding) the state flag, if writing. |
| 4155 | */ |
| 4156 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4157 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4158 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4159 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4160 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4161 | } |
| 4162 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4163 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4164 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4165 | /* |
| 4166 | * Before insertion into the bmbt, zero the range being converted |
| 4167 | * if required. |
| 4168 | */ |
| 4169 | if (flags & XFS_BMAPI_ZERO) { |
| 4170 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4171 | mval->br_blockcount); |
| 4172 | if (error) |
| 4173 | return error; |
| 4174 | } |
| 4175 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4176 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4177 | &bma->icur, &bma->cur, mval, bma->firstblock, |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 4178 | &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4179 | /* |
| 4180 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4181 | * path because the conversion might not have done so (e.g., if the |
| 4182 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4183 | * in the transaction for the sake of fsync(), even if nothing has |
| 4184 | * changed, because fsync() will not force the log for this transaction |
| 4185 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4186 | * |
| 4187 | * Note: If we're only converting cow fork extents, there aren't |
| 4188 | * 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] | 4189 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4190 | if (whichfork != XFS_COW_FORK) |
| 4191 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4192 | if (error) |
| 4193 | return error; |
| 4194 | |
| 4195 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4196 | * Update our extent pointer, given that |
| 4197 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4198 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4199 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4200 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4201 | |
| 4202 | /* |
| 4203 | * We may have combined previously unwritten space with written space, |
| 4204 | * so generate another request. |
| 4205 | */ |
| 4206 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4207 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4208 | return 0; |
| 4209 | } |
| 4210 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4211 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4212 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4213 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4214 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4215 | * locking problems. |
| 4216 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | * The returned value in "firstblock" from the first call in a transaction |
| 4218 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4219 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4220 | * the first call in "total"; if no allocation group has that many free |
| 4221 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4222 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4223 | int |
| 4224 | xfs_bmapi_write( |
| 4225 | struct xfs_trans *tp, /* transaction pointer */ |
| 4226 | struct xfs_inode *ip, /* incore inode */ |
| 4227 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4228 | xfs_filblks_t len, /* length to map in file */ |
| 4229 | int flags, /* XFS_BMAPI_... */ |
| 4230 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4231 | controls a.g. for allocs */ |
| 4232 | xfs_extlen_t total, /* total blocks needed */ |
| 4233 | struct xfs_bmbt_irec *mval, /* output: map values */ |
Brian Foster | 6e702a5 | 2018-07-11 22:26:12 -0700 | [diff] [blame] | 4234 | int *nmap) /* i/o: mval size/count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4236 | struct xfs_mount *mp = ip->i_mount; |
| 4237 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4238 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4239 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4240 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4241 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4242 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4243 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4244 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4247 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4248 | int orig_flags; /* original flags arg value */ |
| 4249 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4250 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4251 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | |
| 4253 | orig_bno = bno; |
| 4254 | orig_len = len; |
| 4255 | orig_flags = flags; |
| 4256 | orig_mval = mval; |
| 4257 | orig_nmap = *nmap; |
| 4258 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4259 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4262 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4263 | ASSERT(tp != NULL || |
| 4264 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4265 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4266 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4267 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4268 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4269 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4270 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4271 | /* zeroing is for currently only for data extents, not metadata */ |
| 4272 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4273 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4274 | /* |
| 4275 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4276 | * but it makes no sense to do both at once. This would result in |
| 4277 | * zeroing the unwritten extent twice, but it still being an |
| 4278 | * unwritten extent.... |
| 4279 | */ |
| 4280 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4281 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | if (unlikely(XFS_TEST_ERROR( |
| 4284 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4285 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4286 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4287 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4288 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4292 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4295 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4296 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4297 | |
Brian Foster | 3ae2d89 | 2018-07-11 22:26:19 -0700 | [diff] [blame] | 4298 | if (!tp || *firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4300 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4302 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4303 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4304 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4308 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4309 | if (error) |
| 4310 | goto error0; |
| 4311 | } |
| 4312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | n = 0; |
| 4314 | end = bno + len; |
| 4315 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4316 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4317 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4318 | eof = true; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4319 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4320 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4321 | bma.tp = tp; |
| 4322 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4323 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4324 | bma.datatype = 0; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4325 | bma.firstblock = firstblock; |
Brian Foster | 3e3673e | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 4326 | ASSERT(!tp || tp->t_dfops); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4329 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4330 | |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4331 | /* in hole or beyond EOF? */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4332 | if (eof || bma.got.br_startoff > bno) { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4333 | /* |
| 4334 | * CoW fork conversions should /never/ hit EOF or |
| 4335 | * holes. There should always be something for us |
| 4336 | * to work on. |
| 4337 | */ |
| 4338 | ASSERT(!((flags & XFS_BMAPI_CONVERT) && |
| 4339 | (flags & XFS_BMAPI_COWFORK))); |
| 4340 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4341 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4342 | /* |
| 4343 | * For the COW fork we can reasonably get a |
| 4344 | * request for converting an extent that races |
| 4345 | * with other threads already having converted |
| 4346 | * part of it, as there converting COW to |
| 4347 | * regular blocks is not protected using the |
| 4348 | * IOLOCK. |
| 4349 | */ |
| 4350 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4351 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4352 | error = -EIO; |
| 4353 | goto error0; |
| 4354 | } |
| 4355 | |
| 4356 | if (eof || bno >= end) |
| 4357 | break; |
| 4358 | } else { |
| 4359 | need_alloc = true; |
| 4360 | } |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4361 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4362 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4363 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4364 | |
| 4365 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | * First, deal with the hole before the allocated space |
| 4367 | * that we found, if any. |
| 4368 | */ |
Christoph Hellwig | b121459 | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 4369 | if ((need_alloc || wasdelay) && |
| 4370 | !(flags & XFS_BMAPI_CONVERT_ONLY)) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4371 | bma.eof = eof; |
| 4372 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4373 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4374 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4375 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4376 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4377 | /* |
| 4378 | * There's a 32/64 bit type mismatch between the |
| 4379 | * allocation length request (which can be 64 bits in |
| 4380 | * length) and the bma length request, which is |
| 4381 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4382 | * check for 32-bit overflows and handle them here. |
| 4383 | */ |
| 4384 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4385 | bma.length = MAXEXTLEN; |
| 4386 | else |
| 4387 | bma.length = len; |
| 4388 | |
| 4389 | ASSERT(len > 0); |
| 4390 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4391 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | if (error) |
| 4393 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4394 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4395 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4396 | |
| 4397 | /* |
| 4398 | * If this is a CoW allocation, record the data in |
| 4399 | * the refcount btree for orphan recovery. |
| 4400 | */ |
| 4401 | if (whichfork == XFS_COW_FORK) { |
Brian Foster | 6e702a5 | 2018-07-11 22:26:12 -0700 | [diff] [blame] | 4402 | error = xfs_refcount_alloc_cow_extent(mp, |
| 4403 | tp->t_dfops, bma.blkno, |
| 4404 | bma.length); |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4405 | if (error) |
| 4406 | goto error0; |
| 4407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4409 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4410 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4411 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4412 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4414 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4415 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4416 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4417 | continue; |
| 4418 | if (error) |
| 4419 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4421 | /* update the extent map to return */ |
| 4422 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | /* |
| 4425 | * If we're done, stop now. Stop when we've allocated |
| 4426 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4427 | * the transaction may get too big. |
| 4428 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4429 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4431 | |
| 4432 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4433 | bma.prev = bma.got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4434 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4435 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | /* |
| 4440 | * Transform from btree to extents, give it cur. |
| 4441 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4442 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4443 | int tmp_logflags = 0; |
| 4444 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4445 | ASSERT(bma.cur); |
| 4446 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4448 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | if (error) |
| 4450 | goto error0; |
| 4451 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4454 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4455 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | error0: |
| 4458 | /* |
| 4459 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4460 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4462 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4464 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4465 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4467 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | /* |
| 4469 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4470 | * detecting a case where the data is changed, there's an error, |
| 4471 | * and it's not logged so we don't shutdown when we should. |
| 4472 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4473 | if (bma.logflags) |
| 4474 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4475 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4476 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | if (!error) { |
| 4478 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4479 | XFS_FSB_TO_AGNO(mp, *firstblock) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | XFS_FSB_TO_AGNO(mp, |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4481 | bma.cur->bc_private.b.firstblock)); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4482 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4484 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4486 | } |
| 4487 | if (!error) |
| 4488 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4489 | orig_nmap, *nmap); |
| 4490 | return error; |
| 4491 | } |
| 4492 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4493 | int |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4494 | xfs_bmapi_remap( |
| 4495 | struct xfs_trans *tp, |
| 4496 | struct xfs_inode *ip, |
| 4497 | xfs_fileoff_t bno, |
| 4498 | xfs_filblks_t len, |
| 4499 | xfs_fsblock_t startblock, |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4500 | int flags) |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4501 | { |
| 4502 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4503 | struct xfs_ifork *ifp; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4504 | struct xfs_btree_cur *cur = NULL; |
| 4505 | xfs_fsblock_t firstblock = NULLFSBLOCK; |
| 4506 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4507 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4508 | int whichfork = xfs_bmapi_whichfork(flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4509 | int logflags = 0, error; |
| 4510 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4511 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4512 | ASSERT(len > 0); |
| 4513 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4514 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4515 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC | |
| 4516 | XFS_BMAPI_NORMAP))); |
| 4517 | ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) != |
| 4518 | (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4519 | |
| 4520 | if (unlikely(XFS_TEST_ERROR( |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4521 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4522 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4523 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4524 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4525 | return -EFSCORRUPTED; |
| 4526 | } |
| 4527 | |
| 4528 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4529 | return -EIO; |
| 4530 | |
| 4531 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4532 | error = xfs_iread_extents(tp, ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4533 | if (error) |
| 4534 | return error; |
| 4535 | } |
| 4536 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4537 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4538 | /* make sure we only reflink into a hole. */ |
| 4539 | ASSERT(got.br_startoff > bno); |
| 4540 | ASSERT(got.br_startoff - bno >= len); |
| 4541 | } |
| 4542 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4543 | ip->i_d.di_nblocks += len; |
| 4544 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4545 | |
| 4546 | if (ifp->if_flags & XFS_IFBROOT) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4547 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4548 | cur->bc_private.b.firstblock = firstblock; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4549 | cur->bc_private.b.flags = 0; |
| 4550 | } |
| 4551 | |
| 4552 | got.br_startoff = bno; |
| 4553 | got.br_startblock = startblock; |
| 4554 | got.br_blockcount = len; |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4555 | if (flags & XFS_BMAPI_PREALLOC) |
| 4556 | got.br_state = XFS_EXT_UNWRITTEN; |
| 4557 | else |
| 4558 | got.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4559 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4560 | error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur, |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 4561 | &cur, &got, &firstblock, &logflags, flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4562 | if (error) |
| 4563 | goto error0; |
| 4564 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4565 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4566 | int tmp_logflags = 0; |
| 4567 | |
| 4568 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4569 | &tmp_logflags, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4570 | logflags |= tmp_logflags; |
| 4571 | } |
| 4572 | |
| 4573 | error0: |
| 4574 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4575 | logflags &= ~XFS_ILOG_DEXT; |
| 4576 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4577 | logflags &= ~XFS_ILOG_DBROOT; |
| 4578 | |
| 4579 | if (logflags) |
| 4580 | xfs_trans_log_inode(tp, ip, logflags); |
| 4581 | if (cur) { |
| 4582 | xfs_btree_del_cursor(cur, |
| 4583 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4584 | } |
| 4585 | return error; |
| 4586 | } |
| 4587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4589 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4590 | * indlen reservation must be shared across the two new extents that are left |
| 4591 | * behind. |
| 4592 | * |
| 4593 | * Given the original reservation and the worst case indlen for the two new |
| 4594 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4595 | * reservation fairly across the two new extents. If necessary, steal available |
| 4596 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4597 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4598 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4599 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4600 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4601 | xfs_bmap_split_indlen( |
| 4602 | xfs_filblks_t ores, /* original res. */ |
| 4603 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4604 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4605 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4606 | { |
| 4607 | xfs_filblks_t len1 = *indlen1; |
| 4608 | xfs_filblks_t len2 = *indlen2; |
| 4609 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4610 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4611 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4612 | |
| 4613 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4614 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4615 | * indlen for both new extents. |
| 4616 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4617 | if (ores < nres && avail) |
| 4618 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4619 | ores += stolen; |
| 4620 | |
| 4621 | /* nothing else to do if we've satisfied the new reservation */ |
| 4622 | if (ores >= nres) |
| 4623 | return stolen; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4624 | |
| 4625 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4626 | * We can't meet the total required reservation for the two extents. |
| 4627 | * Calculate the percent of the overall shortage between both extents |
| 4628 | * and apply this percentage to each of the requested indlen values. |
| 4629 | * This distributes the shortage fairly and reduces the chances that one |
| 4630 | * of the two extents is left with nothing when extents are repeatedly |
| 4631 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4632 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4633 | resfactor = (ores * 100); |
| 4634 | do_div(resfactor, nres); |
| 4635 | len1 *= resfactor; |
| 4636 | do_div(len1, 100); |
| 4637 | len2 *= resfactor; |
| 4638 | do_div(len2, 100); |
| 4639 | ASSERT(len1 + len2 <= ores); |
| 4640 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4641 | |
| 4642 | /* |
| 4643 | * Hand out the remainder to each extent. If one of the two reservations |
| 4644 | * is zero, we want to make sure that one gets a block first. The loop |
| 4645 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4646 | * is zero. |
| 4647 | */ |
| 4648 | ores -= (len1 + len2); |
| 4649 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4650 | if (ores && !len2 && *indlen2) { |
| 4651 | len2++; |
| 4652 | ores--; |
| 4653 | } |
| 4654 | while (ores) { |
| 4655 | if (len1 < *indlen1) { |
| 4656 | len1++; |
| 4657 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4658 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4659 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4660 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4661 | if (len2 < *indlen2) { |
| 4662 | len2++; |
| 4663 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4664 | } |
| 4665 | } |
| 4666 | |
| 4667 | *indlen1 = len1; |
| 4668 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4669 | |
| 4670 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4671 | } |
| 4672 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4673 | int |
| 4674 | xfs_bmap_del_extent_delay( |
| 4675 | struct xfs_inode *ip, |
| 4676 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4677 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4678 | struct xfs_bmbt_irec *got, |
| 4679 | struct xfs_bmbt_irec *del) |
| 4680 | { |
| 4681 | struct xfs_mount *mp = ip->i_mount; |
| 4682 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4683 | struct xfs_bmbt_irec new; |
| 4684 | int64_t da_old, da_new, da_diff = 0; |
| 4685 | xfs_fileoff_t del_endoff, got_endoff; |
| 4686 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4687 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4688 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4689 | bool isrt; |
| 4690 | |
| 4691 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4692 | |
| 4693 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4694 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4695 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4696 | da_old = startblockval(got->br_startblock); |
| 4697 | da_new = 0; |
| 4698 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4699 | ASSERT(del->br_blockcount > 0); |
| 4700 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4701 | ASSERT(got_endoff >= del_endoff); |
| 4702 | |
| 4703 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4704 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4705 | |
| 4706 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4707 | xfs_mod_frextents(mp, rtexts); |
| 4708 | } |
| 4709 | |
| 4710 | /* |
| 4711 | * Update the inode delalloc counter now and wait to update the |
| 4712 | * sb counters as we might have to borrow some blocks for the |
| 4713 | * indirect block accounting. |
| 4714 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4715 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4716 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4717 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4718 | if (error) |
| 4719 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4720 | ip->i_delayed_blks -= del->br_blockcount; |
| 4721 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4722 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4723 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4724 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4725 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4726 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4727 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4728 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4729 | /* |
| 4730 | * Matches the whole extent. Delete the entry. |
| 4731 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4732 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4733 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4734 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4735 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4736 | /* |
| 4737 | * Deleting the first part of the extent. |
| 4738 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4739 | got->br_startoff = del_endoff; |
| 4740 | got->br_blockcount -= del->br_blockcount; |
| 4741 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4742 | got->br_blockcount), da_old); |
| 4743 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4744 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4745 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4746 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4747 | /* |
| 4748 | * Deleting the last part of the extent. |
| 4749 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4750 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4751 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4752 | got->br_blockcount), da_old); |
| 4753 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4754 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4755 | break; |
| 4756 | case 0: |
| 4757 | /* |
| 4758 | * Deleting the middle of the extent. |
| 4759 | * |
| 4760 | * Distribute the original indlen reservation across the two new |
| 4761 | * extents. Steal blocks from the deleted extent if necessary. |
| 4762 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4763 | * Warn if either of the new indlen reservations is zero as this |
| 4764 | * can lead to delalloc problems. |
| 4765 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4766 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4767 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4768 | |
| 4769 | new.br_blockcount = got_endoff - del_endoff; |
| 4770 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4771 | |
| 4772 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4773 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4774 | del->br_blockcount); |
| 4775 | |
| 4776 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4777 | |
| 4778 | new.br_startoff = del_endoff; |
| 4779 | new.br_state = got->br_state; |
| 4780 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4781 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4782 | xfs_iext_update_extent(ip, state, icur, got); |
| 4783 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4784 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4785 | |
| 4786 | da_new = got_indlen + new_indlen - stolen; |
| 4787 | del->br_blockcount -= stolen; |
| 4788 | break; |
| 4789 | } |
| 4790 | |
| 4791 | ASSERT(da_old >= da_new); |
| 4792 | da_diff = da_old - da_new; |
| 4793 | if (!isrt) |
| 4794 | da_diff += del->br_blockcount; |
| 4795 | if (da_diff) |
| 4796 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4797 | return error; |
| 4798 | } |
| 4799 | |
| 4800 | void |
| 4801 | xfs_bmap_del_extent_cow( |
| 4802 | struct xfs_inode *ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4803 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4804 | struct xfs_bmbt_irec *got, |
| 4805 | struct xfs_bmbt_irec *del) |
| 4806 | { |
| 4807 | struct xfs_mount *mp = ip->i_mount; |
| 4808 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4809 | struct xfs_bmbt_irec new; |
| 4810 | xfs_fileoff_t del_endoff, got_endoff; |
| 4811 | int state = BMAP_COWFORK; |
| 4812 | |
| 4813 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4814 | |
| 4815 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4816 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4817 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4818 | ASSERT(del->br_blockcount > 0); |
| 4819 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4820 | ASSERT(got_endoff >= del_endoff); |
| 4821 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4822 | |
| 4823 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4824 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4825 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4826 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4827 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4828 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4829 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4830 | /* |
| 4831 | * Matches the whole extent. Delete the entry. |
| 4832 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4833 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4834 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4835 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4836 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4837 | /* |
| 4838 | * Deleting the first part of the extent. |
| 4839 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4840 | got->br_startoff = del_endoff; |
| 4841 | got->br_blockcount -= del->br_blockcount; |
| 4842 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4843 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4844 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4845 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4846 | /* |
| 4847 | * Deleting the last part of the extent. |
| 4848 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4849 | got->br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4850 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4851 | break; |
| 4852 | case 0: |
| 4853 | /* |
| 4854 | * Deleting the middle of the extent. |
| 4855 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4856 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4857 | |
| 4858 | new.br_startoff = del_endoff; |
| 4859 | new.br_blockcount = got_endoff - del_endoff; |
| 4860 | new.br_state = got->br_state; |
| 4861 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4862 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4863 | xfs_iext_update_extent(ip, state, icur, got); |
| 4864 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4865 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4866 | break; |
| 4867 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 4868 | ip->i_delayed_blks -= del->br_blockcount; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4869 | } |
| 4870 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4871 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4872 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4873 | * after removing space. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4874 | */ |
| 4875 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4876 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4877 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4878 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4879 | struct xfs_iext_cursor *icur, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4880 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4881 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4882 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4883 | int whichfork, /* data or attr fork */ |
| 4884 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4885 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4886 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4887 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4888 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4889 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4890 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4891 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4892 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4893 | int i; /* temp state */ |
| 4894 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4895 | xfs_mount_t *mp; /* mount structure */ |
| 4896 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4897 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4898 | /* REFERENCED */ |
| 4899 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4900 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4901 | struct xfs_bmbt_irec old; |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 4902 | struct xfs_defer_ops *dfops = tp ? tp->t_dfops : NULL; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4903 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4904 | mp = ip->i_mount; |
| 4905 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4906 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4907 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4908 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4909 | xfs_iext_get_extent(ifp, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4910 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4911 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4912 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4913 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4914 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4915 | qfield = 0; |
| 4916 | error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4917 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4918 | /* |
| 4919 | * If it's the case where the directory code is running with no block |
| 4920 | * reservation, and the deleted block is in the middle of its extent, |
| 4921 | * and the resulting insert of an extent would cause transformation to |
| 4922 | * btree format, then reject it. The calling code will then swap blocks |
| 4923 | * around instead. We have to do this now, rather than waiting for the |
| 4924 | * conversion to btree format, since the transaction will be dirty then. |
| 4925 | */ |
| 4926 | if (tp->t_blk_res == 0 && |
| 4927 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 4928 | XFS_IFORK_NEXTENTS(ip, whichfork) >= |
| 4929 | XFS_IFORK_MAXEXT(ip, whichfork) && |
| 4930 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 4931 | return -ENOSPC; |
| 4932 | |
| 4933 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4934 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4935 | xfs_fsblock_t bno; |
| 4936 | xfs_filblks_t len; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 4937 | xfs_extlen_t mod; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4938 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 4939 | bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize, |
| 4940 | &mod); |
| 4941 | ASSERT(mod == 0); |
| 4942 | len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize, |
| 4943 | &mod); |
| 4944 | ASSERT(mod == 0); |
| 4945 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4946 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4947 | if (error) |
| 4948 | goto done; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4949 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4950 | nblks = len * mp->m_sb.sb_rextsize; |
| 4951 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4952 | } else { |
| 4953 | do_fx = 1; |
| 4954 | nblks = del->br_blockcount; |
| 4955 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4956 | } |
| 4957 | |
| 4958 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4959 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4960 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4961 | if (error) |
| 4962 | goto done; |
| 4963 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4964 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 4965 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4966 | if (got.br_startoff == del->br_startoff) |
| 4967 | state |= BMAP_LEFT_FILLING; |
| 4968 | if (got_endoff == del_endoff) |
| 4969 | state |= BMAP_RIGHT_FILLING; |
| 4970 | |
| 4971 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4972 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4973 | /* |
| 4974 | * Matches the whole extent. Delete the entry. |
| 4975 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4976 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4977 | xfs_iext_prev(ifp, icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4978 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4979 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 4980 | flags |= XFS_ILOG_CORE; |
| 4981 | if (!cur) { |
| 4982 | flags |= xfs_ilog_fext(whichfork); |
| 4983 | break; |
| 4984 | } |
| 4985 | if ((error = xfs_btree_delete(cur, &i))) |
| 4986 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4987 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4988 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4989 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4990 | /* |
| 4991 | * Deleting the first part of the extent. |
| 4992 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4993 | got.br_startoff = del_endoff; |
| 4994 | got.br_startblock = del_endblock; |
| 4995 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4996 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4997 | if (!cur) { |
| 4998 | flags |= xfs_ilog_fext(whichfork); |
| 4999 | break; |
| 5000 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5001 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5002 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5003 | goto done; |
| 5004 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5005 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5006 | /* |
| 5007 | * Deleting the last part of the extent. |
| 5008 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5009 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5010 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5011 | if (!cur) { |
| 5012 | flags |= xfs_ilog_fext(whichfork); |
| 5013 | break; |
| 5014 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5015 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5016 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5017 | goto done; |
| 5018 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5019 | case 0: |
| 5020 | /* |
| 5021 | * Deleting the middle of the extent. |
| 5022 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5023 | old = got; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5024 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5025 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5026 | xfs_iext_update_extent(ip, state, icur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5027 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5028 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5029 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5030 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5031 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5032 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5033 | flags |= XFS_ILOG_CORE; |
| 5034 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5035 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5036 | if (error) |
| 5037 | goto done; |
| 5038 | error = xfs_btree_increment(cur, 0, &i); |
| 5039 | if (error) |
| 5040 | goto done; |
| 5041 | cur->bc_rec.b = new; |
| 5042 | error = xfs_btree_insert(cur, &i); |
| 5043 | if (error && error != -ENOSPC) |
| 5044 | goto done; |
| 5045 | /* |
| 5046 | * If get no-space back from btree insert, it tried a |
| 5047 | * split, and we have a zero block reservation. Fix up |
| 5048 | * our state and return the error. |
| 5049 | */ |
| 5050 | if (error == -ENOSPC) { |
| 5051 | /* |
| 5052 | * Reset the cursor, don't trust it after any |
| 5053 | * insert operation. |
| 5054 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5055 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5056 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5057 | goto done; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5058 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5059 | /* |
| 5060 | * Update the btree record back |
| 5061 | * to the original value. |
| 5062 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5063 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5064 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5065 | goto done; |
| 5066 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5067 | * Reset the extent record back |
| 5068 | * to the original value. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5069 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5070 | xfs_iext_update_extent(ip, state, icur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5071 | flags = 0; |
| 5072 | error = -ENOSPC; |
| 5073 | goto done; |
| 5074 | } |
| 5075 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5076 | } else |
| 5077 | flags |= xfs_ilog_fext(whichfork); |
| 5078 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5079 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5080 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5081 | xfs_iext_insert(ip, icur, &new, state); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5082 | break; |
| 5083 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5084 | |
| 5085 | /* remove reverse mapping */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5086 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5087 | if (error) |
| 5088 | goto done; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5089 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5090 | /* |
| 5091 | * If we need to, add to list of extents to delete. |
| 5092 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5093 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5094 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5095 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5096 | if (error) |
| 5097 | goto done; |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5098 | } else { |
Brian Foster | 4e52933 | 2018-05-10 09:35:42 -0700 | [diff] [blame] | 5099 | __xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5100 | del->br_blockcount, NULL, |
| 5101 | (bflags & XFS_BMAPI_NODISCARD) || |
| 5102 | del->br_state == XFS_EXT_UNWRITTEN); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5103 | } |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5104 | } |
| 5105 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5106 | /* |
| 5107 | * Adjust inode # blocks in the file. |
| 5108 | */ |
| 5109 | if (nblks) |
| 5110 | ip->i_d.di_nblocks -= nblks; |
| 5111 | /* |
| 5112 | * Adjust quota data. |
| 5113 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5114 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5115 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5116 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5117 | done: |
| 5118 | *logflagsp = flags; |
| 5119 | return error; |
| 5120 | } |
| 5121 | |
| 5122 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | * Unmap (remove) blocks from a file. |
| 5124 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5125 | * that value. If not all extents in the block range can be removed then |
| 5126 | * *done is set. |
| 5127 | */ |
| 5128 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5129 | __xfs_bunmapi( |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5130 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5132 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5133 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5134 | int flags, /* misc flags */ |
| 5135 | xfs_extnum_t nexts, /* number of extents max */ |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5136 | xfs_fsblock_t *firstblock) /* first allocated block |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | controls a.g. for allocs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | { |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5139 | struct xfs_btree_cur *cur; /* bmap btree cursor */ |
| 5140 | struct xfs_bmbt_irec del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | int error; /* error return value */ |
| 5142 | xfs_extnum_t extno; /* extent number in list */ |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5143 | struct xfs_bmbt_irec got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5145 | int isrt; /* freeing in rt area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | int logflags; /* transaction logging flags */ |
| 5147 | xfs_extlen_t mod; /* rt extent offset */ |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5148 | struct xfs_mount *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | int tmp_logflags; /* partial logging flags */ |
| 5150 | int wasdel; /* was a delayed alloc extent */ |
| 5151 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5153 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5154 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5155 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5156 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5157 | struct xfs_iext_cursor icur; |
| 5158 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5160 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5161 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5162 | whichfork = xfs_bmapi_whichfork(flags); |
| 5163 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5165 | if (unlikely( |
| 5166 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5167 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5168 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5169 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5170 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | } |
| 5172 | mp = ip->i_mount; |
| 5173 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5174 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5175 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5176 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | ASSERT(len > 0); |
| 5178 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5179 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5180 | /* |
| 5181 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5182 | * blowing out the transaction with a mix of EFIs and reflink |
| 5183 | * adjustments. |
| 5184 | */ |
Darrick J. Wong | 8c57b88 | 2017-12-10 18:03:53 -0800 | [diff] [blame] | 5185 | if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5186 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5187 | else |
| 5188 | max_len = len; |
| 5189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5191 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5192 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5193 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5194 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | return 0; |
| 5196 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5197 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5198 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5199 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5200 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5201 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5202 | *rlen = 0; |
| 5203 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5205 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | logflags = 0; |
| 5208 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5209 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5210 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | cur->bc_private.b.firstblock = *firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | cur->bc_private.b.flags = 0; |
| 5213 | } else |
| 5214 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5215 | |
| 5216 | if (isrt) { |
| 5217 | /* |
| 5218 | * Synchronize by locking the bitmap inode. |
| 5219 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5220 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5221 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5222 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5223 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | extno = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5227 | while (end != (xfs_fileoff_t)-1 && end >= start && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5228 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5230 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | * Just back up to the previous extent, if so. |
| 5232 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5233 | if (got.br_startoff > end && |
| 5234 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5235 | done = true; |
| 5236 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | } |
| 5238 | /* |
| 5239 | * Is the last block of this extent before the range |
| 5240 | * we're supposed to delete? If so, we're done. |
| 5241 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5242 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5244 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | break; |
| 5246 | /* |
| 5247 | * Then deal with the (possibly delayed) allocated space |
| 5248 | * we found. |
| 5249 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5251 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5252 | |
| 5253 | /* |
| 5254 | * Make sure we don't touch multiple AGF headers out of order |
| 5255 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5256 | */ |
| 5257 | if (!wasdel) { |
| 5258 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5259 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5260 | break; |
| 5261 | prev_agno = agno; |
| 5262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | if (got.br_startoff < start) { |
| 5264 | del.br_startoff = start; |
| 5265 | del.br_blockcount -= start - got.br_startoff; |
| 5266 | if (!wasdel) |
| 5267 | del.br_startblock += start - got.br_startoff; |
| 5268 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5269 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5270 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5271 | |
| 5272 | /* How much can we safely unmap? */ |
| 5273 | if (max_len < del.br_blockcount) { |
| 5274 | del.br_startoff += del.br_blockcount - max_len; |
| 5275 | if (!wasdel) |
| 5276 | del.br_startblock += del.br_blockcount - max_len; |
| 5277 | del.br_blockcount = max_len; |
| 5278 | } |
| 5279 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5280 | if (!isrt) |
| 5281 | goto delete; |
| 5282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | sum = del.br_startblock + del.br_blockcount; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5284 | div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod); |
| 5285 | if (mod) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | /* |
| 5287 | * Realtime extent not lined up at the end. |
| 5288 | * The extent could have been split into written |
| 5289 | * and unwritten pieces, or we could just be |
| 5290 | * unmapping part of it. But we can't really |
| 5291 | * get rid of part of a realtime extent. |
| 5292 | */ |
| 5293 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5294 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | /* |
| 5296 | * This piece is unwritten, or we're not |
| 5297 | * using unwritten extents. Skip over it. |
| 5298 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5299 | ASSERT(end >= mod); |
| 5300 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | del.br_blockcount : mod; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5302 | if (end < got.br_startoff && |
| 5303 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5304 | done = true; |
| 5305 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | } |
| 5307 | continue; |
| 5308 | } |
| 5309 | /* |
| 5310 | * It's written, turn it unwritten. |
| 5311 | * This is better than zeroing it. |
| 5312 | */ |
| 5313 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5314 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | /* |
| 5316 | * If this spans a realtime extent boundary, |
| 5317 | * chop it back to the start of the one we end at. |
| 5318 | */ |
| 5319 | if (del.br_blockcount > mod) { |
| 5320 | del.br_startoff += del.br_blockcount - mod; |
| 5321 | del.br_startblock += del.br_blockcount - mod; |
| 5322 | del.br_blockcount = mod; |
| 5323 | } |
| 5324 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5325 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5326 | whichfork, &icur, &cur, &del, |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 5327 | firstblock, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | if (error) |
| 5329 | goto error0; |
| 5330 | goto nodelete; |
| 5331 | } |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5332 | div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod); |
| 5333 | if (mod) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5334 | /* |
| 5335 | * Realtime extent is lined up at the end but not |
| 5336 | * at the front. We'll get rid of full extents if |
| 5337 | * we can. |
| 5338 | */ |
| 5339 | mod = mp->m_sb.sb_rextsize - mod; |
| 5340 | if (del.br_blockcount > mod) { |
| 5341 | del.br_blockcount -= mod; |
| 5342 | del.br_startoff += mod; |
| 5343 | del.br_startblock += mod; |
| 5344 | } else if ((del.br_startoff == start && |
| 5345 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5346 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5347 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | /* |
| 5349 | * Can't make it unwritten. There isn't |
| 5350 | * a full extent here so just skip it. |
| 5351 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5352 | ASSERT(end >= del.br_blockcount); |
| 5353 | end -= del.br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5354 | if (got.br_startoff > end && |
| 5355 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5356 | done = true; |
| 5357 | break; |
| 5358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | continue; |
| 5360 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5361 | struct xfs_bmbt_irec prev; |
| 5362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | /* |
| 5364 | * This one is already unwritten. |
| 5365 | * It must have a written left neighbor. |
| 5366 | * Unwrite the killed part of that one and |
| 5367 | * try again. |
| 5368 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5369 | if (!xfs_iext_prev_extent(ifp, &icur, &prev)) |
| 5370 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5372 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | ASSERT(del.br_startblock == |
| 5374 | prev.br_startblock + prev.br_blockcount); |
| 5375 | if (prev.br_startoff < start) { |
| 5376 | mod = start - prev.br_startoff; |
| 5377 | prev.br_blockcount -= mod; |
| 5378 | prev.br_startblock += mod; |
| 5379 | prev.br_startoff = start; |
| 5380 | } |
| 5381 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5382 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5383 | ip, whichfork, &icur, &cur, |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 5384 | &prev, firstblock, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | if (error) |
| 5386 | goto error0; |
| 5387 | goto nodelete; |
| 5388 | } else { |
| 5389 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5390 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5391 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5392 | ip, whichfork, &icur, &cur, |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 5393 | &del, firstblock, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | if (error) |
| 5395 | goto error0; |
| 5396 | goto nodelete; |
| 5397 | } |
| 5398 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5399 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5400 | delete: |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5401 | if (wasdel) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5402 | error = xfs_bmap_del_extent_delay(ip, whichfork, &icur, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5403 | &got, &del); |
| 5404 | } else { |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 5405 | error = xfs_bmap_del_extent_real(ip, tp, &icur, cur, |
| 5406 | &del, &tmp_logflags, whichfork, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5407 | flags); |
| 5408 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5409 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | if (error) |
| 5412 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5413 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5414 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5415 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | /* |
| 5418 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5420 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5421 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
| 5422 | (got.br_startoff > end && |
| 5423 | !xfs_iext_prev_extent(ifp, &icur, &got))) { |
| 5424 | done = true; |
| 5425 | break; |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | extno++; |
| 5428 | } |
| 5429 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5430 | if (done || end == (xfs_fileoff_t)-1 || end < start) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5431 | *rlen = 0; |
| 5432 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5433 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5435 | /* |
| 5436 | * Convert to a btree if necessary. |
| 5437 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5438 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | ASSERT(cur == NULL); |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 5440 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, &cur, 0, |
| 5441 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | logflags |= tmp_logflags; |
| 5443 | if (error) |
| 5444 | goto error0; |
| 5445 | } |
| 5446 | /* |
| 5447 | * transform from btree to extents, give it cur |
| 5448 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5449 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | ASSERT(cur != NULL); |
| 5451 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5452 | whichfork); |
| 5453 | logflags |= tmp_logflags; |
| 5454 | if (error) |
| 5455 | goto error0; |
| 5456 | } |
| 5457 | /* |
| 5458 | * transform from extents to local? |
| 5459 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | error = 0; |
| 5461 | error0: |
| 5462 | /* |
| 5463 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5464 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5466 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5468 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5469 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5471 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | /* |
| 5473 | * Log inode even in the error case, if the transaction |
| 5474 | * is dirty we'll need to shut down the filesystem. |
| 5475 | */ |
| 5476 | if (logflags) |
| 5477 | xfs_trans_log_inode(tp, ip, logflags); |
| 5478 | if (cur) { |
| 5479 | if (!error) { |
| 5480 | *firstblock = cur->bc_private.b.firstblock; |
| 5481 | cur->bc_private.b.allocated = 0; |
| 5482 | } |
| 5483 | xfs_btree_del_cursor(cur, |
| 5484 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5485 | } |
| 5486 | return error; |
| 5487 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5488 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5489 | /* Unmap a range of a file. */ |
| 5490 | int |
| 5491 | xfs_bunmapi( |
| 5492 | xfs_trans_t *tp, |
| 5493 | struct xfs_inode *ip, |
| 5494 | xfs_fileoff_t bno, |
| 5495 | xfs_filblks_t len, |
| 5496 | int flags, |
| 5497 | xfs_extnum_t nexts, |
| 5498 | xfs_fsblock_t *firstblock, |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5499 | int *done) |
| 5500 | { |
| 5501 | int error; |
| 5502 | |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 5503 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock); |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5504 | *done = (len == 0); |
| 5505 | return error; |
| 5506 | } |
| 5507 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5508 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5509 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5510 | * extent that precedes the target hole of the shift. |
| 5511 | */ |
| 5512 | STATIC bool |
| 5513 | xfs_bmse_can_merge( |
| 5514 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5515 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5516 | xfs_fileoff_t shift) /* shift fsb */ |
| 5517 | { |
| 5518 | xfs_fileoff_t startoff; |
| 5519 | |
| 5520 | startoff = got->br_startoff - shift; |
| 5521 | |
| 5522 | /* |
| 5523 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5524 | * the preceding extent. |
| 5525 | */ |
| 5526 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5527 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5528 | (left->br_state != got->br_state) || |
| 5529 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5530 | return false; |
| 5531 | |
| 5532 | return true; |
| 5533 | } |
| 5534 | |
| 5535 | /* |
| 5536 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5537 | * hole in the file. If an extent shift would result in the extent being fully |
| 5538 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5539 | * the preceding extent rather than do the shift. |
| 5540 | * |
| 5541 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5542 | * with the provided extents via xfs_bmse_can_merge(). |
| 5543 | */ |
| 5544 | STATIC int |
| 5545 | xfs_bmse_merge( |
| 5546 | struct xfs_inode *ip, |
| 5547 | int whichfork, |
| 5548 | xfs_fileoff_t shift, /* shift fsb */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5549 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5550 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5551 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5552 | struct xfs_btree_cur *cur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5553 | int *logflags, /* output */ |
| 5554 | struct xfs_defer_ops *dfops) |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5555 | { |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5556 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5557 | xfs_filblks_t blockcount; |
| 5558 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5559 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5560 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5561 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5562 | |
| 5563 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5564 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5565 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5566 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5567 | new = *left; |
| 5568 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5569 | |
| 5570 | /* |
| 5571 | * Update the on-disk extent count, the btree if necessary and log the |
| 5572 | * inode. |
| 5573 | */ |
| 5574 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5575 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5576 | *logflags |= XFS_ILOG_CORE; |
| 5577 | if (!cur) { |
| 5578 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5579 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5583 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5584 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5585 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5586 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5587 | |
| 5588 | error = xfs_btree_delete(cur, &i); |
| 5589 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5590 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5591 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5592 | |
| 5593 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5594 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5595 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5596 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5597 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5598 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5599 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5600 | if (error) |
| 5601 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5602 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5603 | done: |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5604 | xfs_iext_remove(ip, icur, 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5605 | xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur); |
| 5606 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5607 | &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5608 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5609 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5610 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got); |
| 5611 | if (error) |
| 5612 | return error; |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5613 | memcpy(&new, got, sizeof(new)); |
| 5614 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5615 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5616 | } |
| 5617 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5618 | static int |
| 5619 | xfs_bmap_shift_update_extent( |
| 5620 | struct xfs_inode *ip, |
| 5621 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5622 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5623 | struct xfs_bmbt_irec *got, |
| 5624 | struct xfs_btree_cur *cur, |
| 5625 | int *logflags, |
| 5626 | struct xfs_defer_ops *dfops, |
| 5627 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5628 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5629 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5630 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5631 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5632 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5633 | *logflags |= XFS_ILOG_CORE; |
| 5634 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5635 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5636 | |
| 5637 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5638 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5639 | if (error) |
| 5640 | return error; |
| 5641 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
| 5642 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5643 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5644 | if (error) |
| 5645 | return error; |
| 5646 | } else { |
| 5647 | *logflags |= XFS_ILOG_DEXT; |
| 5648 | } |
| 5649 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5650 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5651 | got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5652 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5653 | /* update reverse mapping */ |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5654 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5655 | if (error) |
| 5656 | return error; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5657 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5658 | } |
| 5659 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5660 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5661 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5662 | struct xfs_trans *tp, |
| 5663 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5664 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5665 | xfs_fileoff_t offset_shift_fsb, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5666 | bool *done, |
Brian Foster | f4a9cf97 | 2018-07-11 22:26:15 -0700 | [diff] [blame] | 5667 | xfs_fsblock_t *firstblock) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5668 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5669 | int whichfork = XFS_DATA_FORK; |
| 5670 | struct xfs_mount *mp = ip->i_mount; |
| 5671 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5672 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5673 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5674 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5675 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5676 | int error = 0; |
| 5677 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5678 | |
| 5679 | if (unlikely(XFS_TEST_ERROR( |
| 5680 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5681 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5682 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5683 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5684 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5688 | return -EIO; |
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 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5691 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5692 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5693 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5694 | if (error) |
| 5695 | return error; |
| 5696 | } |
| 5697 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5698 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5699 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5700 | cur->bc_private.b.firstblock = *firstblock; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5701 | cur->bc_private.b.flags = 0; |
| 5702 | } |
| 5703 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5704 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5705 | *done = true; |
| 5706 | goto del_cursor; |
| 5707 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5708 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5709 | del_cursor); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5710 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5711 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5712 | if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5713 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5714 | error = -EINVAL; |
| 5715 | goto del_cursor; |
| 5716 | } |
| 5717 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5718 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
| 5719 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5720 | &icur, &got, &prev, cur, &logflags, |
Brian Foster | f4a9cf97 | 2018-07-11 22:26:15 -0700 | [diff] [blame] | 5721 | tp->t_dfops); |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5722 | if (error) |
| 5723 | goto del_cursor; |
| 5724 | goto done; |
| 5725 | } |
| 5726 | } else { |
| 5727 | if (got.br_startoff < offset_shift_fsb) { |
| 5728 | error = -EINVAL; |
| 5729 | goto del_cursor; |
| 5730 | } |
| 5731 | } |
| 5732 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5733 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
Brian Foster | f4a9cf97 | 2018-07-11 22:26:15 -0700 | [diff] [blame] | 5734 | &logflags, tp->t_dfops, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5735 | if (error) |
| 5736 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5737 | |
Christoph Hellwig | 4263036 | 2017-11-03 10:34:41 -0700 | [diff] [blame] | 5738 | done: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5739 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
| 5740 | *done = true; |
| 5741 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5742 | } |
| 5743 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5744 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5745 | del_cursor: |
| 5746 | if (cur) |
| 5747 | xfs_btree_del_cursor(cur, |
| 5748 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5749 | if (logflags) |
| 5750 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5751 | return error; |
| 5752 | } |
| 5753 | |
Darrick J. Wong | f62cb48 | 2018-06-21 23:26:57 -0700 | [diff] [blame] | 5754 | /* Make sure we won't be right-shifting an extent past the maximum bound. */ |
| 5755 | int |
| 5756 | xfs_bmap_can_insert_extents( |
| 5757 | struct xfs_inode *ip, |
| 5758 | xfs_fileoff_t off, |
| 5759 | xfs_fileoff_t shift) |
| 5760 | { |
| 5761 | struct xfs_bmbt_irec got; |
| 5762 | int is_empty; |
| 5763 | int error = 0; |
| 5764 | |
| 5765 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5766 | |
| 5767 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 5768 | return -EIO; |
| 5769 | |
| 5770 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 5771 | error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty); |
| 5772 | if (!error && !is_empty && got.br_startoff >= off && |
| 5773 | ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff) |
| 5774 | error = -EINVAL; |
| 5775 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 5776 | |
| 5777 | return error; |
| 5778 | } |
| 5779 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5780 | int |
| 5781 | xfs_bmap_insert_extents( |
| 5782 | struct xfs_trans *tp, |
| 5783 | struct xfs_inode *ip, |
| 5784 | xfs_fileoff_t *next_fsb, |
| 5785 | xfs_fileoff_t offset_shift_fsb, |
| 5786 | bool *done, |
| 5787 | xfs_fileoff_t stop_fsb, |
Brian Foster | f4a9cf97 | 2018-07-11 22:26:15 -0700 | [diff] [blame] | 5788 | xfs_fsblock_t *firstblock) |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5789 | { |
| 5790 | int whichfork = XFS_DATA_FORK; |
| 5791 | struct xfs_mount *mp = ip->i_mount; |
| 5792 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5793 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5794 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5795 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5796 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5797 | int error = 0; |
| 5798 | int logflags = 0; |
| 5799 | |
| 5800 | if (unlikely(XFS_TEST_ERROR( |
| 5801 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5802 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5803 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
| 5804 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 5805 | return -EFSCORRUPTED; |
| 5806 | } |
| 5807 | |
| 5808 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5809 | return -EIO; |
| 5810 | |
| 5811 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5812 | |
| 5813 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5814 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5815 | if (error) |
| 5816 | return error; |
| 5817 | } |
| 5818 | |
| 5819 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5820 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5821 | cur->bc_private.b.firstblock = *firstblock; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5822 | cur->bc_private.b.flags = 0; |
| 5823 | } |
| 5824 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5825 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5826 | xfs_iext_last(ifp, &icur); |
| 5827 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5828 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5829 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5830 | goto del_cursor; |
| 5831 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5832 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5833 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5834 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5835 | goto del_cursor; |
| 5836 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5837 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5838 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5839 | del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5840 | |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5841 | if (stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5842 | error = -EIO; |
| 5843 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5844 | } |
| 5845 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5846 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5847 | if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5848 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5849 | error = -EINVAL; |
| 5850 | goto del_cursor; |
| 5851 | } |
| 5852 | |
| 5853 | /* |
| 5854 | * Unlike a left shift (which involves a hole punch), a right |
| 5855 | * shift does not modify extent neighbors in any way. We should |
| 5856 | * never find mergeable extents in this scenario. Check anyways |
| 5857 | * and warn if we encounter two extents that could be one. |
| 5858 | */ |
| 5859 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5860 | WARN_ON_ONCE(1); |
| 5861 | } |
| 5862 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5863 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
Brian Foster | f4a9cf97 | 2018-07-11 22:26:15 -0700 | [diff] [blame] | 5864 | &logflags, tp->t_dfops, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5865 | if (error) |
| 5866 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5867 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5868 | if (!xfs_iext_prev_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5869 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5870 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5871 | goto del_cursor; |
| 5872 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5873 | |
| 5874 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5875 | del_cursor: |
| 5876 | if (cur) |
| 5877 | xfs_btree_del_cursor(cur, |
| 5878 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5879 | if (logflags) |
| 5880 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5881 | return error; |
| 5882 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5883 | |
| 5884 | /* |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5885 | * Splits an extent into two extents at split_fsb block such that it is the |
| 5886 | * first block of the current_ext. @ext is a target extent to be split. |
| 5887 | * @split_fsb is a block where the extents is split. If split_fsb lies in a |
| 5888 | * hole or the first block of extents, just return 0. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5889 | */ |
| 5890 | STATIC int |
| 5891 | xfs_bmap_split_extent_at( |
| 5892 | struct xfs_trans *tp, |
| 5893 | struct xfs_inode *ip, |
| 5894 | xfs_fileoff_t split_fsb, |
Brian Foster | d76e6ce | 2018-07-11 22:26:10 -0700 | [diff] [blame] | 5895 | xfs_fsblock_t *firstfsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5896 | { |
| 5897 | int whichfork = XFS_DATA_FORK; |
| 5898 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5899 | struct xfs_bmbt_irec got; |
| 5900 | struct xfs_bmbt_irec new; /* split extent */ |
| 5901 | struct xfs_mount *mp = ip->i_mount; |
| 5902 | struct xfs_ifork *ifp; |
| 5903 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5904 | struct xfs_iext_cursor icur; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5905 | int error = 0; |
| 5906 | int logflags = 0; |
| 5907 | int i = 0; |
| 5908 | |
| 5909 | if (unlikely(XFS_TEST_ERROR( |
| 5910 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5911 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5912 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5913 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5914 | XFS_ERRLEVEL_LOW, mp); |
| 5915 | return -EFSCORRUPTED; |
| 5916 | } |
| 5917 | |
| 5918 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5919 | return -EIO; |
| 5920 | |
| 5921 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5922 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5923 | /* Read in all the extents */ |
| 5924 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5925 | if (error) |
| 5926 | return error; |
| 5927 | } |
| 5928 | |
| 5929 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5930 | * 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] | 5931 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5932 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5933 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5934 | return 0; |
| 5935 | |
| 5936 | gotblkcnt = split_fsb - got.br_startoff; |
| 5937 | new.br_startoff = split_fsb; |
| 5938 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5939 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5940 | new.br_state = got.br_state; |
| 5941 | |
| 5942 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5943 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5944 | cur->bc_private.b.firstblock = *firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5945 | cur->bc_private.b.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5946 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5947 | if (error) |
| 5948 | goto del_cursor; |
| 5949 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5950 | } |
| 5951 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5952 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5953 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, |
| 5954 | &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5955 | |
| 5956 | logflags = XFS_ILOG_CORE; |
| 5957 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5958 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5959 | if (error) |
| 5960 | goto del_cursor; |
| 5961 | } else |
| 5962 | logflags |= XFS_ILOG_DEXT; |
| 5963 | |
| 5964 | /* Add new extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5965 | xfs_iext_next(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5966 | xfs_iext_insert(ip, &icur, &new, 0); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5967 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5968 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5969 | |
| 5970 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5971 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5972 | if (error) |
| 5973 | goto del_cursor; |
| 5974 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5975 | error = xfs_btree_insert(cur, &i); |
| 5976 | if (error) |
| 5977 | goto del_cursor; |
| 5978 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5979 | } |
| 5980 | |
| 5981 | /* |
| 5982 | * Convert to a btree if necessary. |
| 5983 | */ |
| 5984 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 5985 | int tmp_logflags; /* partial log flag return val */ |
| 5986 | |
| 5987 | ASSERT(cur == NULL); |
Brian Foster | 81ba8f3 | 2018-07-11 22:26:16 -0700 | [diff] [blame] | 5988 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, &cur, 0, |
| 5989 | &tmp_logflags, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5990 | logflags |= tmp_logflags; |
| 5991 | } |
| 5992 | |
| 5993 | del_cursor: |
| 5994 | if (cur) { |
| 5995 | cur->bc_private.b.allocated = 0; |
| 5996 | xfs_btree_del_cursor(cur, |
| 5997 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5998 | } |
| 5999 | |
| 6000 | if (logflags) |
| 6001 | xfs_trans_log_inode(tp, ip, logflags); |
| 6002 | return error; |
| 6003 | } |
| 6004 | |
| 6005 | int |
| 6006 | xfs_bmap_split_extent( |
| 6007 | struct xfs_inode *ip, |
| 6008 | xfs_fileoff_t split_fsb) |
| 6009 | { |
| 6010 | struct xfs_mount *mp = ip->i_mount; |
| 6011 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6012 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6013 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6014 | int error; |
| 6015 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6016 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6017 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6018 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6019 | return error; |
Brian Foster | bcd2c9f | 2018-07-11 22:26:19 -0700 | [diff] [blame] | 6020 | xfs_defer_init(tp, &dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6021 | |
| 6022 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6023 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6024 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6025 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Brian Foster | d76e6ce | 2018-07-11 22:26:10 -0700 | [diff] [blame] | 6026 | &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6027 | if (error) |
| 6028 | goto out; |
| 6029 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 6030 | error = xfs_defer_finish(&tp, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6031 | if (error) |
| 6032 | goto out; |
| 6033 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6034 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6035 | |
| 6036 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6037 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6038 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6039 | return error; |
| 6040 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6041 | |
| 6042 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6043 | static bool |
| 6044 | xfs_bmap_is_update_needed( |
| 6045 | struct xfs_bmbt_irec *bmap) |
| 6046 | { |
| 6047 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6048 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6049 | } |
| 6050 | |
| 6051 | /* Record a bmap intent. */ |
| 6052 | static int |
| 6053 | __xfs_bmap_add( |
| 6054 | struct xfs_mount *mp, |
| 6055 | struct xfs_defer_ops *dfops, |
| 6056 | enum xfs_bmap_intent_type type, |
| 6057 | struct xfs_inode *ip, |
| 6058 | int whichfork, |
| 6059 | struct xfs_bmbt_irec *bmap) |
| 6060 | { |
| 6061 | int error; |
| 6062 | struct xfs_bmap_intent *bi; |
| 6063 | |
| 6064 | trace_xfs_bmap_defer(mp, |
| 6065 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6066 | type, |
| 6067 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6068 | ip->i_ino, whichfork, |
| 6069 | bmap->br_startoff, |
| 6070 | bmap->br_blockcount, |
| 6071 | bmap->br_state); |
| 6072 | |
| 6073 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6074 | INIT_LIST_HEAD(&bi->bi_list); |
| 6075 | bi->bi_type = type; |
| 6076 | bi->bi_owner = ip; |
| 6077 | bi->bi_whichfork = whichfork; |
| 6078 | bi->bi_bmap = *bmap; |
| 6079 | |
Christoph Hellwig | 882d878 | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 6080 | error = xfs_defer_ijoin(dfops, bi->bi_owner); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6081 | if (error) { |
| 6082 | kmem_free(bi); |
| 6083 | return error; |
| 6084 | } |
| 6085 | |
| 6086 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6087 | return 0; |
| 6088 | } |
| 6089 | |
| 6090 | /* Map an extent into a file. */ |
| 6091 | int |
| 6092 | xfs_bmap_map_extent( |
| 6093 | struct xfs_mount *mp, |
| 6094 | struct xfs_defer_ops *dfops, |
| 6095 | struct xfs_inode *ip, |
| 6096 | struct xfs_bmbt_irec *PREV) |
| 6097 | { |
| 6098 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6099 | return 0; |
| 6100 | |
| 6101 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6102 | XFS_DATA_FORK, PREV); |
| 6103 | } |
| 6104 | |
| 6105 | /* Unmap an extent out of a file. */ |
| 6106 | int |
| 6107 | xfs_bmap_unmap_extent( |
| 6108 | struct xfs_mount *mp, |
| 6109 | struct xfs_defer_ops *dfops, |
| 6110 | struct xfs_inode *ip, |
| 6111 | struct xfs_bmbt_irec *PREV) |
| 6112 | { |
| 6113 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6114 | return 0; |
| 6115 | |
| 6116 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6117 | XFS_DATA_FORK, PREV); |
| 6118 | } |
| 6119 | |
| 6120 | /* |
| 6121 | * Process one of the deferred bmap operations. We pass back the |
| 6122 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6123 | */ |
| 6124 | int |
| 6125 | xfs_bmap_finish_one( |
| 6126 | struct xfs_trans *tp, |
| 6127 | struct xfs_defer_ops *dfops, |
| 6128 | struct xfs_inode *ip, |
| 6129 | enum xfs_bmap_intent_type type, |
| 6130 | int whichfork, |
| 6131 | xfs_fileoff_t startoff, |
| 6132 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6133 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6134 | xfs_exntst_t state) |
| 6135 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6136 | xfs_fsblock_t firstfsb; |
| 6137 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6138 | |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6139 | /* |
| 6140 | * firstfsb is tied to the transaction lifetime and is used to |
| 6141 | * ensure correct AG locking order and schedule work item |
| 6142 | * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us |
| 6143 | * to only making one bmap call per transaction, so it should |
| 6144 | * be safe to have it as a local variable here. |
| 6145 | */ |
| 6146 | firstfsb = NULLFSBLOCK; |
| 6147 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6148 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6149 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6150 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6151 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6152 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6153 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6154 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6155 | |
| 6156 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6157 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6158 | return -EIO; |
| 6159 | |
| 6160 | switch (type) { |
| 6161 | case XFS_BMAP_MAP: |
Brian Foster | ff3edf2 | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 6162 | ASSERT(dfops == tp->t_dfops); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6163 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Brian Foster | ff3edf2 | 2018-07-11 22:26:14 -0700 | [diff] [blame] | 6164 | startblock, 0); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6165 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6166 | break; |
| 6167 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6168 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 6169 | XFS_BMAPI_REMAP, 1, &firstfsb); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6170 | break; |
| 6171 | default: |
| 6172 | ASSERT(0); |
| 6173 | error = -EFSCORRUPTED; |
| 6174 | } |
| 6175 | |
| 6176 | return error; |
| 6177 | } |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6178 | |
| 6179 | /* Check that an inode's extent does not have invalid flags or bad ranges. */ |
| 6180 | xfs_failaddr_t |
| 6181 | xfs_bmap_validate_extent( |
| 6182 | struct xfs_inode *ip, |
| 6183 | int whichfork, |
| 6184 | struct xfs_bmbt_irec *irec) |
| 6185 | { |
| 6186 | struct xfs_mount *mp = ip->i_mount; |
| 6187 | xfs_fsblock_t endfsb; |
| 6188 | bool isrt; |
| 6189 | |
| 6190 | isrt = XFS_IS_REALTIME_INODE(ip); |
| 6191 | endfsb = irec->br_startblock + irec->br_blockcount - 1; |
| 6192 | if (isrt) { |
| 6193 | if (!xfs_verify_rtbno(mp, irec->br_startblock)) |
| 6194 | return __this_address; |
| 6195 | if (!xfs_verify_rtbno(mp, endfsb)) |
| 6196 | return __this_address; |
| 6197 | } else { |
| 6198 | if (!xfs_verify_fsbno(mp, irec->br_startblock)) |
| 6199 | return __this_address; |
| 6200 | if (!xfs_verify_fsbno(mp, endfsb)) |
| 6201 | return __this_address; |
| 6202 | if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) != |
| 6203 | XFS_FSB_TO_AGNO(mp, endfsb)) |
| 6204 | return __this_address; |
| 6205 | } |
| 6206 | if (irec->br_state != XFS_EXT_NORM) { |
| 6207 | if (whichfork != XFS_DATA_FORK) |
| 6208 | return __this_address; |
| 6209 | if (!xfs_sb_version_hasextflgbit(&mp->m_sb)) |
| 6210 | return __this_address; |
| 6211 | } |
| 6212 | return NULL; |
| 6213 | } |