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); |
| 627 | xfs_bmap_add_free(mp, cur->bc_private.b.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( |
| 647 | xfs_trans_t *tp, /* transaction pointer */ |
| 648 | xfs_inode_t *ip, /* incore inode pointer */ |
| 649 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 650 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 651 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 652 | int wasdel, /* converting a delayed alloc */ |
| 653 | int *logflagsp, /* inode logging flags */ |
| 654 | int whichfork) /* data or attr fork */ |
| 655 | { |
| 656 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 657 | xfs_buf_t *abp; /* buffer for ablock */ |
| 658 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 659 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 660 | struct xfs_btree_block *block; /* btree root block */ |
| 661 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 662 | int error; /* error return value */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 663 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 664 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 665 | xfs_mount_t *mp; /* mount structure */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 666 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 667 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 668 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 669 | xfs_extnum_t cnt = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 670 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 671 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 672 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 673 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 674 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 675 | |
| 676 | /* |
| 677 | * Make space in the inode incore. |
| 678 | */ |
| 679 | xfs_iroot_realloc(ip, 1, whichfork); |
| 680 | ifp->if_flags |= XFS_IFBROOT; |
| 681 | |
| 682 | /* |
| 683 | * Fill in the root. |
| 684 | */ |
| 685 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 686 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 687 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 688 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 689 | /* |
| 690 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 691 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 692 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 693 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 694 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 695 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 696 | /* |
| 697 | * Convert to a btree with two levels, one record in root. |
| 698 | */ |
| 699 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 700 | memset(&args, 0, sizeof(args)); |
| 701 | args.tp = tp; |
| 702 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 703 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 704 | args.firstblock = *firstblock; |
| 705 | if (*firstblock == NULLFSBLOCK) { |
| 706 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 707 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 708 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 709 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 710 | args.fsbno = *firstblock; |
| 711 | } else { |
| 712 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 713 | args.fsbno = *firstblock; |
| 714 | } |
| 715 | args.minlen = args.maxlen = args.prod = 1; |
| 716 | args.wasdel = wasdel; |
| 717 | *logflagsp = 0; |
| 718 | if ((error = xfs_alloc_vextent(&args))) { |
| 719 | xfs_iroot_realloc(ip, -1, whichfork); |
Eric Sandeen | 2c4306f | 2018-04-16 23:07:27 -0700 | [diff] [blame] | 720 | ASSERT(ifp->if_broot == NULL); |
| 721 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 722 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 723 | return error; |
| 724 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 725 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 726 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
| 727 | xfs_iroot_realloc(ip, -1, whichfork); |
Eric Sandeen | 2c4306f | 2018-04-16 23:07:27 -0700 | [diff] [blame] | 728 | ASSERT(ifp->if_broot == NULL); |
| 729 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 730 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 731 | return -ENOSPC; |
| 732 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 733 | /* |
| 734 | * Allocation can't fail, the space was reserved. |
| 735 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 736 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 737 | args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 738 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 739 | cur->bc_private.b.allocated++; |
| 740 | ip->i_d.di_nblocks++; |
| 741 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 742 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 743 | /* |
| 744 | * Fill in the child block. |
| 745 | */ |
| 746 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 747 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 748 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 749 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 750 | XFS_BTREE_LONG_PTRS); |
| 751 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 752 | for_each_xfs_iext(ifp, &icur, &rec) { |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 753 | if (isnullstartblock(rec.br_startblock)) |
| 754 | continue; |
| 755 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt); |
| 756 | xfs_bmbt_disk_set_all(arp, &rec); |
| 757 | cnt++; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 758 | } |
| 759 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 760 | xfs_btree_set_numrecs(ablock, cnt); |
| 761 | |
| 762 | /* |
| 763 | * Fill in the root key and pointer. |
| 764 | */ |
| 765 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 766 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 767 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 768 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 769 | be16_to_cpu(block->bb_level))); |
| 770 | *pp = cpu_to_be64(args.fsbno); |
| 771 | |
| 772 | /* |
| 773 | * Do all this logging at the end so that |
| 774 | * the root is at the right level. |
| 775 | */ |
| 776 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 777 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 778 | ASSERT(*curp == NULL); |
| 779 | *curp = cur; |
| 780 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Convert a local file to an extents file. |
| 786 | * This code is out of bounds for data forks of regular files, |
| 787 | * since the file data needs to get logged so things will stay consistent. |
| 788 | * (The bmap-level manipulations are ok, though). |
| 789 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 790 | void |
| 791 | xfs_bmap_local_to_extents_empty( |
| 792 | struct xfs_inode *ip, |
| 793 | int whichfork) |
| 794 | { |
| 795 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 796 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 797 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 798 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 799 | ASSERT(ifp->if_bytes == 0); |
| 800 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 801 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 802 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 803 | ifp->if_flags &= ~XFS_IFINLINE; |
| 804 | ifp->if_flags |= XFS_IFEXTENTS; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 805 | ifp->if_u1.if_root = NULL; |
| 806 | ifp->if_height = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 807 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 808 | } |
| 809 | |
| 810 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 811 | STATIC int /* error */ |
| 812 | xfs_bmap_local_to_extents( |
| 813 | xfs_trans_t *tp, /* transaction pointer */ |
| 814 | xfs_inode_t *ip, /* incore inode pointer */ |
| 815 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 816 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 817 | int *logflagsp, /* inode logging flags */ |
| 818 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 819 | void (*init_fn)(struct xfs_trans *tp, |
| 820 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 821 | struct xfs_inode *ip, |
| 822 | struct xfs_ifork *ifp)) |
| 823 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 824 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 825 | int flags; /* logging flags returned */ |
| 826 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 827 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 828 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 829 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 830 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 831 | |
| 832 | /* |
| 833 | * We don't want to deal with the case of keeping inode data inline yet. |
| 834 | * So sending the data fork of a regular inode is invalid. |
| 835 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 836 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 837 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 838 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 839 | |
| 840 | if (!ifp->if_bytes) { |
| 841 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 842 | flags = XFS_ILOG_CORE; |
| 843 | goto done; |
| 844 | } |
| 845 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 846 | flags = 0; |
| 847 | error = 0; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 848 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 849 | memset(&args, 0, sizeof(args)); |
| 850 | args.tp = tp; |
| 851 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 852 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 853 | args.firstblock = *firstblock; |
| 854 | /* |
| 855 | * Allocate a block. We know we need only one, since the |
| 856 | * file currently fits in an inode. |
| 857 | */ |
| 858 | if (*firstblock == NULLFSBLOCK) { |
| 859 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 860 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 861 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 862 | args.fsbno = *firstblock; |
| 863 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 864 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 865 | args.total = total; |
| 866 | args.minlen = args.maxlen = args.prod = 1; |
| 867 | error = xfs_alloc_vextent(&args); |
| 868 | if (error) |
| 869 | goto done; |
| 870 | |
| 871 | /* Can't fail, the space was reserved. */ |
| 872 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 873 | ASSERT(args.len == 1); |
| 874 | *firstblock = args.fsbno; |
| 875 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 876 | |
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 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 879 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 880 | * The callout is responsible for logging because the remote format |
| 881 | * might differ from the local format and thus we don't know how much to |
| 882 | * log here. Note that init_fn must also set the buffer log item type |
| 883 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 884 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 885 | init_fn(tp, bp, ip, ifp); |
| 886 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 887 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 888 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 889 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 890 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 891 | |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 892 | ifp->if_u1.if_root = NULL; |
| 893 | ifp->if_height = 0; |
| 894 | |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 895 | rec.br_startoff = 0; |
| 896 | rec.br_startblock = args.fsbno; |
| 897 | rec.br_blockcount = 1; |
| 898 | rec.br_state = XFS_EXT_NORM; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 899 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 900 | xfs_iext_insert(ip, &icur, &rec, 0); |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 901 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 902 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 903 | ip->i_d.di_nblocks = 1; |
| 904 | xfs_trans_mod_dquot_byino(tp, ip, |
| 905 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 906 | flags |= xfs_ilog_fext(whichfork); |
| 907 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 908 | done: |
| 909 | *logflagsp = flags; |
| 910 | return error; |
| 911 | } |
| 912 | |
| 913 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 915 | */ |
| 916 | STATIC int /* error */ |
| 917 | xfs_bmap_add_attrfork_btree( |
| 918 | xfs_trans_t *tp, /* transaction pointer */ |
| 919 | xfs_inode_t *ip, /* incore inode pointer */ |
| 920 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 921 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | int *flags) /* inode logging flags */ |
| 923 | { |
| 924 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 925 | int error; /* error return value */ |
| 926 | xfs_mount_t *mp; /* file system mount struct */ |
| 927 | int stat; /* newroot status */ |
| 928 | |
| 929 | mp = ip->i_mount; |
| 930 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 931 | *flags |= XFS_ILOG_DBROOT; |
| 932 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 933 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 934 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | cur->bc_private.b.firstblock = *firstblock; |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 936 | error = xfs_bmbt_lookup_first(cur, &stat); |
| 937 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 939 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 940 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 941 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | goto error0; |
| 943 | if (stat == 0) { |
| 944 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 945 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
| 947 | *firstblock = cur->bc_private.b.firstblock; |
| 948 | cur->bc_private.b.allocated = 0; |
| 949 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 950 | } |
| 951 | return 0; |
| 952 | error0: |
| 953 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 954 | return error; |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 959 | */ |
| 960 | STATIC int /* error */ |
| 961 | xfs_bmap_add_attrfork_extents( |
| 962 | xfs_trans_t *tp, /* transaction pointer */ |
| 963 | xfs_inode_t *ip, /* incore inode pointer */ |
| 964 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 965 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | int *flags) /* inode logging flags */ |
| 967 | { |
| 968 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 969 | int error; /* error return value */ |
| 970 | |
| 971 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 972 | return 0; |
| 973 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 974 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | flags, XFS_DATA_FORK); |
| 976 | if (cur) { |
| 977 | cur->bc_private.b.allocated = 0; |
| 978 | xfs_btree_del_cursor(cur, |
| 979 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 980 | } |
| 981 | return error; |
| 982 | } |
| 983 | |
| 984 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 985 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 986 | * different data fork content type needs a different callout to do the |
| 987 | * conversion. Some are basic and only require special block initialisation |
| 988 | * callouts for the data formating, others (directories) are so specialised they |
| 989 | * handle everything themselves. |
| 990 | * |
| 991 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 992 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 993 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | */ |
| 995 | STATIC int /* error */ |
| 996 | xfs_bmap_add_attrfork_local( |
| 997 | xfs_trans_t *tp, /* transaction pointer */ |
| 998 | xfs_inode_t *ip, /* incore inode pointer */ |
| 999 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1000 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | int *flags) /* inode logging flags */ |
| 1002 | { |
| 1003 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
| 1005 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1006 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1007 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1008 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1010 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | dargs.dp = ip; |
| 1012 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1013 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1014 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | dargs.whichfork = XFS_DATA_FORK; |
| 1016 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1017 | return xfs_dir2_sf_to_block(&dargs); |
| 1018 | } |
| 1019 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1020 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1021 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1022 | flags, XFS_DATA_FORK, |
| 1023 | xfs_symlink_local_to_remote); |
| 1024 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1025 | /* should only be called for types that support local format data */ |
| 1026 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1027 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1031 | * Convert inode from non-attributed to attributed. |
| 1032 | * Must not be in a transaction, ip must not be locked. |
| 1033 | */ |
| 1034 | int /* error code */ |
| 1035 | xfs_bmap_add_attrfork( |
| 1036 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1037 | int size, /* space new attribute needs */ |
| 1038 | int rsvd) /* xact may use reserved blks */ |
| 1039 | { |
| 1040 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1041 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1042 | xfs_mount_t *mp; /* mount structure */ |
| 1043 | xfs_trans_t *tp; /* transaction pointer */ |
| 1044 | int blks; /* space reservation */ |
| 1045 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1046 | int logflags; /* logging flags */ |
| 1047 | int error; /* error return value */ |
| 1048 | |
| 1049 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1050 | |
| 1051 | mp = ip->i_mount; |
| 1052 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1053 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1054 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1055 | |
| 1056 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1057 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1058 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1059 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1060 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1061 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1062 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1063 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1064 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1065 | if (error) |
| 1066 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1067 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1068 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1069 | if (ip->i_d.di_anextents != 0) { |
| 1070 | error = -EFSCORRUPTED; |
| 1071 | goto trans_cancel; |
| 1072 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1073 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1074 | /* |
| 1075 | * For inodes coming from pre-6.2 filesystems. |
| 1076 | */ |
| 1077 | ASSERT(ip->i_d.di_aformat == 0); |
| 1078 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1079 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1080 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1081 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1082 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1083 | |
| 1084 | switch (ip->i_d.di_format) { |
| 1085 | case XFS_DINODE_FMT_DEV: |
| 1086 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1087 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1088 | case XFS_DINODE_FMT_LOCAL: |
| 1089 | case XFS_DINODE_FMT_EXTENTS: |
| 1090 | case XFS_DINODE_FMT_BTREE: |
| 1091 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1092 | if (!ip->i_d.di_forkoff) |
| 1093 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1094 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1095 | version = 2; |
| 1096 | break; |
| 1097 | default: |
| 1098 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1099 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1100 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | ASSERT(ip->i_afp == NULL); |
| 1104 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1105 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1106 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1107 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1108 | switch (ip->i_d.di_format) { |
| 1109 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1110 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1111 | &logflags); |
| 1112 | break; |
| 1113 | case XFS_DINODE_FMT_EXTENTS: |
| 1114 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1115 | &dfops, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1116 | break; |
| 1117 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1118 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1119 | &logflags); |
| 1120 | break; |
| 1121 | default: |
| 1122 | error = 0; |
| 1123 | break; |
| 1124 | } |
| 1125 | if (logflags) |
| 1126 | xfs_trans_log_inode(tp, ip, logflags); |
| 1127 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1128 | goto bmap_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1129 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1130 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1131 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1132 | |
| 1133 | spin_lock(&mp->m_sb_lock); |
| 1134 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1135 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1136 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1137 | } |
| 1138 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1139 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1140 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1141 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1142 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1143 | if (log_sb) |
| 1144 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1145 | } |
| 1146 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 1147 | error = xfs_defer_finish(&tp, &dfops); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1148 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1149 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1150 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1151 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1152 | return error; |
| 1153 | |
| 1154 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1155 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1156 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1157 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1158 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1159 | return error; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Internal and external extent tree search functions. |
| 1164 | */ |
| 1165 | |
| 1166 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1167 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1168 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1169 | int |
| 1170 | xfs_iread_extents( |
| 1171 | struct xfs_trans *tp, |
| 1172 | struct xfs_inode *ip, |
| 1173 | int whichfork) |
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 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | e8e0e17 | 2017-10-19 11:06:29 -0700 | [diff] [blame] | 1176 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1177 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1178 | xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
| 1179 | struct xfs_btree_block *block = ifp->if_broot; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1180 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1181 | struct xfs_bmbt_irec new; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1182 | xfs_fsblock_t bno; |
| 1183 | struct xfs_buf *bp; |
| 1184 | xfs_extnum_t i, j; |
| 1185 | int level; |
| 1186 | __be64 *pp; |
| 1187 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1188 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1189 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1190 | |
| 1191 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 1192 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 1193 | return -EFSCORRUPTED; |
| 1194 | } |
| 1195 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1196 | /* |
| 1197 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1198 | */ |
| 1199 | level = be16_to_cpu(block->bb_level); |
| 1200 | ASSERT(level > 0); |
| 1201 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1202 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1203 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1204 | /* |
| 1205 | * Go down the tree until leaf level is reached, following the first |
| 1206 | * pointer (leftmost) at each level. |
| 1207 | */ |
| 1208 | while (level-- > 0) { |
| 1209 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1210 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1211 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1212 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1213 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1214 | if (level == 0) |
| 1215 | break; |
| 1216 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1217 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1218 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 1219 | xfs_verify_fsbno(mp, bno), out_brelse); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1220 | xfs_trans_brelse(tp, bp); |
| 1221 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1222 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1223 | /* |
| 1224 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1225 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1226 | i = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1227 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1228 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1229 | /* |
| 1230 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1231 | */ |
| 1232 | for (;;) { |
| 1233 | xfs_bmbt_rec_t *frp; |
| 1234 | xfs_fsblock_t nextbno; |
| 1235 | xfs_extnum_t num_recs; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1236 | |
| 1237 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1238 | if (unlikely(i + num_recs > nextents)) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1239 | xfs_warn(ip->i_mount, |
| 1240 | "corrupt dinode %Lu, (btree extents).", |
| 1241 | (unsigned long long) ip->i_ino); |
Darrick J. Wong | 90a58f9 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1242 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
| 1243 | __func__, block, sizeof(*block), |
| 1244 | __this_address); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1245 | error = -EFSCORRUPTED; |
| 1246 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1247 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1248 | /* |
| 1249 | * Read-ahead the next leaf block, if any. |
| 1250 | */ |
| 1251 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1252 | if (nextbno != NULLFSBLOCK) |
| 1253 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1254 | &xfs_bmbt_buf_ops); |
| 1255 | /* |
| 1256 | * Copy records into the extent records. |
| 1257 | */ |
| 1258 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1259 | for (j = 0; j < num_recs; j++, frp++, i++) { |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1260 | xfs_failaddr_t fa; |
| 1261 | |
Christoph Hellwig | dac9c9b | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1262 | xfs_bmbt_disk_get_all(frp, &new); |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1263 | fa = xfs_bmap_validate_extent(ip, whichfork, &new); |
| 1264 | if (fa) { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1265 | error = -EFSCORRUPTED; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1266 | xfs_inode_verifier_error(ip, error, |
| 1267 | "xfs_iread_extents(2)", |
| 1268 | frp, sizeof(*frp), fa); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1269 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1270 | } |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1271 | xfs_iext_insert(ip, &icur, &new, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1272 | trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); |
| 1273 | xfs_iext_next(ifp, &icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1274 | } |
| 1275 | xfs_trans_brelse(tp, bp); |
| 1276 | bno = nextbno; |
| 1277 | /* |
| 1278 | * If we've reached the end, stop. |
| 1279 | */ |
| 1280 | if (bno == NULLFSBLOCK) |
| 1281 | break; |
| 1282 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1283 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1284 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1285 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1286 | block = XFS_BUF_TO_BLOCK(bp); |
| 1287 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1288 | |
| 1289 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) { |
| 1290 | error = -EFSCORRUPTED; |
| 1291 | goto out; |
| 1292 | } |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1293 | ASSERT(i == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1294 | |
| 1295 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1296 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1297 | |
| 1298 | out_brelse: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1299 | xfs_trans_brelse(tp, bp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1300 | out: |
| 1301 | xfs_iext_destroy(ifp); |
| 1302 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1303 | } |
| 1304 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1305 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1306 | * Returns the relative block number of the first unused block(s) in the given |
| 1307 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1308 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1309 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1310 | */ |
| 1311 | int /* error */ |
| 1312 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1313 | struct xfs_trans *tp, /* transaction pointer */ |
| 1314 | struct xfs_inode *ip, /* incore inode */ |
| 1315 | xfs_extlen_t len, /* size of hole to find */ |
| 1316 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1317 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1318 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1319 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1320 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1321 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1322 | xfs_fileoff_t lastaddr = 0; |
| 1323 | xfs_fileoff_t lowest, max; |
| 1324 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1325 | |
| 1326 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1327 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1328 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1329 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1330 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1331 | *first_unused = 0; |
| 1332 | return 0; |
| 1333 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1334 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1335 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1336 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1337 | if (error) |
| 1338 | return error; |
| 1339 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1340 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1341 | lowest = max = *first_unused; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1342 | for_each_xfs_iext(ifp, &icur, &got) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1343 | /* |
| 1344 | * See if the hole before this extent will work. |
| 1345 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1346 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1347 | got.br_startoff - max >= len) |
| 1348 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1349 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1350 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1351 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1352 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1353 | *first_unused = max; |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1358 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1359 | * last_block (input value) in the file. |
| 1360 | * This is not based on i_size, it is based on the extent records. |
| 1361 | * Returns 0 for local files, as they do not have extent records. |
| 1362 | */ |
| 1363 | int /* error */ |
| 1364 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1365 | struct xfs_trans *tp, /* transaction pointer */ |
| 1366 | struct xfs_inode *ip, /* incore inode */ |
| 1367 | xfs_fileoff_t *last_block, /* last block */ |
| 1368 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1369 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1370 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1371 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1372 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1373 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1374 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1375 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1376 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1377 | *last_block = 0; |
| 1378 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1379 | case XFS_DINODE_FMT_BTREE: |
| 1380 | case XFS_DINODE_FMT_EXTENTS: |
| 1381 | break; |
| 1382 | default: |
| 1383 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1384 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1385 | |
| 1386 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1387 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1388 | if (error) |
| 1389 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1390 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1391 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1392 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1393 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1394 | return 0; |
| 1395 | } |
| 1396 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1397 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1398 | xfs_bmap_last_extent( |
| 1399 | struct xfs_trans *tp, |
| 1400 | struct xfs_inode *ip, |
| 1401 | int whichfork, |
| 1402 | struct xfs_bmbt_irec *rec, |
| 1403 | int *is_empty) |
| 1404 | { |
| 1405 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1406 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1407 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1408 | |
| 1409 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1410 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1411 | if (error) |
| 1412 | return error; |
| 1413 | } |
| 1414 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1415 | xfs_iext_last(ifp, &icur); |
| 1416 | if (!xfs_iext_get_extent(ifp, &icur, rec)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1417 | *is_empty = 1; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1418 | else |
| 1419 | *is_empty = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * Check the last inode extent to determine whether this allocation will result |
| 1425 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1426 | * blocks at the end of the file which do not start at the previous data block, |
| 1427 | * we will try to align the new blocks at stripe unit boundaries. |
| 1428 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1429 | * 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] | 1430 | * at, or past the EOF. |
| 1431 | */ |
| 1432 | STATIC int |
| 1433 | xfs_bmap_isaeof( |
| 1434 | struct xfs_bmalloca *bma, |
| 1435 | int whichfork) |
| 1436 | { |
| 1437 | struct xfs_bmbt_irec rec; |
| 1438 | int is_empty; |
| 1439 | int error; |
| 1440 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1441 | bma->aeof = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1442 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1443 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1444 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1445 | return error; |
| 1446 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1447 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1448 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1449 | return 0; |
| 1450 | } |
| 1451 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1452 | /* |
| 1453 | * Check if we are allocation or past the last extent, or at least into |
| 1454 | * the last delayed allocated extent. |
| 1455 | */ |
| 1456 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1457 | (bma->offset >= rec.br_startoff && |
| 1458 | isnullstartblock(rec.br_startblock)); |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1463 | * Returns the file-relative block number of the first block past eof in |
| 1464 | * the file. This is not based on i_size, it is based on the extent records. |
| 1465 | * Returns 0 for local files, as they do not have extent records. |
| 1466 | */ |
| 1467 | int |
| 1468 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1469 | struct xfs_inode *ip, |
| 1470 | xfs_fileoff_t *last_block, |
| 1471 | int whichfork) |
| 1472 | { |
| 1473 | struct xfs_bmbt_irec rec; |
| 1474 | int is_empty; |
| 1475 | int error; |
| 1476 | |
| 1477 | *last_block = 0; |
| 1478 | |
| 1479 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1480 | return 0; |
| 1481 | |
| 1482 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1483 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1484 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1485 | |
| 1486 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1487 | if (error || is_empty) |
| 1488 | return error; |
| 1489 | |
| 1490 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | /* |
| 1495 | * Returns whether the selected fork of the inode has exactly one |
| 1496 | * block or not. For the data fork we check this matches di_size, |
| 1497 | * implying the file's range is 0..bsize-1. |
| 1498 | */ |
| 1499 | int /* 1=>1 block, 0=>otherwise */ |
| 1500 | xfs_bmap_one_block( |
| 1501 | xfs_inode_t *ip, /* incore inode */ |
| 1502 | int whichfork) /* data or attr fork */ |
| 1503 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1504 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1505 | int rval; /* return value */ |
| 1506 | xfs_bmbt_irec_t s; /* internal version of extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1507 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1508 | |
| 1509 | #ifndef DEBUG |
| 1510 | if (whichfork == XFS_DATA_FORK) |
| 1511 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1512 | #endif /* !DEBUG */ |
| 1513 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1514 | return 0; |
| 1515 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1516 | return 0; |
| 1517 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1518 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1519 | xfs_iext_first(ifp, &icur); |
| 1520 | xfs_iext_get_extent(ifp, &icur, &s); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1521 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1522 | if (rval && whichfork == XFS_DATA_FORK) |
| 1523 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1524 | return rval; |
| 1525 | } |
| 1526 | |
| 1527 | /* |
| 1528 | * Extent tree manipulation functions used during allocation. |
| 1529 | */ |
| 1530 | |
| 1531 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1532 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | */ |
| 1534 | STATIC int /* error */ |
| 1535 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1536 | struct xfs_bmalloca *bma, |
| 1537 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1539 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1542 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1544 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1545 | /* left is 0, right is 1, prev is 2 */ |
| 1546 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1547 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1548 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1549 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1550 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1552 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1553 | xfs_extnum_t *nextents; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1554 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1556 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1557 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1558 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1559 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1560 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1561 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1562 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1563 | ASSERT(!bma->cur || |
| 1564 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1565 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1566 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | #define LEFT r[0] |
| 1569 | #define RIGHT r[1] |
| 1570 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | |
| 1572 | /* |
| 1573 | * Set up a bunch of variables to make the tests simpler. |
| 1574 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1575 | xfs_iext_get_extent(ifp, &bma->icur, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1577 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1579 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1580 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1581 | da_old = startblockval(PREV.br_startblock); |
| 1582 | da_new = 0; |
| 1583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | /* |
| 1585 | * Set flags determining what part of the previous delayed allocation |
| 1586 | * extent is being replaced by a real allocation. |
| 1587 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1588 | if (PREV.br_startoff == new->br_startoff) |
| 1589 | state |= BMAP_LEFT_FILLING; |
| 1590 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1591 | state |= BMAP_RIGHT_FILLING; |
| 1592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | /* |
| 1594 | * Check and set flags if this segment has a left neighbor. |
| 1595 | * Don't set contiguous if the combined extent would be too large. |
| 1596 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1597 | if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1598 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1599 | if (isnullstartblock(LEFT.br_startblock)) |
| 1600 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1602 | |
| 1603 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1604 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1605 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1606 | LEFT.br_state == new->br_state && |
| 1607 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1608 | state |= BMAP_LEFT_CONTIG; |
| 1609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | /* |
| 1611 | * Check and set flags if this segment has a right neighbor. |
| 1612 | * Don't set contiguous if the combined extent would be too large. |
| 1613 | * Also check for all-three-contiguous being too large. |
| 1614 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1615 | if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1616 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1617 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1618 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1620 | |
| 1621 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1622 | new_endoff == RIGHT.br_startoff && |
| 1623 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1624 | new->br_state == RIGHT.br_state && |
| 1625 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1626 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1627 | BMAP_RIGHT_FILLING)) != |
| 1628 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1629 | BMAP_RIGHT_FILLING) || |
| 1630 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1631 | <= MAXEXTLEN)) |
| 1632 | state |= BMAP_RIGHT_CONTIG; |
| 1633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | error = 0; |
| 1635 | /* |
| 1636 | * Switch out based on the FILLING and CONTIG state bits. |
| 1637 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1638 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1639 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1640 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1641 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | /* |
| 1643 | * Filling in all of a previously delayed allocation extent. |
| 1644 | * The left and right neighbors are both contiguous with new. |
| 1645 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1646 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1647 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1648 | xfs_iext_remove(bma->ip, &bma->icur, state); |
| 1649 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1650 | xfs_iext_prev(ifp, &bma->icur); |
| 1651 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1652 | (*nextents)--; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1653 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1654 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1656 | else { |
| 1657 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1658 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1659 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1661 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1662 | error = xfs_btree_delete(bma->cur, &i); |
| 1663 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1665 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1666 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1667 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1669 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1670 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1671 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | goto done; |
| 1673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | break; |
| 1675 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1676 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | /* |
| 1678 | * Filling in all of a previously delayed allocation extent. |
| 1679 | * The left neighbor is contiguous, the right is not. |
| 1680 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1681 | old = LEFT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1682 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1683 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1684 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1685 | xfs_iext_prev(ifp, &bma->icur); |
| 1686 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1687 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1688 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | rval = XFS_ILOG_DEXT; |
| 1690 | else { |
| 1691 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1692 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1693 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1695 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1696 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1697 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | goto done; |
| 1699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | break; |
| 1701 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1702 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | /* |
| 1704 | * Filling in all of a previously delayed allocation extent. |
| 1705 | * The right neighbor is contiguous, the left is not. |
| 1706 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1707 | PREV.br_startblock = new->br_startblock; |
| 1708 | PREV.br_blockcount += RIGHT.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1709 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1710 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1711 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1712 | xfs_iext_prev(ifp, &bma->icur); |
| 1713 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1714 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1715 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | rval = XFS_ILOG_DEXT; |
| 1717 | else { |
| 1718 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1719 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1720 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1722 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1723 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1724 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | goto done; |
| 1726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | break; |
| 1728 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1729 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | /* |
| 1731 | * Filling in all of a previously delayed allocation extent. |
| 1732 | * Neither the left nor right neighbors are contiguous with |
| 1733 | * the new one. |
| 1734 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1735 | PREV.br_startblock = new->br_startblock; |
| 1736 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1737 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1738 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1739 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1740 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1742 | else { |
| 1743 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1744 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1745 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1747 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1748 | error = xfs_btree_insert(bma->cur, &i); |
| 1749 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1751 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | break; |
| 1754 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1755 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | /* |
| 1757 | * Filling in the first part of a previous delayed allocation. |
| 1758 | * The left neighbor is contiguous. |
| 1759 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1760 | old = LEFT; |
| 1761 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1762 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1763 | startblockval(PREV.br_startblock)); |
| 1764 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1765 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1766 | |
Christoph Hellwig | bf99971 | 2017-11-03 10:34:38 -0700 | [diff] [blame] | 1767 | PREV.br_blockcount = temp; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1768 | PREV.br_startoff += new->br_blockcount; |
| 1769 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1770 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1771 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1772 | xfs_iext_prev(ifp, &bma->icur); |
| 1773 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1774 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1775 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | rval = XFS_ILOG_DEXT; |
| 1777 | else { |
| 1778 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1779 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1780 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1782 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1783 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1784 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | goto done; |
| 1786 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | break; |
| 1788 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1789 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | /* |
| 1791 | * Filling in the first part of a previous delayed allocation. |
| 1792 | * The left neighbor is not contiguous. |
| 1793 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1794 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1795 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1796 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1798 | else { |
| 1799 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1800 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1801 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1803 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1804 | error = xfs_btree_insert(bma->cur, &i); |
| 1805 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1807 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1809 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1810 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1811 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1812 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1813 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | rval |= tmp_rval; |
| 1815 | if (error) |
| 1816 | goto done; |
| 1817 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1818 | |
| 1819 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1820 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1821 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1822 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1823 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1824 | PREV.br_startoff = new_endoff; |
| 1825 | PREV.br_blockcount = temp; |
| 1826 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1827 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1828 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1829 | xfs_iext_prev(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | break; |
| 1831 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1832 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | /* |
| 1834 | * Filling in the last part of a previous delayed allocation. |
| 1835 | * The right neighbor is contiguous with the new allocation. |
| 1836 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1837 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1838 | RIGHT.br_startoff = new->br_startoff; |
| 1839 | RIGHT.br_startblock = new->br_startblock; |
| 1840 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1841 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1842 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | rval = XFS_ILOG_DEXT; |
| 1844 | else { |
| 1845 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1846 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1847 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1849 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1850 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1851 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | goto done; |
| 1853 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1854 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1855 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1856 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1857 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1858 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1859 | PREV.br_blockcount = temp; |
| 1860 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1861 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1862 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1863 | xfs_iext_next(ifp, &bma->icur); |
| 1864 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | break; |
| 1866 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1867 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | /* |
| 1869 | * Filling in the last part of a previous delayed allocation. |
| 1870 | * The right neighbor is not contiguous. |
| 1871 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1872 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1873 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1874 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1876 | else { |
| 1877 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1878 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1879 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1881 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1882 | error = xfs_btree_insert(bma->cur, &i); |
| 1883 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1885 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1887 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1888 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1889 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1890 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1891 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | rval |= tmp_rval; |
| 1893 | if (error) |
| 1894 | goto done; |
| 1895 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1896 | |
| 1897 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1898 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1899 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1900 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1901 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1902 | PREV.br_startblock = nullstartblock(da_new); |
| 1903 | PREV.br_blockcount = temp; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1904 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1905 | xfs_iext_next(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | break; |
| 1907 | |
| 1908 | case 0: |
| 1909 | /* |
| 1910 | * Filling in the middle part of a previous delayed allocation. |
| 1911 | * Contiguity is impossible here. |
| 1912 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1913 | * |
| 1914 | * We start with a delayed allocation: |
| 1915 | * |
| 1916 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1917 | * PREV @ idx |
| 1918 | * |
| 1919 | * and we are allocating: |
| 1920 | * +rrrrrrrrrrrrrrrrr+ |
| 1921 | * new |
| 1922 | * |
| 1923 | * and we set it up for insertion as: |
| 1924 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1925 | * new |
| 1926 | * PREV @ idx LEFT RIGHT |
| 1927 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1929 | old = PREV; |
| 1930 | |
| 1931 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1932 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1933 | |
| 1934 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1935 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1936 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1937 | RIGHT.br_blockcount = |
| 1938 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1939 | RIGHT.br_startblock = |
| 1940 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1941 | RIGHT.br_blockcount)); |
| 1942 | |
| 1943 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1944 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1945 | PREV.br_startblock = |
| 1946 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1947 | PREV.br_blockcount)); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1948 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1949 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1950 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1951 | xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); |
| 1952 | xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1953 | (*nextents)++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1954 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1955 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1957 | else { |
| 1958 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1959 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1960 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1962 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1963 | error = xfs_btree_insert(bma->cur, &i); |
| 1964 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1966 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1968 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1969 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1970 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1971 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1972 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | rval |= tmp_rval; |
| 1974 | if (error) |
| 1975 | goto done; |
| 1976 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1977 | |
| 1978 | da_new = startblockval(PREV.br_startblock) + |
| 1979 | startblockval(RIGHT.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | break; |
| 1981 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1982 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1983 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1984 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1985 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1986 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1987 | case BMAP_LEFT_CONTIG: |
| 1988 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | /* |
| 1990 | * These cases are all impossible. |
| 1991 | */ |
| 1992 | ASSERT(0); |
| 1993 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1994 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 1995 | /* add reverse mapping unless caller opted out */ |
| 1996 | if (!(bma->flags & XFS_BMAPI_NORMAP)) { |
| 1997 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, |
| 1998 | whichfork, new); |
| 1999 | if (error) |
| 2000 | goto done; |
| 2001 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2002 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2003 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2004 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2005 | int tmp_logflags; /* partial log flag return val */ |
| 2006 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2007 | ASSERT(bma->cur == NULL); |
| 2008 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2009 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2010 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2011 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2012 | if (error) |
| 2013 | goto done; |
| 2014 | } |
| 2015 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2016 | if (bma->cur) { |
| 2017 | da_new += bma->cur->bc_private.b.allocated; |
| 2018 | bma->cur->bc_private.b.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2021 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2022 | if (da_new != da_old) { |
| 2023 | ASSERT(state == 0 || da_new < da_old); |
| 2024 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2025 | false); |
| 2026 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2027 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2028 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2030 | if (whichfork != XFS_COW_FORK) |
| 2031 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | return error; |
| 2033 | #undef LEFT |
| 2034 | #undef RIGHT |
| 2035 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2039 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | */ |
| 2041 | STATIC int /* error */ |
| 2042 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2043 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2045 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2046 | struct xfs_iext_cursor *icur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2048 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2049 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2050 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2051 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2056 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2059 | /* left is 0, right is 1, prev is 2 */ |
| 2060 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2061 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2062 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2063 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2065 | *logflagsp = 0; |
| 2066 | |
| 2067 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2068 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2069 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2070 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2071 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2072 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | #define LEFT r[0] |
| 2075 | #define RIGHT r[1] |
| 2076 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | /* |
| 2079 | * Set up a bunch of variables to make the tests simpler. |
| 2080 | */ |
| 2081 | error = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2082 | xfs_iext_get_extent(ifp, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2083 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2085 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2086 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | /* |
| 2089 | * Set flags determining what part of the previous oldext allocation |
| 2090 | * extent is being replaced by a newext allocation. |
| 2091 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2092 | if (PREV.br_startoff == new->br_startoff) |
| 2093 | state |= BMAP_LEFT_FILLING; |
| 2094 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2095 | state |= BMAP_RIGHT_FILLING; |
| 2096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | /* |
| 2098 | * Check and set flags if this segment has a left neighbor. |
| 2099 | * Don't set contiguous if the combined extent would be too large. |
| 2100 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2101 | if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2102 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2103 | if (isnullstartblock(LEFT.br_startblock)) |
| 2104 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2106 | |
| 2107 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2108 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2109 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2110 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2111 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2112 | state |= BMAP_LEFT_CONTIG; |
| 2113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | /* |
| 2115 | * Check and set flags if this segment has a right neighbor. |
| 2116 | * Don't set contiguous if the combined extent would be too large. |
| 2117 | * Also check for all-three-contiguous being too large. |
| 2118 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2119 | if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2120 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2121 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2122 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2124 | |
| 2125 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2126 | new_endoff == RIGHT.br_startoff && |
| 2127 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2128 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2129 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2130 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2131 | BMAP_RIGHT_FILLING)) != |
| 2132 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2133 | BMAP_RIGHT_FILLING) || |
| 2134 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2135 | <= MAXEXTLEN)) |
| 2136 | state |= BMAP_RIGHT_CONTIG; |
| 2137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | /* |
| 2139 | * Switch out based on the FILLING and CONTIG state bits. |
| 2140 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2141 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2142 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2143 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2144 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | /* |
| 2146 | * Setting all of a previous oldext extent to newext. |
| 2147 | * The left and right neighbors are both contiguous with new. |
| 2148 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2149 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2150 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2151 | xfs_iext_remove(ip, icur, state); |
| 2152 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2153 | xfs_iext_prev(ifp, icur); |
| 2154 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2155 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2156 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | if (cur == NULL) |
| 2158 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2159 | else { |
| 2160 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2161 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2162 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2164 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2165 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2167 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2168 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2170 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2171 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2173 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2174 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2176 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2177 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2178 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | goto done; |
| 2180 | } |
| 2181 | break; |
| 2182 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2183 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | /* |
| 2185 | * Setting all of a previous oldext extent to newext. |
| 2186 | * The left neighbor is contiguous, the right is not. |
| 2187 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2188 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2189 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2190 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2191 | xfs_iext_prev(ifp, icur); |
| 2192 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2193 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2194 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | if (cur == NULL) |
| 2196 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2197 | else { |
| 2198 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2199 | error = xfs_bmbt_lookup_eq(cur, &PREV, &i); |
| 2200 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2202 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2203 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2205 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2206 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2208 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2209 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2210 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | goto done; |
| 2212 | } |
| 2213 | break; |
| 2214 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2215 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | /* |
| 2217 | * Setting all of a previous oldext extent to newext. |
| 2218 | * The right neighbor is contiguous, the left is not. |
| 2219 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2220 | PREV.br_blockcount += RIGHT.br_blockcount; |
| 2221 | PREV.br_state = new->br_state; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2222 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2223 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2224 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2225 | xfs_iext_prev(ifp, icur); |
| 2226 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2227 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2228 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2229 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | if (cur == NULL) |
| 2231 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2232 | else { |
| 2233 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2234 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2235 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2237 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2238 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2240 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2241 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2243 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2244 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2245 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | goto done; |
| 2247 | } |
| 2248 | break; |
| 2249 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2250 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | /* |
| 2252 | * Setting all of a previous oldext extent to newext. |
| 2253 | * Neither the left nor right neighbors are contiguous with |
| 2254 | * the new one. |
| 2255 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2256 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2257 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | if (cur == NULL) |
| 2260 | rval = XFS_ILOG_DEXT; |
| 2261 | else { |
| 2262 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2263 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2264 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2266 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2267 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2268 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | goto done; |
| 2270 | } |
| 2271 | break; |
| 2272 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2273 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | /* |
| 2275 | * Setting the first part of a previous oldext extent to newext. |
| 2276 | * The left neighbor is contiguous. |
| 2277 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2278 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2279 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2280 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2281 | PREV.br_startoff += new->br_blockcount; |
| 2282 | PREV.br_startblock += new->br_blockcount; |
| 2283 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2284 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2285 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2286 | xfs_iext_prev(ifp, icur); |
| 2287 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | if (cur == NULL) |
| 2290 | rval = XFS_ILOG_DEXT; |
| 2291 | else { |
| 2292 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2293 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2294 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2296 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2297 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2298 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2300 | error = xfs_btree_decrement(cur, 0, &i); |
| 2301 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2303 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2304 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | goto done; |
| 2306 | } |
| 2307 | break; |
| 2308 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2309 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | /* |
| 2311 | * Setting the first part of a previous oldext extent to newext. |
| 2312 | * The left neighbor is not contiguous. |
| 2313 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2314 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2315 | PREV.br_startoff += new->br_blockcount; |
| 2316 | PREV.br_startblock += new->br_blockcount; |
| 2317 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2318 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2319 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2320 | xfs_iext_insert(ip, icur, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2321 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2322 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | if (cur == NULL) |
| 2324 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2325 | else { |
| 2326 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2327 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2328 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2330 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2331 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2332 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | goto done; |
| 2334 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2335 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2337 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | } |
| 2339 | break; |
| 2340 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2341 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | /* |
| 2343 | * Setting the last part of a previous oldext extent to newext. |
| 2344 | * The right neighbor is contiguous with the new allocation. |
| 2345 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2346 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2347 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2348 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2349 | RIGHT.br_startoff = new->br_startoff; |
| 2350 | RIGHT.br_startblock = new->br_startblock; |
| 2351 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2352 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2353 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2354 | xfs_iext_next(ifp, icur); |
| 2355 | xfs_iext_update_extent(ip, state, icur, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | if (cur == NULL) |
| 2358 | rval = XFS_ILOG_DEXT; |
| 2359 | else { |
| 2360 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2361 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2362 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2364 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2365 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2366 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2368 | error = xfs_btree_increment(cur, 0, &i); |
| 2369 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2371 | error = xfs_bmbt_update(cur, &RIGHT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2372 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | goto done; |
| 2374 | } |
| 2375 | break; |
| 2376 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2377 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | /* |
| 2379 | * Setting the last part of a previous oldext extent to newext. |
| 2380 | * The right neighbor is not contiguous. |
| 2381 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2382 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2383 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2384 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2385 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2386 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2387 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2388 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2389 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2390 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | if (cur == NULL) |
| 2392 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2393 | else { |
| 2394 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2395 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2396 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2398 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2399 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2400 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2402 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2403 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2405 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2406 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2408 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | } |
| 2410 | break; |
| 2411 | |
| 2412 | case 0: |
| 2413 | /* |
| 2414 | * Setting the middle part of a previous oldext extent to |
| 2415 | * newext. Contiguity is impossible here. |
| 2416 | * One extent becomes three extents. |
| 2417 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2418 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2419 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | r[0] = *new; |
| 2422 | r[1].br_startoff = new_endoff; |
| 2423 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2424 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2426 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2427 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2428 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2429 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2430 | xfs_iext_insert(ip, icur, &r[1], state); |
| 2431 | xfs_iext_insert(ip, icur, &r[0], state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2432 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2433 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2434 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | if (cur == NULL) |
| 2436 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2437 | else { |
| 2438 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2439 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2440 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2442 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2444 | error = xfs_bmbt_update(cur, &r[1]); |
| 2445 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | goto done; |
| 2447 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2449 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2451 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2452 | /* |
| 2453 | * Reset the cursor to the position of the new extent |
| 2454 | * we are about to insert as we can't trust it after |
| 2455 | * the previous insert. |
| 2456 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2457 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2458 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2460 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2462 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2464 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | } |
| 2466 | break; |
| 2467 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2468 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2469 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2470 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2471 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2472 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2473 | case BMAP_LEFT_CONTIG: |
| 2474 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | /* |
| 2476 | * These cases are all impossible. |
| 2477 | */ |
| 2478 | ASSERT(0); |
| 2479 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2480 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2481 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2482 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2483 | if (error) |
| 2484 | goto done; |
| 2485 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2486 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2487 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2488 | int tmp_logflags; /* partial log flag return val */ |
| 2489 | |
| 2490 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2491 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2492 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2493 | *logflagsp |= tmp_logflags; |
| 2494 | if (error) |
| 2495 | goto done; |
| 2496 | } |
| 2497 | |
| 2498 | /* clear out the allocated field, done with it now in any case. */ |
| 2499 | if (cur) { |
| 2500 | cur->bc_private.b.allocated = 0; |
| 2501 | *curp = cur; |
| 2502 | } |
| 2503 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2504 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2506 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | return error; |
| 2508 | #undef LEFT |
| 2509 | #undef RIGHT |
| 2510 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2514 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2516 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | xfs_bmap_add_extent_hole_delay( |
| 2518 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2519 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2520 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2521 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2523 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2525 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2526 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2527 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2528 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2529 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2531 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2532 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | /* |
| 2535 | * Check and set flags if this segment has a left neighbor |
| 2536 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2537 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2538 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2539 | if (isnullstartblock(left.br_startblock)) |
| 2540 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | /* |
| 2544 | * Check and set flags if the current (right) segment exists. |
| 2545 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2546 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2547 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2548 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2549 | if (isnullstartblock(right.br_startblock)) |
| 2550 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | /* |
| 2554 | * Set contiguity flags on the left and right neighbors. |
| 2555 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2556 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2557 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2558 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2559 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2560 | state |= BMAP_LEFT_CONTIG; |
| 2561 | |
| 2562 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2563 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2564 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2565 | (!(state & BMAP_LEFT_CONTIG) || |
| 2566 | (left.br_blockcount + new->br_blockcount + |
| 2567 | right.br_blockcount <= MAXEXTLEN))) |
| 2568 | state |= BMAP_RIGHT_CONTIG; |
| 2569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | /* |
| 2571 | * Switch out based on the contiguity flags. |
| 2572 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2573 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2574 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | /* |
| 2576 | * New allocation is contiguous with delayed allocations |
| 2577 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2578 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | */ |
| 2580 | temp = left.br_blockcount + new->br_blockcount + |
| 2581 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2582 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2583 | oldlen = startblockval(left.br_startblock) + |
| 2584 | startblockval(new->br_startblock) + |
| 2585 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2586 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2587 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2588 | left.br_startblock = nullstartblock(newlen); |
| 2589 | left.br_blockcount = temp; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2590 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2591 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2592 | xfs_iext_prev(ifp, icur); |
| 2593 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | break; |
| 2595 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2596 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | /* |
| 2598 | * New allocation is contiguous with a delayed allocation |
| 2599 | * on the left. |
| 2600 | * Merge the new allocation with the left neighbor. |
| 2601 | */ |
| 2602 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2603 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2604 | oldlen = startblockval(left.br_startblock) + |
| 2605 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2606 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2607 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2608 | left.br_blockcount = temp; |
| 2609 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | 41d196f | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 2610 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2611 | xfs_iext_prev(ifp, icur); |
| 2612 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | break; |
| 2614 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2615 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | /* |
| 2617 | * New allocation is contiguous with a delayed allocation |
| 2618 | * on the right. |
| 2619 | * Merge the new allocation with the right neighbor. |
| 2620 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2622 | oldlen = startblockval(new->br_startblock) + |
| 2623 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2624 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2625 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2626 | right.br_startoff = new->br_startoff; |
| 2627 | right.br_startblock = nullstartblock(newlen); |
| 2628 | right.br_blockcount = temp; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2629 | xfs_iext_update_extent(ip, state, icur, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | break; |
| 2631 | |
| 2632 | case 0: |
| 2633 | /* |
| 2634 | * New allocation is not contiguous with another |
| 2635 | * delayed allocation. |
| 2636 | * Insert a new entry. |
| 2637 | */ |
| 2638 | oldlen = newlen = 0; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2639 | xfs_iext_insert(ip, icur, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | break; |
| 2641 | } |
| 2642 | if (oldlen != newlen) { |
| 2643 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2644 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2645 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | /* |
| 2647 | * Nothing to do for disk quota accounting here. |
| 2648 | */ |
| 2649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2653 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | */ |
| 2655 | STATIC int /* error */ |
| 2656 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2657 | struct xfs_trans *tp, |
| 2658 | struct xfs_inode *ip, |
| 2659 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2660 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2661 | struct xfs_btree_cur **curp, |
| 2662 | struct xfs_bmbt_irec *new, |
| 2663 | xfs_fsblock_t *first, |
| 2664 | struct xfs_defer_ops *dfops, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2665 | int *logflagsp, |
| 2666 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2668 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2669 | struct xfs_mount *mp = ip->i_mount; |
| 2670 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2674 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2675 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2676 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2677 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2679 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2680 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2681 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2682 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | /* |
| 2685 | * Check and set flags if this segment has a left neighbor. |
| 2686 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2687 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2688 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2689 | if (isnullstartblock(left.br_startblock)) |
| 2690 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | /* |
| 2694 | * Check and set flags if this segment has a current value. |
| 2695 | * Not true if we're inserting into the "hole" at eof. |
| 2696 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2697 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2698 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2699 | if (isnullstartblock(right.br_startblock)) |
| 2700 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | /* |
| 2704 | * We're inserting a real allocation between "left" and "right". |
| 2705 | * Set the contiguity flags. Don't let extents get too large. |
| 2706 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2707 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2708 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2709 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2710 | left.br_state == new->br_state && |
| 2711 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2712 | state |= BMAP_LEFT_CONTIG; |
| 2713 | |
| 2714 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2715 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2716 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2717 | new->br_state == right.br_state && |
| 2718 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2719 | (!(state & BMAP_LEFT_CONTIG) || |
| 2720 | left.br_blockcount + new->br_blockcount + |
| 2721 | right.br_blockcount <= MAXEXTLEN)) |
| 2722 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2724 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | /* |
| 2726 | * Select which case we're in here, and implement it. |
| 2727 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2728 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2729 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | /* |
| 2731 | * New allocation is contiguous with real allocations on the |
| 2732 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2733 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2735 | left.br_blockcount += new->br_blockcount + right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2736 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2737 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2738 | xfs_iext_prev(ifp, icur); |
| 2739 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2740 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2741 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2742 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2743 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2744 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2745 | } else { |
| 2746 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2747 | error = xfs_bmbt_lookup_eq(cur, &right, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2748 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2749 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2750 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2751 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2752 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2753 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2754 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2755 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2756 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2757 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2758 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2759 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2760 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2761 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2763 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2765 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | /* |
| 2767 | * New allocation is contiguous with a real allocation |
| 2768 | * on the left. |
| 2769 | * Merge the new allocation with the left neighbor. |
| 2770 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2771 | old = left; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2772 | left.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 1d2e008 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2773 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2774 | xfs_iext_prev(ifp, icur); |
| 2775 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2776 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2777 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2778 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2779 | } else { |
| 2780 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2781 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2782 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2783 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2784 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2785 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2786 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2787 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2789 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2791 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | /* |
| 2793 | * New allocation is contiguous with a real allocation |
| 2794 | * on the right. |
| 2795 | * Merge the new allocation with the right neighbor. |
| 2796 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2797 | old = right; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2798 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2799 | right.br_startoff = new->br_startoff; |
| 2800 | right.br_startblock = new->br_startblock; |
| 2801 | right.br_blockcount += new->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2802 | xfs_iext_update_extent(ip, state, icur, &right); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2803 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2804 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2805 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2806 | } else { |
| 2807 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2808 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2809 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2810 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2811 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2812 | error = xfs_bmbt_update(cur, &right); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2813 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2814 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2816 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | |
| 2818 | case 0: |
| 2819 | /* |
| 2820 | * New allocation is not contiguous with another |
| 2821 | * real allocation. |
| 2822 | * Insert a new entry. |
| 2823 | */ |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2824 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2825 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2826 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2827 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2828 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2829 | } else { |
| 2830 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2831 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2832 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2833 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2834 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2835 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2836 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2837 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2838 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2840 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2842 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2843 | /* add reverse mapping unless caller opted out */ |
| 2844 | if (!(flags & XFS_BMAPI_NORMAP)) { |
| 2845 | error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new); |
| 2846 | if (error) |
| 2847 | goto done; |
| 2848 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2849 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2850 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2851 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2852 | int tmp_logflags; /* partial log flag return val */ |
| 2853 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2854 | ASSERT(cur == NULL); |
| 2855 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2856 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2857 | *logflagsp |= tmp_logflags; |
| 2858 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2859 | if (error) |
| 2860 | goto done; |
| 2861 | } |
| 2862 | |
| 2863 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2864 | if (cur) |
| 2865 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2866 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2867 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2868 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2869 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2870 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | } |
| 2872 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2873 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2874 | * Functions used in the extent read, allocate and remove paths |
| 2875 | */ |
| 2876 | |
| 2877 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2878 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2879 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2880 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2881 | xfs_bmap_extsize_align( |
| 2882 | xfs_mount_t *mp, |
| 2883 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2884 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2885 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2886 | int rt, /* is this a realtime inode? */ |
| 2887 | int eof, /* is extent at end-of-file? */ |
| 2888 | int delay, /* creating delalloc extent? */ |
| 2889 | int convert, /* overwriting unwritten extent? */ |
| 2890 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2891 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2892 | { |
| 2893 | xfs_fileoff_t orig_off; /* original offset */ |
| 2894 | xfs_extlen_t orig_alen; /* original length */ |
| 2895 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2896 | xfs_fileoff_t nexto; /* next file offset */ |
| 2897 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2898 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2899 | xfs_extlen_t align_alen; /* temp for length */ |
| 2900 | xfs_extlen_t temp; /* temp for calculations */ |
| 2901 | |
| 2902 | if (convert) |
| 2903 | return 0; |
| 2904 | |
| 2905 | orig_off = align_off = *offp; |
| 2906 | orig_alen = align_alen = *lenp; |
| 2907 | orig_end = orig_off + orig_alen; |
| 2908 | |
| 2909 | /* |
| 2910 | * If this request overlaps an existing extent, then don't |
| 2911 | * attempt to perform any additional alignment. |
| 2912 | */ |
| 2913 | if (!delay && !eof && |
| 2914 | (orig_off >= gotp->br_startoff) && |
| 2915 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2916 | return 0; |
| 2917 | } |
| 2918 | |
| 2919 | /* |
| 2920 | * If the file offset is unaligned vs. the extent size |
| 2921 | * we need to align it. This will be possible unless |
| 2922 | * the file was previously written with a kernel that didn't |
| 2923 | * perform this alignment, or if a truncate shot us in the |
| 2924 | * foot. |
| 2925 | */ |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 2926 | div_u64_rem(orig_off, extsz, &temp); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2927 | if (temp) { |
| 2928 | align_alen += temp; |
| 2929 | align_off -= temp; |
| 2930 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2931 | |
| 2932 | /* Same adjustment for the end of the requested area. */ |
| 2933 | temp = (align_alen % extsz); |
| 2934 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2935 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2936 | |
| 2937 | /* |
| 2938 | * For large extent hint sizes, the aligned extent might be larger than |
| 2939 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 2940 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 2941 | * short allocation just fine, so it is safe to do this. We only want to |
| 2942 | * do it when we are forced to, though, because it means more allocation |
| 2943 | * operations are required. |
| 2944 | */ |
| 2945 | while (align_alen > MAXEXTLEN) |
| 2946 | align_alen -= extsz; |
| 2947 | ASSERT(align_alen <= MAXEXTLEN); |
| 2948 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2949 | /* |
| 2950 | * If the previous block overlaps with this proposed allocation |
| 2951 | * then move the start forward without adjusting the length. |
| 2952 | */ |
| 2953 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2954 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2955 | prevo = prevp->br_startoff; |
| 2956 | else |
| 2957 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2958 | } else |
| 2959 | prevo = 0; |
| 2960 | if (align_off != orig_off && align_off < prevo) |
| 2961 | align_off = prevo; |
| 2962 | /* |
| 2963 | * If the next block overlaps with this proposed allocation |
| 2964 | * then move the start back without adjusting the length, |
| 2965 | * but not before offset 0. |
| 2966 | * This may of course make the start overlap previous block, |
| 2967 | * and if we hit the offset 0 limit then the next block |
| 2968 | * can still overlap too. |
| 2969 | */ |
| 2970 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2971 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2972 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2973 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2974 | else |
| 2975 | nexto = gotp->br_startoff; |
| 2976 | } else |
| 2977 | nexto = NULLFILEOFF; |
| 2978 | if (!eof && |
| 2979 | align_off + align_alen != orig_end && |
| 2980 | align_off + align_alen > nexto) |
| 2981 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2982 | /* |
| 2983 | * If we're now overlapping the next or previous extent that |
| 2984 | * means we can't fit an extsz piece in this hole. Just move |
| 2985 | * the start forward to the first valid spot and set |
| 2986 | * the length so we hit the end. |
| 2987 | */ |
| 2988 | if (align_off != orig_off && align_off < prevo) |
| 2989 | align_off = prevo; |
| 2990 | if (align_off + align_alen != orig_end && |
| 2991 | align_off + align_alen > nexto && |
| 2992 | nexto != NULLFILEOFF) { |
| 2993 | ASSERT(nexto > prevo); |
| 2994 | align_alen = nexto - align_off; |
| 2995 | } |
| 2996 | |
| 2997 | /* |
| 2998 | * If realtime, and the result isn't a multiple of the realtime |
| 2999 | * extent size we need to remove blocks until it is. |
| 3000 | */ |
| 3001 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3002 | /* |
| 3003 | * We're not covering the original request, or |
| 3004 | * we won't be able to once we fix the length. |
| 3005 | */ |
| 3006 | if (orig_off < align_off || |
| 3007 | orig_end > align_off + align_alen || |
| 3008 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3009 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3010 | /* |
| 3011 | * Try to fix it by moving the start up. |
| 3012 | */ |
| 3013 | if (align_off + temp <= orig_off) { |
| 3014 | align_alen -= temp; |
| 3015 | align_off += temp; |
| 3016 | } |
| 3017 | /* |
| 3018 | * Try to fix it by moving the end in. |
| 3019 | */ |
| 3020 | else if (align_off + align_alen - temp >= orig_end) |
| 3021 | align_alen -= temp; |
| 3022 | /* |
| 3023 | * Set the start to the minimum then trim the length. |
| 3024 | */ |
| 3025 | else { |
| 3026 | align_alen -= orig_off - align_off; |
| 3027 | align_off = orig_off; |
| 3028 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3029 | } |
| 3030 | /* |
| 3031 | * Result doesn't cover the request, fail it. |
| 3032 | */ |
| 3033 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3034 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3035 | } else { |
| 3036 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3037 | /* see MAXEXTLEN handling above */ |
| 3038 | ASSERT(orig_end <= align_off + align_alen || |
| 3039 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | #ifdef DEBUG |
| 3043 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3044 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3045 | if (prevp->br_startoff != NULLFILEOFF) |
| 3046 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3047 | #endif |
| 3048 | |
| 3049 | *lenp = align_alen; |
| 3050 | *offp = align_off; |
| 3051 | return 0; |
| 3052 | } |
| 3053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3055 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3056 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3057 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3058 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | { |
| 3060 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3062 | xfs_mount_t *mp; /* mount point structure */ |
| 3063 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3064 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | |
| 3066 | #define ISVALID(x,y) \ |
| 3067 | (rt ? \ |
| 3068 | (x) < mp->m_sb.sb_rblocks : \ |
| 3069 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3070 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3071 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3074 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3075 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3076 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3077 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | /* |
| 3079 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3080 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3082 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3083 | !isnullstartblock(ap->prev.br_startblock) && |
| 3084 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3085 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3086 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3087 | /* |
| 3088 | * Adjust for the gap between prevp and us. |
| 3089 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3090 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3091 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3093 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3094 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | } |
| 3096 | /* |
| 3097 | * If not at eof, then compare the two neighbor blocks. |
| 3098 | * Figure out whether either one gives us a good starting point, |
| 3099 | * and pick the better one. |
| 3100 | */ |
| 3101 | else if (!ap->eof) { |
| 3102 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3103 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3104 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3105 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3106 | |
| 3107 | /* |
| 3108 | * If there's a previous (left) block, select a requested |
| 3109 | * start block based on it. |
| 3110 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3111 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3112 | !isnullstartblock(ap->prev.br_startblock) && |
| 3113 | (prevbno = ap->prev.br_startblock + |
| 3114 | ap->prev.br_blockcount) && |
| 3115 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | /* |
| 3117 | * Calculate gap to end of previous block. |
| 3118 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3119 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3120 | (ap->prev.br_startoff + |
| 3121 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | /* |
| 3123 | * Figure the startblock based on the previous block's |
| 3124 | * end and the gap size. |
| 3125 | * Heuristic! |
| 3126 | * If the gap is large relative to the piece we're |
| 3127 | * allocating, or using it gives us an invalid block |
| 3128 | * number, then just use the end of the previous block. |
| 3129 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3130 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3132 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | prevbno += adjust; |
| 3134 | else |
| 3135 | prevdiff += adjust; |
| 3136 | /* |
| 3137 | * If the firstblock forbids it, can't use it, |
| 3138 | * must use default. |
| 3139 | */ |
| 3140 | if (!rt && !nullfb && |
| 3141 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3142 | prevbno = NULLFSBLOCK; |
| 3143 | } |
| 3144 | /* |
| 3145 | * No previous block or can't follow it, just default. |
| 3146 | */ |
| 3147 | else |
| 3148 | prevbno = NULLFSBLOCK; |
| 3149 | /* |
| 3150 | * If there's a following (right) block, select a requested |
| 3151 | * start block based on it. |
| 3152 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3153 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | /* |
| 3155 | * Calculate gap to start of next block. |
| 3156 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3157 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | /* |
| 3159 | * Figure the startblock based on the next block's |
| 3160 | * start and the gap size. |
| 3161 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3162 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | /* |
| 3164 | * Heuristic! |
| 3165 | * If the gap is large relative to the piece we're |
| 3166 | * allocating, or using it gives us an invalid block |
| 3167 | * number, then just use the start of the next block |
| 3168 | * offset by our length. |
| 3169 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3170 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3172 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3173 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3174 | gotbno -= ap->length; |
| 3175 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | } else |
| 3177 | gotdiff += adjust; |
| 3178 | /* |
| 3179 | * If the firstblock forbids it, can't use it, |
| 3180 | * must use default. |
| 3181 | */ |
| 3182 | if (!rt && !nullfb && |
| 3183 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3184 | gotbno = NULLFSBLOCK; |
| 3185 | } |
| 3186 | /* |
| 3187 | * No next block, just default. |
| 3188 | */ |
| 3189 | else |
| 3190 | gotbno = NULLFSBLOCK; |
| 3191 | /* |
| 3192 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3193 | * 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] | 3194 | */ |
| 3195 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3196 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3198 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3200 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3202 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3203 | } |
| 3204 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3205 | static int |
| 3206 | xfs_bmap_longest_free_extent( |
| 3207 | struct xfs_trans *tp, |
| 3208 | xfs_agnumber_t ag, |
| 3209 | xfs_extlen_t *blen, |
| 3210 | int *notinit) |
| 3211 | { |
| 3212 | struct xfs_mount *mp = tp->t_mountp; |
| 3213 | struct xfs_perag *pag; |
| 3214 | xfs_extlen_t longest; |
| 3215 | int error = 0; |
| 3216 | |
| 3217 | pag = xfs_perag_get(mp, ag); |
| 3218 | if (!pag->pagf_init) { |
| 3219 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3220 | if (error) |
| 3221 | goto out; |
| 3222 | |
| 3223 | if (!pag->pagf_init) { |
| 3224 | *notinit = 1; |
| 3225 | goto out; |
| 3226 | } |
| 3227 | } |
| 3228 | |
Eric Sandeen | a1f6941 | 2018-04-06 10:09:42 -0700 | [diff] [blame] | 3229 | longest = xfs_alloc_longest_free_extent(pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3230 | xfs_alloc_min_freelist(mp, pag), |
| 3231 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3232 | if (*blen < longest) |
| 3233 | *blen = longest; |
| 3234 | |
| 3235 | out: |
| 3236 | xfs_perag_put(pag); |
| 3237 | return error; |
| 3238 | } |
| 3239 | |
| 3240 | static void |
| 3241 | xfs_bmap_select_minlen( |
| 3242 | struct xfs_bmalloca *ap, |
| 3243 | struct xfs_alloc_arg *args, |
| 3244 | xfs_extlen_t *blen, |
| 3245 | int notinit) |
| 3246 | { |
| 3247 | if (notinit || *blen < ap->minlen) { |
| 3248 | /* |
| 3249 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3250 | * there is space for this request. |
| 3251 | */ |
| 3252 | args->minlen = ap->minlen; |
| 3253 | } else if (*blen < args->maxlen) { |
| 3254 | /* |
| 3255 | * If the best seen length is less than the request length, |
| 3256 | * use the best as the minimum. |
| 3257 | */ |
| 3258 | args->minlen = *blen; |
| 3259 | } else { |
| 3260 | /* |
| 3261 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3262 | * as the minimum. |
| 3263 | */ |
| 3264 | args->minlen = args->maxlen; |
| 3265 | } |
| 3266 | } |
| 3267 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3268 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3269 | xfs_bmap_btalloc_nullfb( |
| 3270 | struct xfs_bmalloca *ap, |
| 3271 | struct xfs_alloc_arg *args, |
| 3272 | xfs_extlen_t *blen) |
| 3273 | { |
| 3274 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3275 | xfs_agnumber_t ag, startag; |
| 3276 | int notinit = 0; |
| 3277 | int error; |
| 3278 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3279 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3280 | args->total = ap->total; |
| 3281 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3282 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3283 | if (startag == NULLAGNUMBER) |
| 3284 | startag = ag = 0; |
| 3285 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3286 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3287 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3288 | ¬init); |
| 3289 | if (error) |
| 3290 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3291 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3292 | if (++ag == mp->m_sb.sb_agcount) |
| 3293 | ag = 0; |
| 3294 | if (ag == startag) |
| 3295 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3296 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3297 | |
| 3298 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3299 | return 0; |
| 3300 | } |
| 3301 | |
| 3302 | STATIC int |
| 3303 | xfs_bmap_btalloc_filestreams( |
| 3304 | struct xfs_bmalloca *ap, |
| 3305 | struct xfs_alloc_arg *args, |
| 3306 | xfs_extlen_t *blen) |
| 3307 | { |
| 3308 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3309 | xfs_agnumber_t ag; |
| 3310 | int notinit = 0; |
| 3311 | int error; |
| 3312 | |
| 3313 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3314 | args->total = ap->total; |
| 3315 | |
| 3316 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3317 | if (ag == NULLAGNUMBER) |
| 3318 | ag = 0; |
| 3319 | |
| 3320 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3321 | if (error) |
| 3322 | return error; |
| 3323 | |
| 3324 | if (*blen < args->maxlen) { |
| 3325 | error = xfs_filestream_new_ag(ap, &ag); |
| 3326 | if (error) |
| 3327 | return error; |
| 3328 | |
| 3329 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3330 | ¬init); |
| 3331 | if (error) |
| 3332 | return error; |
| 3333 | |
| 3334 | } |
| 3335 | |
| 3336 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3337 | |
| 3338 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3339 | * Set the failure fallback case to look in the selected AG as stream |
| 3340 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3341 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3342 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3343 | return 0; |
| 3344 | } |
| 3345 | |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3346 | /* Update all inode and quota accounting for the allocation we just did. */ |
| 3347 | static void |
| 3348 | xfs_bmap_btalloc_accounting( |
| 3349 | struct xfs_bmalloca *ap, |
| 3350 | struct xfs_alloc_arg *args) |
| 3351 | { |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3352 | if (ap->flags & XFS_BMAPI_COWFORK) { |
| 3353 | /* |
| 3354 | * COW fork blocks are in-core only and thus are treated as |
| 3355 | * in-core quota reservation (like delalloc blocks) even when |
| 3356 | * converted to real blocks. The quota reservation is not |
| 3357 | * accounted to disk until blocks are remapped to the data |
| 3358 | * fork. So if these blocks were previously delalloc, we |
| 3359 | * already have quota reservation and there's nothing to do |
| 3360 | * yet. |
| 3361 | */ |
| 3362 | if (ap->wasdel) |
| 3363 | return; |
| 3364 | |
| 3365 | /* |
| 3366 | * Otherwise, we've allocated blocks in a hole. The transaction |
| 3367 | * has acquired in-core quota reservation for this extent. |
| 3368 | * Rather than account these as real blocks, however, we reduce |
| 3369 | * the transaction quota reservation based on the allocation. |
| 3370 | * This essentially transfers the transaction quota reservation |
| 3371 | * to that of a delalloc extent. |
| 3372 | */ |
| 3373 | ap->ip->i_delayed_blks += args->len; |
| 3374 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS, |
| 3375 | -(long)args->len); |
| 3376 | return; |
| 3377 | } |
| 3378 | |
| 3379 | /* data/attr fork only */ |
| 3380 | ap->ip->i_d.di_nblocks += args->len; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3381 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3382 | if (ap->wasdel) |
| 3383 | ap->ip->i_delayed_blks -= args->len; |
| 3384 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 3385 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT, |
| 3386 | args->len); |
| 3387 | } |
| 3388 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3389 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3390 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3391 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3392 | { |
| 3393 | xfs_mount_t *mp; /* mount point structure */ |
| 3394 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3395 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3396 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3397 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3398 | xfs_alloc_arg_t args; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3399 | xfs_fileoff_t orig_offset; |
| 3400 | xfs_extlen_t orig_length; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3401 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3402 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3403 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3404 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3405 | int tryagain; |
| 3406 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3407 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3408 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3409 | ASSERT(ap->length); |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3410 | orig_offset = ap->offset; |
| 3411 | orig_length = ap->length; |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3412 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3413 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3414 | |
| 3415 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3416 | stripe_align = 0; |
| 3417 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3418 | stripe_align = mp->m_swidth; |
| 3419 | else if (mp->m_dalign) |
| 3420 | stripe_align = mp->m_dalign; |
| 3421 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3422 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3423 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3424 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3425 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3426 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3427 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3428 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3429 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3430 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3431 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3432 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3433 | |
| 3434 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3435 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3436 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3437 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3438 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3439 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3440 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3441 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3442 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3443 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3444 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3445 | } |
| 3446 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3447 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3448 | |
| 3449 | xfs_bmap_adjacent(ap); |
| 3450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3452 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | * it's in the right allocation group. |
| 3454 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3455 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | ; |
| 3457 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3458 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | * Normal allocation, done through xfs_alloc_vextent. |
| 3461 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3462 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3463 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3464 | args.tp = ap->tp; |
| 3465 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3466 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3467 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3468 | |
| 3469 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 9bb54cb | 2018-06-07 07:54:02 -0700 | [diff] [blame] | 3470 | args.maxlen = min(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3471 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3472 | blen = 0; |
| 3473 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3474 | /* |
| 3475 | * Search for an allocation group with a single extent large |
| 3476 | * enough for the request. If one isn't found, then adjust |
| 3477 | * the minimum allocation size to the largest space found. |
| 3478 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3479 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3480 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3481 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3482 | else |
| 3483 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3484 | if (error) |
| 3485 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3486 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3487 | if (xfs_inode_is_filestream(ap->ip)) |
| 3488 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3489 | else |
| 3490 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3491 | args.total = args.minlen = ap->minlen; |
| 3492 | } else { |
| 3493 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3494 | args.total = ap->total; |
| 3495 | args.minlen = ap->minlen; |
| 3496 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3497 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3498 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3499 | args.prod = align; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3500 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3501 | if (args.mod) |
| 3502 | args.mod = args.prod - args.mod; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3503 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3504 | args.prod = 1; |
| 3505 | args.mod = 0; |
| 3506 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3507 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 3508 | div_u64_rem(ap->offset, args.prod, &args.mod); |
| 3509 | if (args.mod) |
| 3510 | args.mod = args.prod - args.mod; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3511 | } |
| 3512 | /* |
| 3513 | * If we are not low on available data blocks, and the |
| 3514 | * underlying logical volume manager is a stripe, and |
| 3515 | * the file offset is zero then try to allocate data |
| 3516 | * blocks on stripe unit boundary. |
| 3517 | * NOTE: ap->aeof is only set if the allocation length |
| 3518 | * is >= the stripe unit and the allocation offset is |
| 3519 | * at the end of file. |
| 3520 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3521 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3522 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3523 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3524 | atype = args.type; |
| 3525 | isaligned = 1; |
| 3526 | /* |
| 3527 | * Adjust for alignment |
| 3528 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3529 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3530 | args.minlen = blen - args.alignment; |
| 3531 | args.minalignslop = 0; |
| 3532 | } else { |
| 3533 | /* |
| 3534 | * First try an exact bno allocation. |
| 3535 | * If it fails then do a near or start bno |
| 3536 | * allocation with alignment turned on. |
| 3537 | */ |
| 3538 | atype = args.type; |
| 3539 | tryagain = 1; |
| 3540 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3541 | args.alignment = 1; |
| 3542 | /* |
| 3543 | * Compute the minlen+alignment for the |
| 3544 | * next case. Set slop so that the value |
| 3545 | * of minlen+alignment+slop doesn't go up |
| 3546 | * between the calls. |
| 3547 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3548 | if (blen > stripe_align && blen <= args.maxlen) |
| 3549 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3550 | else |
| 3551 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3552 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3553 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3554 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3555 | args.minlen - 1; |
| 3556 | else |
| 3557 | args.minalignslop = 0; |
| 3558 | } |
| 3559 | } else { |
| 3560 | args.alignment = 1; |
| 3561 | args.minalignslop = 0; |
| 3562 | } |
| 3563 | args.minleft = ap->minleft; |
| 3564 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3565 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3566 | args.datatype = ap->datatype; |
| 3567 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3568 | args.ip = ap->ip; |
| 3569 | |
| 3570 | error = xfs_alloc_vextent(&args); |
| 3571 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3572 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3573 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3574 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3575 | /* |
| 3576 | * Exact allocation failed. Now try with alignment |
| 3577 | * turned on. |
| 3578 | */ |
| 3579 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3580 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3581 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3582 | args.minlen = nextminlen; |
| 3583 | args.minalignslop = 0; |
| 3584 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3585 | if ((error = xfs_alloc_vextent(&args))) |
| 3586 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3587 | } |
| 3588 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3589 | /* |
| 3590 | * allocation failed, so turn off alignment and |
| 3591 | * try again. |
| 3592 | */ |
| 3593 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3594 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3595 | args.alignment = 0; |
| 3596 | if ((error = xfs_alloc_vextent(&args))) |
| 3597 | return error; |
| 3598 | } |
| 3599 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3600 | args.minlen > ap->minlen) { |
| 3601 | args.minlen = ap->minlen; |
| 3602 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3603 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3604 | if ((error = xfs_alloc_vextent(&args))) |
| 3605 | return error; |
| 3606 | } |
| 3607 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3608 | args.fsbno = 0; |
| 3609 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3610 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3611 | if ((error = xfs_alloc_vextent(&args))) |
| 3612 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3613 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3614 | } |
| 3615 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3616 | /* |
| 3617 | * check the allocation happened at the same or higher AG than |
| 3618 | * the first block that was allocated. |
| 3619 | */ |
| 3620 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3621 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) <= |
| 3622 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3623 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3624 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3625 | if (*ap->firstblock == NULLFSBLOCK) |
| 3626 | *ap->firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3627 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3628 | ap->length = args.len; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3629 | /* |
| 3630 | * If the extent size hint is active, we tried to round the |
| 3631 | * caller's allocation request offset down to extsz and the |
| 3632 | * length up to another extsz boundary. If we found a free |
| 3633 | * extent we mapped it in starting at this new offset. If the |
| 3634 | * newly mapped space isn't long enough to cover any of the |
| 3635 | * range of offsets that was originally requested, move the |
| 3636 | * mapping up so that we can fill as much of the caller's |
| 3637 | * original request as possible. Free space is apparently |
| 3638 | * very fragmented so we're unlikely to be able to satisfy the |
| 3639 | * hints anyway. |
| 3640 | */ |
| 3641 | if (ap->length <= orig_length) |
| 3642 | ap->offset = orig_offset; |
| 3643 | else if (ap->offset + ap->length < orig_offset + orig_length) |
| 3644 | ap->offset = orig_offset + orig_length - ap->length; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3645 | xfs_bmap_btalloc_accounting(ap, &args); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3646 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3647 | ap->blkno = NULLFSBLOCK; |
| 3648 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | } |
| 3650 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | /* |
| 3654 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3655 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3656 | */ |
| 3657 | STATIC int |
| 3658 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3659 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3660 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3661 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3662 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3663 | return xfs_bmap_rtalloc(ap); |
| 3664 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | } |
| 3666 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3667 | /* Trim extent to fit a logical block range. */ |
| 3668 | void |
| 3669 | xfs_trim_extent( |
| 3670 | struct xfs_bmbt_irec *irec, |
| 3671 | xfs_fileoff_t bno, |
| 3672 | xfs_filblks_t len) |
| 3673 | { |
| 3674 | xfs_fileoff_t distance; |
| 3675 | xfs_fileoff_t end = bno + len; |
| 3676 | |
| 3677 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3678 | irec->br_startoff >= end) { |
| 3679 | irec->br_blockcount = 0; |
| 3680 | return; |
| 3681 | } |
| 3682 | |
| 3683 | if (irec->br_startoff < bno) { |
| 3684 | distance = bno - irec->br_startoff; |
| 3685 | if (isnullstartblock(irec->br_startblock)) |
| 3686 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3687 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3688 | irec->br_startblock != HOLESTARTBLOCK) |
| 3689 | irec->br_startblock += distance; |
| 3690 | irec->br_startoff += distance; |
| 3691 | irec->br_blockcount -= distance; |
| 3692 | } |
| 3693 | |
| 3694 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3695 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3696 | irec->br_blockcount -= distance; |
| 3697 | } |
| 3698 | } |
| 3699 | |
Brian Foster | 40214d1 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 3700 | /* trim extent to within eof */ |
| 3701 | void |
| 3702 | xfs_trim_extent_eof( |
| 3703 | struct xfs_bmbt_irec *irec, |
| 3704 | struct xfs_inode *ip) |
| 3705 | |
| 3706 | { |
| 3707 | xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount, |
| 3708 | i_size_read(VFS_I(ip)))); |
| 3709 | } |
| 3710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3712 | * Trim the returned map to the required bounds |
| 3713 | */ |
| 3714 | STATIC void |
| 3715 | xfs_bmapi_trim_map( |
| 3716 | struct xfs_bmbt_irec *mval, |
| 3717 | struct xfs_bmbt_irec *got, |
| 3718 | xfs_fileoff_t *bno, |
| 3719 | xfs_filblks_t len, |
| 3720 | xfs_fileoff_t obno, |
| 3721 | xfs_fileoff_t end, |
| 3722 | int n, |
| 3723 | int flags) |
| 3724 | { |
| 3725 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3726 | got->br_startoff + got->br_blockcount <= obno) { |
| 3727 | *mval = *got; |
| 3728 | if (isnullstartblock(got->br_startblock)) |
| 3729 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3730 | return; |
| 3731 | } |
| 3732 | |
| 3733 | if (obno > *bno) |
| 3734 | *bno = obno; |
| 3735 | ASSERT((*bno >= obno) || (n == 0)); |
| 3736 | ASSERT(*bno < end); |
| 3737 | mval->br_startoff = *bno; |
| 3738 | if (isnullstartblock(got->br_startblock)) |
| 3739 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3740 | else |
| 3741 | mval->br_startblock = got->br_startblock + |
| 3742 | (*bno - got->br_startoff); |
| 3743 | /* |
| 3744 | * Return the minimum of what we got and what we asked for for |
| 3745 | * the length. We can use the len variable here because it is |
| 3746 | * modified below and we could have been there before coming |
| 3747 | * here if the first part of the allocation didn't overlap what |
| 3748 | * was asked for. |
| 3749 | */ |
| 3750 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3751 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3752 | mval->br_state = got->br_state; |
| 3753 | ASSERT(mval->br_blockcount <= len); |
| 3754 | return; |
| 3755 | } |
| 3756 | |
| 3757 | /* |
| 3758 | * Update and validate the extent map to return |
| 3759 | */ |
| 3760 | STATIC void |
| 3761 | xfs_bmapi_update_map( |
| 3762 | struct xfs_bmbt_irec **map, |
| 3763 | xfs_fileoff_t *bno, |
| 3764 | xfs_filblks_t *len, |
| 3765 | xfs_fileoff_t obno, |
| 3766 | xfs_fileoff_t end, |
| 3767 | int *n, |
| 3768 | int flags) |
| 3769 | { |
| 3770 | xfs_bmbt_irec_t *mval = *map; |
| 3771 | |
| 3772 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3773 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3774 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3775 | (mval->br_startoff < obno)); |
| 3776 | |
| 3777 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3778 | *len = end - *bno; |
| 3779 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3780 | /* update previous map with new information */ |
| 3781 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3782 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3783 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3784 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3785 | mval[-1].br_state = mval->br_state; |
| 3786 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3787 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3788 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3789 | mval->br_startblock == mval[-1].br_startblock + |
| 3790 | mval[-1].br_blockcount && |
| 3791 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3792 | mval[-1].br_state == mval->br_state)) { |
| 3793 | ASSERT(mval->br_startoff == |
| 3794 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3795 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3796 | } else if (*n > 0 && |
| 3797 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3798 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3799 | mval->br_startoff == |
| 3800 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3801 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3802 | mval[-1].br_state = mval->br_state; |
| 3803 | } else if (!((*n == 0) && |
| 3804 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3805 | obno))) { |
| 3806 | mval++; |
| 3807 | (*n)++; |
| 3808 | } |
| 3809 | *map = mval; |
| 3810 | } |
| 3811 | |
| 3812 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3813 | * Map file blocks to filesystem blocks without allocation. |
| 3814 | */ |
| 3815 | int |
| 3816 | xfs_bmapi_read( |
| 3817 | struct xfs_inode *ip, |
| 3818 | xfs_fileoff_t bno, |
| 3819 | xfs_filblks_t len, |
| 3820 | struct xfs_bmbt_irec *mval, |
| 3821 | int *nmap, |
| 3822 | int flags) |
| 3823 | { |
| 3824 | struct xfs_mount *mp = ip->i_mount; |
| 3825 | struct xfs_ifork *ifp; |
| 3826 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3827 | xfs_fileoff_t obno; |
| 3828 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3829 | struct xfs_iext_cursor icur; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3830 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3831 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3832 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3833 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3834 | |
| 3835 | ASSERT(*nmap >= 1); |
| 3836 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3837 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3838 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3839 | |
| 3840 | if (unlikely(XFS_TEST_ERROR( |
| 3841 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3842 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 3843 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3844 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3845 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3849 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3850 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3851 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3852 | |
| 3853 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3854 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3855 | /* No CoW fork? Return a hole. */ |
| 3856 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 3857 | mval->br_startoff = bno; |
| 3858 | mval->br_startblock = HOLESTARTBLOCK; |
| 3859 | mval->br_blockcount = len; |
| 3860 | mval->br_state = XFS_EXT_NORM; |
| 3861 | *nmap = 1; |
| 3862 | return 0; |
| 3863 | } |
| 3864 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3865 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3866 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3867 | if (error) |
| 3868 | return error; |
| 3869 | } |
| 3870 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3871 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3872 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3873 | end = bno + len; |
| 3874 | obno = bno; |
| 3875 | |
| 3876 | while (bno < end && n < *nmap) { |
| 3877 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3878 | if (eof) |
| 3879 | got.br_startoff = end; |
| 3880 | if (got.br_startoff > bno) { |
| 3881 | /* Reading in a hole. */ |
| 3882 | mval->br_startoff = bno; |
| 3883 | mval->br_startblock = HOLESTARTBLOCK; |
| 3884 | mval->br_blockcount = |
| 3885 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3886 | mval->br_state = XFS_EXT_NORM; |
| 3887 | bno += mval->br_blockcount; |
| 3888 | len -= mval->br_blockcount; |
| 3889 | mval++; |
| 3890 | n++; |
| 3891 | continue; |
| 3892 | } |
| 3893 | |
| 3894 | /* set up the extent map to return. */ |
| 3895 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3896 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3897 | |
| 3898 | /* If we're done, stop now. */ |
| 3899 | if (bno >= end || n >= *nmap) |
| 3900 | break; |
| 3901 | |
| 3902 | /* Else go on to the next record. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3903 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3904 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3905 | } |
| 3906 | *nmap = n; |
| 3907 | return 0; |
| 3908 | } |
| 3909 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3910 | /* |
| 3911 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3912 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3913 | * |
| 3914 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3915 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3916 | * to the extent record that was inserted to the inode fork. |
| 3917 | * |
| 3918 | * Note that the allocated extent may have been merged with contiguous extents |
| 3919 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3920 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3921 | * look up the updated record in the inode fork. |
| 3922 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3923 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3924 | xfs_bmapi_reserve_delalloc( |
| 3925 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3926 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3927 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3928 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3929 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3930 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3931 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3932 | int eof) |
| 3933 | { |
| 3934 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3935 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3936 | xfs_extlen_t alen; |
| 3937 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3938 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3939 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3940 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3941 | /* |
| 3942 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3943 | * tag the inode before we return. |
| 3944 | */ |
| 3945 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3946 | if (!eof) |
| 3947 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3948 | if (prealloc && alen >= len) |
| 3949 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3950 | |
| 3951 | /* Figure out the extent size, adjust alen */ |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3952 | if (whichfork == XFS_COW_FORK) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3953 | struct xfs_bmbt_irec prev; |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3954 | xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3955 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3956 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3957 | prev.br_startoff = NULLFILEOFF; |
| 3958 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3959 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3960 | 1, 0, &aoff, &alen); |
| 3961 | ASSERT(!error); |
| 3962 | } |
| 3963 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3964 | /* |
| 3965 | * Make a transaction-less quota reservation for delayed allocation |
| 3966 | * blocks. This number gets adjusted later. We return if we haven't |
| 3967 | * allocated blocks already inside this loop. |
| 3968 | */ |
| 3969 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3970 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3971 | if (error) |
| 3972 | return error; |
| 3973 | |
| 3974 | /* |
| 3975 | * Split changing sb for alen and indlen since they could be coming |
| 3976 | * from different places. |
| 3977 | */ |
| 3978 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 3979 | ASSERT(indlen > 0); |
| 3980 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3981 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3982 | if (error) |
| 3983 | goto out_unreserve_quota; |
| 3984 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3985 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3986 | if (error) |
| 3987 | goto out_unreserve_blocks; |
| 3988 | |
| 3989 | |
| 3990 | ip->i_delayed_blks += alen; |
| 3991 | |
| 3992 | got->br_startoff = aoff; |
| 3993 | got->br_startblock = nullstartblock(indlen); |
| 3994 | got->br_blockcount = alen; |
| 3995 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3996 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3997 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3998 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3999 | /* |
| 4000 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 4001 | * preallocation can occur at the start or end of the extent, even when |
| 4002 | * prealloc == 0, so we must also check the aligned offset and length. |
| 4003 | */ |
| 4004 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4005 | xfs_inode_set_eofblocks_tag(ip); |
| 4006 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4007 | xfs_inode_set_cowblocks_tag(ip); |
| 4008 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4009 | return 0; |
| 4010 | |
| 4011 | out_unreserve_blocks: |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4012 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4013 | out_unreserve_quota: |
| 4014 | if (XFS_IS_QUOTA_ON(mp)) |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4015 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4016 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4017 | return error; |
| 4018 | } |
| 4019 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4020 | static int |
| 4021 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4022 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4023 | { |
| 4024 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4025 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4026 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4027 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4028 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4029 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4030 | ASSERT(bma->length > 0); |
| 4031 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4032 | /* |
| 4033 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4034 | * for in this bmap call but that wouldn't be as good. |
| 4035 | */ |
| 4036 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4037 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4038 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4039 | xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4040 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4041 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4042 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4043 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4044 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4048 | * Set the data type being allocated. For the data fork, the first data |
| 4049 | * in the file is treated differently to all other allocations. For the |
| 4050 | * attribute fork, we only need to ensure the allocated range is not on |
| 4051 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4052 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4053 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4054 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4055 | if (whichfork == XFS_DATA_FORK) { |
| 4056 | if (bma->offset == 0) |
| 4057 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4058 | else |
| 4059 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4060 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4061 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4062 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4065 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4066 | |
| 4067 | /* |
| 4068 | * Only want to do the alignment at the eof if it is userdata and |
| 4069 | * allocation length is larger than a stripe unit. |
| 4070 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4071 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4072 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4073 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4074 | if (error) |
| 4075 | return error; |
| 4076 | } |
| 4077 | |
| 4078 | error = xfs_bmap_alloc(bma); |
| 4079 | if (error) |
| 4080 | return error; |
| 4081 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4082 | if (bma->cur) |
| 4083 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4084 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4085 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4086 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4087 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4088 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4089 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4090 | } |
| 4091 | /* |
| 4092 | * Bump the number of extents we've allocated |
| 4093 | * in this call. |
| 4094 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4095 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4096 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4097 | if (bma->cur) |
| 4098 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4099 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4100 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4101 | bma->got.br_startoff = bma->offset; |
| 4102 | bma->got.br_startblock = bma->blkno; |
| 4103 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4104 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4105 | |
| 4106 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4107 | * In the data fork, a wasdelay extent has been initialized, so |
| 4108 | * shouldn't be flagged as unwritten. |
| 4109 | * |
| 4110 | * For the cow fork, however, we convert delalloc reservations |
| 4111 | * (extents allocated for speculative preallocation) to |
| 4112 | * allocated unwritten extents, and only convert the unwritten |
| 4113 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4114 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4115 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4116 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4117 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4118 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4119 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4120 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4121 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4122 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4123 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4124 | whichfork, &bma->icur, &bma->cur, &bma->got, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 4125 | bma->firstblock, bma->dfops, &bma->logflags, |
| 4126 | bma->flags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4127 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4128 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4129 | if (error) |
| 4130 | return error; |
| 4131 | |
| 4132 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4133 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4134 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4135 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4136 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4137 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4138 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4139 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4140 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4141 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4142 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4143 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4144 | return 0; |
| 4145 | } |
| 4146 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4147 | STATIC int |
| 4148 | xfs_bmapi_convert_unwritten( |
| 4149 | struct xfs_bmalloca *bma, |
| 4150 | struct xfs_bmbt_irec *mval, |
| 4151 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4152 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4153 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4154 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4155 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4156 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4157 | int error; |
| 4158 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4159 | /* check if we need to do unwritten->real conversion */ |
| 4160 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4161 | (flags & XFS_BMAPI_PREALLOC)) |
| 4162 | return 0; |
| 4163 | |
| 4164 | /* check if we need to do real->unwritten conversion */ |
| 4165 | if (mval->br_state == XFS_EXT_NORM && |
| 4166 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4167 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4168 | return 0; |
| 4169 | |
| 4170 | /* |
| 4171 | * Modify (by adding) the state flag, if writing. |
| 4172 | */ |
| 4173 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4174 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4175 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4176 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4177 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4178 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4179 | } |
| 4180 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4181 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4182 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4183 | /* |
| 4184 | * Before insertion into the bmbt, zero the range being converted |
| 4185 | * if required. |
| 4186 | */ |
| 4187 | if (flags & XFS_BMAPI_ZERO) { |
| 4188 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4189 | mval->br_blockcount); |
| 4190 | if (error) |
| 4191 | return error; |
| 4192 | } |
| 4193 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4194 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4195 | &bma->icur, &bma->cur, mval, bma->firstblock, |
| 4196 | bma->dfops, &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4197 | /* |
| 4198 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4199 | * path because the conversion might not have done so (e.g., if the |
| 4200 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4201 | * in the transaction for the sake of fsync(), even if nothing has |
| 4202 | * changed, because fsync() will not force the log for this transaction |
| 4203 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4204 | * |
| 4205 | * Note: If we're only converting cow fork extents, there aren't |
| 4206 | * 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] | 4207 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4208 | if (whichfork != XFS_COW_FORK) |
| 4209 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4210 | if (error) |
| 4211 | return error; |
| 4212 | |
| 4213 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4214 | * Update our extent pointer, given that |
| 4215 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4216 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4217 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4218 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4219 | |
| 4220 | /* |
| 4221 | * We may have combined previously unwritten space with written space, |
| 4222 | * so generate another request. |
| 4223 | */ |
| 4224 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4225 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4226 | return 0; |
| 4227 | } |
| 4228 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4229 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4230 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4231 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4232 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4233 | * locking problems. |
| 4234 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | * The returned value in "firstblock" from the first call in a transaction |
| 4236 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4237 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4238 | * the first call in "total"; if no allocation group has that many free |
| 4239 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4240 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4241 | int |
| 4242 | xfs_bmapi_write( |
| 4243 | struct xfs_trans *tp, /* transaction pointer */ |
| 4244 | struct xfs_inode *ip, /* incore inode */ |
| 4245 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4246 | xfs_filblks_t len, /* length to map in file */ |
| 4247 | int flags, /* XFS_BMAPI_... */ |
| 4248 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4249 | controls a.g. for allocs */ |
| 4250 | xfs_extlen_t total, /* total blocks needed */ |
| 4251 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4252 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4253 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4255 | struct xfs_mount *mp = ip->i_mount; |
| 4256 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4257 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4258 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4259 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4260 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4261 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4262 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4263 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4266 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4267 | int orig_flags; /* original flags arg value */ |
| 4268 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4269 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4270 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | |
| 4272 | orig_bno = bno; |
| 4273 | orig_len = len; |
| 4274 | orig_flags = flags; |
| 4275 | orig_mval = mval; |
| 4276 | orig_nmap = *nmap; |
| 4277 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4278 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4281 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4282 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4283 | ASSERT(tp != NULL || |
| 4284 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4285 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4286 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4287 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4288 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4289 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4290 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4291 | /* zeroing is for currently only for data extents, not metadata */ |
| 4292 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4293 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4294 | /* |
| 4295 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4296 | * but it makes no sense to do both at once. This would result in |
| 4297 | * zeroing the unwritten extent twice, but it still being an |
| 4298 | * unwritten extent.... |
| 4299 | */ |
| 4300 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4301 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | if (unlikely(XFS_TEST_ERROR( |
| 4304 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4305 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4306 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4307 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4308 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4312 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4315 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4316 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4317 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4318 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4320 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4322 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4323 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4324 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
| 4327 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4328 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4329 | if (error) |
| 4330 | goto error0; |
| 4331 | } |
| 4332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | n = 0; |
| 4334 | end = bno + len; |
| 4335 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4336 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4337 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4338 | eof = true; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4339 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4340 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4341 | bma.tp = tp; |
| 4342 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4343 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4344 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4345 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4346 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4349 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4350 | |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4351 | /* in hole or beyond EOF? */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4352 | if (eof || bma.got.br_startoff > bno) { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4353 | /* |
| 4354 | * CoW fork conversions should /never/ hit EOF or |
| 4355 | * holes. There should always be something for us |
| 4356 | * to work on. |
| 4357 | */ |
| 4358 | ASSERT(!((flags & XFS_BMAPI_CONVERT) && |
| 4359 | (flags & XFS_BMAPI_COWFORK))); |
| 4360 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4361 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4362 | /* |
| 4363 | * For the COW fork we can reasonably get a |
| 4364 | * request for converting an extent that races |
| 4365 | * with other threads already having converted |
| 4366 | * part of it, as there converting COW to |
| 4367 | * regular blocks is not protected using the |
| 4368 | * IOLOCK. |
| 4369 | */ |
| 4370 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4371 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4372 | error = -EIO; |
| 4373 | goto error0; |
| 4374 | } |
| 4375 | |
| 4376 | if (eof || bno >= end) |
| 4377 | break; |
| 4378 | } else { |
| 4379 | need_alloc = true; |
| 4380 | } |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4381 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4382 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4383 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4384 | |
| 4385 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | * First, deal with the hole before the allocated space |
| 4387 | * that we found, if any. |
| 4388 | */ |
Christoph Hellwig | b121459 | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 4389 | if ((need_alloc || wasdelay) && |
| 4390 | !(flags & XFS_BMAPI_CONVERT_ONLY)) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4391 | bma.eof = eof; |
| 4392 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4393 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4394 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4395 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4396 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4397 | /* |
| 4398 | * There's a 32/64 bit type mismatch between the |
| 4399 | * allocation length request (which can be 64 bits in |
| 4400 | * length) and the bma length request, which is |
| 4401 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4402 | * check for 32-bit overflows and handle them here. |
| 4403 | */ |
| 4404 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4405 | bma.length = MAXEXTLEN; |
| 4406 | else |
| 4407 | bma.length = len; |
| 4408 | |
| 4409 | ASSERT(len > 0); |
| 4410 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4411 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | if (error) |
| 4413 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4414 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4415 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4416 | |
| 4417 | /* |
| 4418 | * If this is a CoW allocation, record the data in |
| 4419 | * the refcount btree for orphan recovery. |
| 4420 | */ |
| 4421 | if (whichfork == XFS_COW_FORK) { |
| 4422 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4423 | bma.blkno, bma.length); |
| 4424 | if (error) |
| 4425 | goto error0; |
| 4426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4428 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4429 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4430 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4431 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4433 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4434 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4435 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4436 | continue; |
| 4437 | if (error) |
| 4438 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4440 | /* update the extent map to return */ |
| 4441 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | /* |
| 4444 | * If we're done, stop now. Stop when we've allocated |
| 4445 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4446 | * the transaction may get too big. |
| 4447 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4448 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4450 | |
| 4451 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4452 | bma.prev = bma.got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4453 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4454 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | /* |
| 4459 | * Transform from btree to extents, give it cur. |
| 4460 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4461 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4462 | int tmp_logflags = 0; |
| 4463 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4464 | ASSERT(bma.cur); |
| 4465 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4467 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | if (error) |
| 4469 | goto error0; |
| 4470 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4473 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4474 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | error0: |
| 4477 | /* |
| 4478 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4479 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4481 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4483 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4484 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4486 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | /* |
| 4488 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4489 | * detecting a case where the data is changed, there's an error, |
| 4490 | * and it's not logged so we don't shutdown when we should. |
| 4491 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4492 | if (bma.logflags) |
| 4493 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4494 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4495 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | if (!error) { |
| 4497 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4498 | XFS_FSB_TO_AGNO(mp, *firstblock) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | XFS_FSB_TO_AGNO(mp, |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4500 | bma.cur->bc_private.b.firstblock)); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4501 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4503 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4505 | } |
| 4506 | if (!error) |
| 4507 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4508 | orig_nmap, *nmap); |
| 4509 | return error; |
| 4510 | } |
| 4511 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4512 | int |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4513 | xfs_bmapi_remap( |
| 4514 | struct xfs_trans *tp, |
| 4515 | struct xfs_inode *ip, |
| 4516 | xfs_fileoff_t bno, |
| 4517 | xfs_filblks_t len, |
| 4518 | xfs_fsblock_t startblock, |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4519 | struct xfs_defer_ops *dfops, |
| 4520 | int flags) |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4521 | { |
| 4522 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4523 | struct xfs_ifork *ifp; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4524 | struct xfs_btree_cur *cur = NULL; |
| 4525 | xfs_fsblock_t firstblock = NULLFSBLOCK; |
| 4526 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4527 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4528 | int whichfork = xfs_bmapi_whichfork(flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4529 | int logflags = 0, error; |
| 4530 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4531 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4532 | ASSERT(len > 0); |
| 4533 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4534 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4535 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC | |
| 4536 | XFS_BMAPI_NORMAP))); |
| 4537 | ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) != |
| 4538 | (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4539 | |
| 4540 | if (unlikely(XFS_TEST_ERROR( |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4541 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4542 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4543 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4544 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4545 | return -EFSCORRUPTED; |
| 4546 | } |
| 4547 | |
| 4548 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4549 | return -EIO; |
| 4550 | |
| 4551 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4552 | error = xfs_iread_extents(tp, ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4553 | if (error) |
| 4554 | return error; |
| 4555 | } |
| 4556 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4557 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4558 | /* make sure we only reflink into a hole. */ |
| 4559 | ASSERT(got.br_startoff > bno); |
| 4560 | ASSERT(got.br_startoff - bno >= len); |
| 4561 | } |
| 4562 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4563 | ip->i_d.di_nblocks += len; |
| 4564 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4565 | |
| 4566 | if (ifp->if_flags & XFS_IFBROOT) { |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4567 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4568 | cur->bc_private.b.firstblock = firstblock; |
| 4569 | cur->bc_private.b.dfops = dfops; |
| 4570 | cur->bc_private.b.flags = 0; |
| 4571 | } |
| 4572 | |
| 4573 | got.br_startoff = bno; |
| 4574 | got.br_startblock = startblock; |
| 4575 | got.br_blockcount = len; |
Darrick J. Wong | 7644bd9 | 2018-05-14 06:34:35 -0700 | [diff] [blame] | 4576 | if (flags & XFS_BMAPI_PREALLOC) |
| 4577 | got.br_state = XFS_EXT_UNWRITTEN; |
| 4578 | else |
| 4579 | got.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4580 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4581 | error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur, |
| 4582 | &cur, &got, &firstblock, dfops, &logflags, flags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4583 | if (error) |
| 4584 | goto error0; |
| 4585 | |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4586 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4587 | int tmp_logflags = 0; |
| 4588 | |
| 4589 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 4590 | &tmp_logflags, whichfork); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4591 | logflags |= tmp_logflags; |
| 4592 | } |
| 4593 | |
| 4594 | error0: |
| 4595 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4596 | logflags &= ~XFS_ILOG_DEXT; |
| 4597 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4598 | logflags &= ~XFS_ILOG_DBROOT; |
| 4599 | |
| 4600 | if (logflags) |
| 4601 | xfs_trans_log_inode(tp, ip, logflags); |
| 4602 | if (cur) { |
| 4603 | xfs_btree_del_cursor(cur, |
| 4604 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4605 | } |
| 4606 | return error; |
| 4607 | } |
| 4608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4610 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4611 | * indlen reservation must be shared across the two new extents that are left |
| 4612 | * behind. |
| 4613 | * |
| 4614 | * Given the original reservation and the worst case indlen for the two new |
| 4615 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4616 | * reservation fairly across the two new extents. If necessary, steal available |
| 4617 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4618 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4619 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4620 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4621 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4622 | xfs_bmap_split_indlen( |
| 4623 | xfs_filblks_t ores, /* original res. */ |
| 4624 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4625 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4626 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4627 | { |
| 4628 | xfs_filblks_t len1 = *indlen1; |
| 4629 | xfs_filblks_t len2 = *indlen2; |
| 4630 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4631 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4632 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4633 | |
| 4634 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4635 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4636 | * indlen for both new extents. |
| 4637 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4638 | if (ores < nres && avail) |
| 4639 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4640 | ores += stolen; |
| 4641 | |
| 4642 | /* nothing else to do if we've satisfied the new reservation */ |
| 4643 | if (ores >= nres) |
| 4644 | return stolen; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4645 | |
| 4646 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4647 | * We can't meet the total required reservation for the two extents. |
| 4648 | * Calculate the percent of the overall shortage between both extents |
| 4649 | * and apply this percentage to each of the requested indlen values. |
| 4650 | * This distributes the shortage fairly and reduces the chances that one |
| 4651 | * of the two extents is left with nothing when extents are repeatedly |
| 4652 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4653 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4654 | resfactor = (ores * 100); |
| 4655 | do_div(resfactor, nres); |
| 4656 | len1 *= resfactor; |
| 4657 | do_div(len1, 100); |
| 4658 | len2 *= resfactor; |
| 4659 | do_div(len2, 100); |
| 4660 | ASSERT(len1 + len2 <= ores); |
| 4661 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4662 | |
| 4663 | /* |
| 4664 | * Hand out the remainder to each extent. If one of the two reservations |
| 4665 | * is zero, we want to make sure that one gets a block first. The loop |
| 4666 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4667 | * is zero. |
| 4668 | */ |
| 4669 | ores -= (len1 + len2); |
| 4670 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4671 | if (ores && !len2 && *indlen2) { |
| 4672 | len2++; |
| 4673 | ores--; |
| 4674 | } |
| 4675 | while (ores) { |
| 4676 | if (len1 < *indlen1) { |
| 4677 | len1++; |
| 4678 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4679 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4680 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4681 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4682 | if (len2 < *indlen2) { |
| 4683 | len2++; |
| 4684 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | *indlen1 = len1; |
| 4689 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4690 | |
| 4691 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4692 | } |
| 4693 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4694 | int |
| 4695 | xfs_bmap_del_extent_delay( |
| 4696 | struct xfs_inode *ip, |
| 4697 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4698 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4699 | struct xfs_bmbt_irec *got, |
| 4700 | struct xfs_bmbt_irec *del) |
| 4701 | { |
| 4702 | struct xfs_mount *mp = ip->i_mount; |
| 4703 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4704 | struct xfs_bmbt_irec new; |
| 4705 | int64_t da_old, da_new, da_diff = 0; |
| 4706 | xfs_fileoff_t del_endoff, got_endoff; |
| 4707 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4708 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4709 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4710 | bool isrt; |
| 4711 | |
| 4712 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4713 | |
| 4714 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4715 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4716 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4717 | da_old = startblockval(got->br_startblock); |
| 4718 | da_new = 0; |
| 4719 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4720 | ASSERT(del->br_blockcount > 0); |
| 4721 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4722 | ASSERT(got_endoff >= del_endoff); |
| 4723 | |
| 4724 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4725 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4726 | |
| 4727 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4728 | xfs_mod_frextents(mp, rtexts); |
| 4729 | } |
| 4730 | |
| 4731 | /* |
| 4732 | * Update the inode delalloc counter now and wait to update the |
| 4733 | * sb counters as we might have to borrow some blocks for the |
| 4734 | * indirect block accounting. |
| 4735 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4736 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4737 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4738 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4739 | if (error) |
| 4740 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4741 | ip->i_delayed_blks -= del->br_blockcount; |
| 4742 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4743 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4744 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4745 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4746 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4747 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4748 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4749 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4750 | /* |
| 4751 | * Matches the whole extent. Delete the entry. |
| 4752 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4753 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4754 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4755 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4756 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4757 | /* |
| 4758 | * Deleting the first part of the extent. |
| 4759 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4760 | got->br_startoff = del_endoff; |
| 4761 | got->br_blockcount -= del->br_blockcount; |
| 4762 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4763 | got->br_blockcount), da_old); |
| 4764 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4765 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4766 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4767 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4768 | /* |
| 4769 | * Deleting the last part of the extent. |
| 4770 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4771 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4772 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4773 | got->br_blockcount), da_old); |
| 4774 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4775 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4776 | break; |
| 4777 | case 0: |
| 4778 | /* |
| 4779 | * Deleting the middle of the extent. |
| 4780 | * |
| 4781 | * Distribute the original indlen reservation across the two new |
| 4782 | * extents. Steal blocks from the deleted extent if necessary. |
| 4783 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4784 | * Warn if either of the new indlen reservations is zero as this |
| 4785 | * can lead to delalloc problems. |
| 4786 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4787 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4788 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4789 | |
| 4790 | new.br_blockcount = got_endoff - del_endoff; |
| 4791 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4792 | |
| 4793 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4794 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4795 | del->br_blockcount); |
| 4796 | |
| 4797 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4798 | |
| 4799 | new.br_startoff = del_endoff; |
| 4800 | new.br_state = got->br_state; |
| 4801 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4802 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4803 | xfs_iext_update_extent(ip, state, icur, got); |
| 4804 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4805 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4806 | |
| 4807 | da_new = got_indlen + new_indlen - stolen; |
| 4808 | del->br_blockcount -= stolen; |
| 4809 | break; |
| 4810 | } |
| 4811 | |
| 4812 | ASSERT(da_old >= da_new); |
| 4813 | da_diff = da_old - da_new; |
| 4814 | if (!isrt) |
| 4815 | da_diff += del->br_blockcount; |
| 4816 | if (da_diff) |
| 4817 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4818 | return error; |
| 4819 | } |
| 4820 | |
| 4821 | void |
| 4822 | xfs_bmap_del_extent_cow( |
| 4823 | struct xfs_inode *ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4824 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4825 | struct xfs_bmbt_irec *got, |
| 4826 | struct xfs_bmbt_irec *del) |
| 4827 | { |
| 4828 | struct xfs_mount *mp = ip->i_mount; |
| 4829 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4830 | struct xfs_bmbt_irec new; |
| 4831 | xfs_fileoff_t del_endoff, got_endoff; |
| 4832 | int state = BMAP_COWFORK; |
| 4833 | |
| 4834 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4835 | |
| 4836 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4837 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4838 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4839 | ASSERT(del->br_blockcount > 0); |
| 4840 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4841 | ASSERT(got_endoff >= del_endoff); |
| 4842 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4843 | |
| 4844 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4845 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4846 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4847 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4848 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4849 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4850 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4851 | /* |
| 4852 | * Matches the whole extent. Delete the entry. |
| 4853 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4854 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4855 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4856 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4857 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4858 | /* |
| 4859 | * Deleting the first part of the extent. |
| 4860 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4861 | got->br_startoff = del_endoff; |
| 4862 | got->br_blockcount -= del->br_blockcount; |
| 4863 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4864 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4865 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4866 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4867 | /* |
| 4868 | * Deleting the last part of the extent. |
| 4869 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4870 | got->br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4871 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4872 | break; |
| 4873 | case 0: |
| 4874 | /* |
| 4875 | * Deleting the middle of the extent. |
| 4876 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4877 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4878 | |
| 4879 | new.br_startoff = del_endoff; |
| 4880 | new.br_blockcount = got_endoff - del_endoff; |
| 4881 | new.br_state = got->br_state; |
| 4882 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4883 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4884 | xfs_iext_update_extent(ip, state, icur, got); |
| 4885 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4886 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4887 | break; |
| 4888 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 4889 | ip->i_delayed_blks -= del->br_blockcount; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4890 | } |
| 4891 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4892 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4893 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4894 | * after removing space. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4895 | */ |
| 4896 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4897 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4898 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4899 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4900 | struct xfs_iext_cursor *icur, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4901 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4902 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4903 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4904 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4905 | int whichfork, /* data or attr fork */ |
| 4906 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4907 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4908 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4909 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4910 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4911 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4912 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4913 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4914 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4915 | int i; /* temp state */ |
| 4916 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4917 | xfs_mount_t *mp; /* mount structure */ |
| 4918 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4919 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4920 | /* REFERENCED */ |
| 4921 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4922 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4923 | struct xfs_bmbt_irec old; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4924 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4925 | mp = ip->i_mount; |
| 4926 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4927 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4928 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4929 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4930 | xfs_iext_get_extent(ifp, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4931 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4932 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4933 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4934 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4935 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4936 | qfield = 0; |
| 4937 | error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4938 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4939 | /* |
| 4940 | * If it's the case where the directory code is running with no block |
| 4941 | * reservation, and the deleted block is in the middle of its extent, |
| 4942 | * and the resulting insert of an extent would cause transformation to |
| 4943 | * btree format, then reject it. The calling code will then swap blocks |
| 4944 | * around instead. We have to do this now, rather than waiting for the |
| 4945 | * conversion to btree format, since the transaction will be dirty then. |
| 4946 | */ |
| 4947 | if (tp->t_blk_res == 0 && |
| 4948 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 4949 | XFS_IFORK_NEXTENTS(ip, whichfork) >= |
| 4950 | XFS_IFORK_MAXEXT(ip, whichfork) && |
| 4951 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 4952 | return -ENOSPC; |
| 4953 | |
| 4954 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4955 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4956 | xfs_fsblock_t bno; |
| 4957 | xfs_filblks_t len; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 4958 | xfs_extlen_t mod; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4959 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 4960 | bno = div_u64_rem(del->br_startblock, mp->m_sb.sb_rextsize, |
| 4961 | &mod); |
| 4962 | ASSERT(mod == 0); |
| 4963 | len = div_u64_rem(del->br_blockcount, mp->m_sb.sb_rextsize, |
| 4964 | &mod); |
| 4965 | ASSERT(mod == 0); |
| 4966 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4967 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4968 | if (error) |
| 4969 | goto done; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4970 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4971 | nblks = len * mp->m_sb.sb_rextsize; |
| 4972 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4973 | } else { |
| 4974 | do_fx = 1; |
| 4975 | nblks = del->br_blockcount; |
| 4976 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4977 | } |
| 4978 | |
| 4979 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4980 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4981 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4982 | if (error) |
| 4983 | goto done; |
| 4984 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4985 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 4986 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4987 | if (got.br_startoff == del->br_startoff) |
| 4988 | state |= BMAP_LEFT_FILLING; |
| 4989 | if (got_endoff == del_endoff) |
| 4990 | state |= BMAP_RIGHT_FILLING; |
| 4991 | |
| 4992 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4993 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4994 | /* |
| 4995 | * Matches the whole extent. Delete the entry. |
| 4996 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4997 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4998 | xfs_iext_prev(ifp, icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4999 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5000 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5001 | flags |= XFS_ILOG_CORE; |
| 5002 | if (!cur) { |
| 5003 | flags |= xfs_ilog_fext(whichfork); |
| 5004 | break; |
| 5005 | } |
| 5006 | if ((error = xfs_btree_delete(cur, &i))) |
| 5007 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5008 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5009 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5010 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5011 | /* |
| 5012 | * Deleting the first part of the extent. |
| 5013 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5014 | got.br_startoff = del_endoff; |
| 5015 | got.br_startblock = del_endblock; |
| 5016 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5017 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5018 | if (!cur) { |
| 5019 | flags |= xfs_ilog_fext(whichfork); |
| 5020 | break; |
| 5021 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5022 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5023 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5024 | goto done; |
| 5025 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5026 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5027 | /* |
| 5028 | * Deleting the last part of the extent. |
| 5029 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5030 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5031 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5032 | if (!cur) { |
| 5033 | flags |= xfs_ilog_fext(whichfork); |
| 5034 | break; |
| 5035 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5036 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5037 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5038 | goto done; |
| 5039 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5040 | case 0: |
| 5041 | /* |
| 5042 | * Deleting the middle of the extent. |
| 5043 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5044 | old = got; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5045 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5046 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5047 | xfs_iext_update_extent(ip, state, icur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5048 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5049 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5050 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5051 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5052 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5053 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5054 | flags |= XFS_ILOG_CORE; |
| 5055 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5056 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5057 | if (error) |
| 5058 | goto done; |
| 5059 | error = xfs_btree_increment(cur, 0, &i); |
| 5060 | if (error) |
| 5061 | goto done; |
| 5062 | cur->bc_rec.b = new; |
| 5063 | error = xfs_btree_insert(cur, &i); |
| 5064 | if (error && error != -ENOSPC) |
| 5065 | goto done; |
| 5066 | /* |
| 5067 | * If get no-space back from btree insert, it tried a |
| 5068 | * split, and we have a zero block reservation. Fix up |
| 5069 | * our state and return the error. |
| 5070 | */ |
| 5071 | if (error == -ENOSPC) { |
| 5072 | /* |
| 5073 | * Reset the cursor, don't trust it after any |
| 5074 | * insert operation. |
| 5075 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5076 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5077 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5078 | goto done; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5079 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5080 | /* |
| 5081 | * Update the btree record back |
| 5082 | * to the original value. |
| 5083 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5084 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5085 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5086 | goto done; |
| 5087 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5088 | * Reset the extent record back |
| 5089 | * to the original value. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5090 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5091 | xfs_iext_update_extent(ip, state, icur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5092 | flags = 0; |
| 5093 | error = -ENOSPC; |
| 5094 | goto done; |
| 5095 | } |
| 5096 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5097 | } else |
| 5098 | flags |= xfs_ilog_fext(whichfork); |
| 5099 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5100 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5101 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5102 | xfs_iext_insert(ip, icur, &new, state); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5103 | break; |
| 5104 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5105 | |
| 5106 | /* remove reverse mapping */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5107 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5108 | if (error) |
| 5109 | goto done; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5110 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5111 | /* |
| 5112 | * If we need to, add to list of extents to delete. |
| 5113 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5114 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5115 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5116 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5117 | if (error) |
| 5118 | goto done; |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5119 | } else { |
Brian Foster | 4e52933 | 2018-05-10 09:35:42 -0700 | [diff] [blame] | 5120 | __xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5121 | del->br_blockcount, NULL, |
| 5122 | (bflags & XFS_BMAPI_NODISCARD) || |
| 5123 | del->br_state == XFS_EXT_UNWRITTEN); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5124 | } |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5125 | } |
| 5126 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5127 | /* |
| 5128 | * Adjust inode # blocks in the file. |
| 5129 | */ |
| 5130 | if (nblks) |
| 5131 | ip->i_d.di_nblocks -= nblks; |
| 5132 | /* |
| 5133 | * Adjust quota data. |
| 5134 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5135 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5136 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5137 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5138 | done: |
| 5139 | *logflagsp = flags; |
| 5140 | return error; |
| 5141 | } |
| 5142 | |
| 5143 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | * Unmap (remove) blocks from a file. |
| 5145 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5146 | * that value. If not all extents in the block range can be removed then |
| 5147 | * *done is set. |
| 5148 | */ |
| 5149 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5150 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | xfs_trans_t *tp, /* transaction pointer */ |
| 5152 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5153 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5154 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | int flags, /* misc flags */ |
| 5156 | xfs_extnum_t nexts, /* number of extents max */ |
| 5157 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5158 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5159 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | { |
| 5161 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5162 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | int error; /* error return value */ |
| 5164 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5165 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5167 | int isrt; /* freeing in rt area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | int logflags; /* transaction logging flags */ |
| 5169 | xfs_extlen_t mod; /* rt extent offset */ |
| 5170 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | int tmp_logflags; /* partial logging flags */ |
| 5172 | int wasdel; /* was a delayed alloc extent */ |
| 5173 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5175 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5176 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5177 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5178 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5179 | struct xfs_iext_cursor icur; |
| 5180 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5182 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5183 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5184 | whichfork = xfs_bmapi_whichfork(flags); |
| 5185 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5187 | if (unlikely( |
| 5188 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5189 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5190 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5191 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5192 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | } |
| 5194 | mp = ip->i_mount; |
| 5195 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5196 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5197 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5198 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | ASSERT(len > 0); |
| 5200 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5201 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5202 | /* |
| 5203 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5204 | * blowing out the transaction with a mix of EFIs and reflink |
| 5205 | * adjustments. |
| 5206 | */ |
Darrick J. Wong | 8c57b88 | 2017-12-10 18:03:53 -0800 | [diff] [blame] | 5207 | if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5208 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5209 | else |
| 5210 | max_len = len; |
| 5211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5213 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5214 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5215 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5216 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | return 0; |
| 5218 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5219 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5220 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5221 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5222 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5223 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5224 | *rlen = 0; |
| 5225 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5227 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | logflags = 0; |
| 5230 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5231 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5232 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5234 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | cur->bc_private.b.flags = 0; |
| 5236 | } else |
| 5237 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5238 | |
| 5239 | if (isrt) { |
| 5240 | /* |
| 5241 | * Synchronize by locking the bitmap inode. |
| 5242 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5243 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5244 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5245 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5246 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5247 | } |
| 5248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | extno = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5250 | while (end != (xfs_fileoff_t)-1 && end >= start && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5251 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5253 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | * Just back up to the previous extent, if so. |
| 5255 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5256 | if (got.br_startoff > end && |
| 5257 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5258 | done = true; |
| 5259 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | } |
| 5261 | /* |
| 5262 | * Is the last block of this extent before the range |
| 5263 | * we're supposed to delete? If so, we're done. |
| 5264 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5265 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5267 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | break; |
| 5269 | /* |
| 5270 | * Then deal with the (possibly delayed) allocated space |
| 5271 | * we found. |
| 5272 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5274 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5275 | |
| 5276 | /* |
| 5277 | * Make sure we don't touch multiple AGF headers out of order |
| 5278 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5279 | */ |
| 5280 | if (!wasdel) { |
| 5281 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5282 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5283 | break; |
| 5284 | prev_agno = agno; |
| 5285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | if (got.br_startoff < start) { |
| 5287 | del.br_startoff = start; |
| 5288 | del.br_blockcount -= start - got.br_startoff; |
| 5289 | if (!wasdel) |
| 5290 | del.br_startblock += start - got.br_startoff; |
| 5291 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5292 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5293 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5294 | |
| 5295 | /* How much can we safely unmap? */ |
| 5296 | if (max_len < del.br_blockcount) { |
| 5297 | del.br_startoff += del.br_blockcount - max_len; |
| 5298 | if (!wasdel) |
| 5299 | del.br_startblock += del.br_blockcount - max_len; |
| 5300 | del.br_blockcount = max_len; |
| 5301 | } |
| 5302 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5303 | if (!isrt) |
| 5304 | goto delete; |
| 5305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | sum = del.br_startblock + del.br_blockcount; |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5307 | div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod); |
| 5308 | if (mod) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | /* |
| 5310 | * Realtime extent not lined up at the end. |
| 5311 | * The extent could have been split into written |
| 5312 | * and unwritten pieces, or we could just be |
| 5313 | * unmapping part of it. But we can't really |
| 5314 | * get rid of part of a realtime extent. |
| 5315 | */ |
| 5316 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5317 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | /* |
| 5319 | * This piece is unwritten, or we're not |
| 5320 | * using unwritten extents. Skip over it. |
| 5321 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5322 | ASSERT(end >= mod); |
| 5323 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | del.br_blockcount : mod; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5325 | if (end < got.br_startoff && |
| 5326 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5327 | done = true; |
| 5328 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5329 | } |
| 5330 | continue; |
| 5331 | } |
| 5332 | /* |
| 5333 | * It's written, turn it unwritten. |
| 5334 | * This is better than zeroing it. |
| 5335 | */ |
| 5336 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5337 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | /* |
| 5339 | * If this spans a realtime extent boundary, |
| 5340 | * chop it back to the start of the one we end at. |
| 5341 | */ |
| 5342 | if (del.br_blockcount > mod) { |
| 5343 | del.br_startoff += del.br_blockcount - mod; |
| 5344 | del.br_startblock += del.br_blockcount - mod; |
| 5345 | del.br_blockcount = mod; |
| 5346 | } |
| 5347 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5348 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5349 | whichfork, &icur, &cur, &del, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5350 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 | if (error) |
| 5352 | goto error0; |
| 5353 | goto nodelete; |
| 5354 | } |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5355 | div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod); |
| 5356 | if (mod) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | /* |
| 5358 | * Realtime extent is lined up at the end but not |
| 5359 | * at the front. We'll get rid of full extents if |
| 5360 | * we can. |
| 5361 | */ |
| 5362 | mod = mp->m_sb.sb_rextsize - mod; |
| 5363 | if (del.br_blockcount > mod) { |
| 5364 | del.br_blockcount -= mod; |
| 5365 | del.br_startoff += mod; |
| 5366 | del.br_startblock += mod; |
| 5367 | } else if ((del.br_startoff == start && |
| 5368 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5369 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5370 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | /* |
| 5372 | * Can't make it unwritten. There isn't |
| 5373 | * a full extent here so just skip it. |
| 5374 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5375 | ASSERT(end >= del.br_blockcount); |
| 5376 | end -= del.br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5377 | if (got.br_startoff > end && |
| 5378 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5379 | done = true; |
| 5380 | break; |
| 5381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | continue; |
| 5383 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5384 | struct xfs_bmbt_irec prev; |
| 5385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | /* |
| 5387 | * This one is already unwritten. |
| 5388 | * It must have a written left neighbor. |
| 5389 | * Unwrite the killed part of that one and |
| 5390 | * try again. |
| 5391 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5392 | if (!xfs_iext_prev_extent(ifp, &icur, &prev)) |
| 5393 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5395 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | ASSERT(del.br_startblock == |
| 5397 | prev.br_startblock + prev.br_blockcount); |
| 5398 | if (prev.br_startoff < start) { |
| 5399 | mod = start - prev.br_startoff; |
| 5400 | prev.br_blockcount -= mod; |
| 5401 | prev.br_startblock += mod; |
| 5402 | prev.br_startoff = start; |
| 5403 | } |
| 5404 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5405 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5406 | ip, whichfork, &icur, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5407 | &prev, firstblock, dfops, |
| 5408 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | if (error) |
| 5410 | goto error0; |
| 5411 | goto nodelete; |
| 5412 | } else { |
| 5413 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5414 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5415 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5416 | ip, whichfork, &icur, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5417 | &del, firstblock, dfops, |
| 5418 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | if (error) |
| 5420 | goto error0; |
| 5421 | goto nodelete; |
| 5422 | } |
| 5423 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5424 | |
Dave Chinner | 0703a8e | 2018-06-08 09:54:22 -0700 | [diff] [blame] | 5425 | delete: |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5426 | if (wasdel) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5427 | error = xfs_bmap_del_extent_delay(ip, whichfork, &icur, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5428 | &got, &del); |
| 5429 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5430 | error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5431 | cur, &del, &tmp_logflags, whichfork, |
| 5432 | flags); |
| 5433 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5434 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | if (error) |
| 5437 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5438 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5439 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5440 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | /* |
| 5443 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5445 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5446 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
| 5447 | (got.br_startoff > end && |
| 5448 | !xfs_iext_prev_extent(ifp, &icur, &got))) { |
| 5449 | done = true; |
| 5450 | break; |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | extno++; |
| 5453 | } |
| 5454 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5455 | if (done || end == (xfs_fileoff_t)-1 || end < start) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5456 | *rlen = 0; |
| 5457 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5458 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | /* |
| 5461 | * Convert to a btree if necessary. |
| 5462 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5463 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5465 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | &cur, 0, &tmp_logflags, whichfork); |
| 5467 | logflags |= tmp_logflags; |
| 5468 | if (error) |
| 5469 | goto error0; |
| 5470 | } |
| 5471 | /* |
| 5472 | * transform from btree to extents, give it cur |
| 5473 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5474 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | ASSERT(cur != NULL); |
| 5476 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5477 | whichfork); |
| 5478 | logflags |= tmp_logflags; |
| 5479 | if (error) |
| 5480 | goto error0; |
| 5481 | } |
| 5482 | /* |
| 5483 | * transform from extents to local? |
| 5484 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | error = 0; |
| 5486 | error0: |
| 5487 | /* |
| 5488 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5489 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5491 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5493 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5494 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5496 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | /* |
| 5498 | * Log inode even in the error case, if the transaction |
| 5499 | * is dirty we'll need to shut down the filesystem. |
| 5500 | */ |
| 5501 | if (logflags) |
| 5502 | xfs_trans_log_inode(tp, ip, logflags); |
| 5503 | if (cur) { |
| 5504 | if (!error) { |
| 5505 | *firstblock = cur->bc_private.b.firstblock; |
| 5506 | cur->bc_private.b.allocated = 0; |
| 5507 | } |
| 5508 | xfs_btree_del_cursor(cur, |
| 5509 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5510 | } |
| 5511 | return error; |
| 5512 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5513 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5514 | /* Unmap a range of a file. */ |
| 5515 | int |
| 5516 | xfs_bunmapi( |
| 5517 | xfs_trans_t *tp, |
| 5518 | struct xfs_inode *ip, |
| 5519 | xfs_fileoff_t bno, |
| 5520 | xfs_filblks_t len, |
| 5521 | int flags, |
| 5522 | xfs_extnum_t nexts, |
| 5523 | xfs_fsblock_t *firstblock, |
| 5524 | struct xfs_defer_ops *dfops, |
| 5525 | int *done) |
| 5526 | { |
| 5527 | int error; |
| 5528 | |
| 5529 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5530 | dfops); |
| 5531 | *done = (len == 0); |
| 5532 | return error; |
| 5533 | } |
| 5534 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5535 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5536 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5537 | * extent that precedes the target hole of the shift. |
| 5538 | */ |
| 5539 | STATIC bool |
| 5540 | xfs_bmse_can_merge( |
| 5541 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5542 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5543 | xfs_fileoff_t shift) /* shift fsb */ |
| 5544 | { |
| 5545 | xfs_fileoff_t startoff; |
| 5546 | |
| 5547 | startoff = got->br_startoff - shift; |
| 5548 | |
| 5549 | /* |
| 5550 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5551 | * the preceding extent. |
| 5552 | */ |
| 5553 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5554 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5555 | (left->br_state != got->br_state) || |
| 5556 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5557 | return false; |
| 5558 | |
| 5559 | return true; |
| 5560 | } |
| 5561 | |
| 5562 | /* |
| 5563 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5564 | * hole in the file. If an extent shift would result in the extent being fully |
| 5565 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5566 | * the preceding extent rather than do the shift. |
| 5567 | * |
| 5568 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5569 | * with the provided extents via xfs_bmse_can_merge(). |
| 5570 | */ |
| 5571 | STATIC int |
| 5572 | xfs_bmse_merge( |
| 5573 | struct xfs_inode *ip, |
| 5574 | int whichfork, |
| 5575 | xfs_fileoff_t shift, /* shift fsb */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5576 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5577 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5578 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5579 | struct xfs_btree_cur *cur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5580 | int *logflags, /* output */ |
| 5581 | struct xfs_defer_ops *dfops) |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5582 | { |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5583 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5584 | xfs_filblks_t blockcount; |
| 5585 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5586 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5587 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5588 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5589 | |
| 5590 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5591 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5592 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5593 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5594 | new = *left; |
| 5595 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5596 | |
| 5597 | /* |
| 5598 | * Update the on-disk extent count, the btree if necessary and log the |
| 5599 | * inode. |
| 5600 | */ |
| 5601 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5602 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5603 | *logflags |= XFS_ILOG_CORE; |
| 5604 | if (!cur) { |
| 5605 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5606 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5610 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5611 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5612 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5613 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5614 | |
| 5615 | error = xfs_btree_delete(cur, &i); |
| 5616 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5617 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5618 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5619 | |
| 5620 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5621 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5622 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5623 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5624 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5625 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5626 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5627 | if (error) |
| 5628 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5629 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5630 | done: |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5631 | xfs_iext_remove(ip, icur, 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5632 | xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur); |
| 5633 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5634 | &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5635 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5636 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5637 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got); |
| 5638 | if (error) |
| 5639 | return error; |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5640 | memcpy(&new, got, sizeof(new)); |
| 5641 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5642 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5643 | } |
| 5644 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5645 | static int |
| 5646 | xfs_bmap_shift_update_extent( |
| 5647 | struct xfs_inode *ip, |
| 5648 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5649 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5650 | struct xfs_bmbt_irec *got, |
| 5651 | struct xfs_btree_cur *cur, |
| 5652 | int *logflags, |
| 5653 | struct xfs_defer_ops *dfops, |
| 5654 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5655 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5656 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5657 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5658 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5659 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5660 | *logflags |= XFS_ILOG_CORE; |
| 5661 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5662 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5663 | |
| 5664 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5665 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5666 | if (error) |
| 5667 | return error; |
| 5668 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
| 5669 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5670 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5671 | if (error) |
| 5672 | return error; |
| 5673 | } else { |
| 5674 | *logflags |= XFS_ILOG_DEXT; |
| 5675 | } |
| 5676 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5677 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5678 | got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5679 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5680 | /* update reverse mapping */ |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5681 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5682 | if (error) |
| 5683 | return error; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5684 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5685 | } |
| 5686 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5687 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5688 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5689 | struct xfs_trans *tp, |
| 5690 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5691 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5692 | xfs_fileoff_t offset_shift_fsb, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5693 | bool *done, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5694 | xfs_fsblock_t *firstblock, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5695 | struct xfs_defer_ops *dfops) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5696 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5697 | int whichfork = XFS_DATA_FORK; |
| 5698 | struct xfs_mount *mp = ip->i_mount; |
| 5699 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5700 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5701 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5702 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5703 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5704 | int error = 0; |
| 5705 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5706 | |
| 5707 | if (unlikely(XFS_TEST_ERROR( |
| 5708 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5709 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5710 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5711 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5712 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5713 | } |
| 5714 | |
| 5715 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5716 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5717 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5718 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5719 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5720 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5721 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5722 | if (error) |
| 5723 | return error; |
| 5724 | } |
| 5725 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5726 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5727 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5728 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5729 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5730 | cur->bc_private.b.flags = 0; |
| 5731 | } |
| 5732 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5733 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5734 | *done = true; |
| 5735 | goto del_cursor; |
| 5736 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5737 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5738 | del_cursor); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5739 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5740 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5741 | if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5742 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5743 | error = -EINVAL; |
| 5744 | goto del_cursor; |
| 5745 | } |
| 5746 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5747 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
| 5748 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5749 | &icur, &got, &prev, cur, &logflags, |
| 5750 | dfops); |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5751 | if (error) |
| 5752 | goto del_cursor; |
| 5753 | goto done; |
| 5754 | } |
| 5755 | } else { |
| 5756 | if (got.br_startoff < offset_shift_fsb) { |
| 5757 | error = -EINVAL; |
| 5758 | goto del_cursor; |
| 5759 | } |
| 5760 | } |
| 5761 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5762 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
| 5763 | &logflags, dfops, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5764 | if (error) |
| 5765 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5766 | |
Christoph Hellwig | 4263036 | 2017-11-03 10:34:41 -0700 | [diff] [blame] | 5767 | done: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5768 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
| 5769 | *done = true; |
| 5770 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5771 | } |
| 5772 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5773 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5774 | del_cursor: |
| 5775 | if (cur) |
| 5776 | xfs_btree_del_cursor(cur, |
| 5777 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5778 | if (logflags) |
| 5779 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5780 | return error; |
| 5781 | } |
| 5782 | |
Darrick J. Wong | f62cb48 | 2018-06-21 23:26:57 -0700 | [diff] [blame^] | 5783 | /* Make sure we won't be right-shifting an extent past the maximum bound. */ |
| 5784 | int |
| 5785 | xfs_bmap_can_insert_extents( |
| 5786 | struct xfs_inode *ip, |
| 5787 | xfs_fileoff_t off, |
| 5788 | xfs_fileoff_t shift) |
| 5789 | { |
| 5790 | struct xfs_bmbt_irec got; |
| 5791 | int is_empty; |
| 5792 | int error = 0; |
| 5793 | |
| 5794 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5795 | |
| 5796 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 5797 | return -EIO; |
| 5798 | |
| 5799 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 5800 | error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty); |
| 5801 | if (!error && !is_empty && got.br_startoff >= off && |
| 5802 | ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff) |
| 5803 | error = -EINVAL; |
| 5804 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 5805 | |
| 5806 | return error; |
| 5807 | } |
| 5808 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5809 | int |
| 5810 | xfs_bmap_insert_extents( |
| 5811 | struct xfs_trans *tp, |
| 5812 | struct xfs_inode *ip, |
| 5813 | xfs_fileoff_t *next_fsb, |
| 5814 | xfs_fileoff_t offset_shift_fsb, |
| 5815 | bool *done, |
| 5816 | xfs_fileoff_t stop_fsb, |
| 5817 | xfs_fsblock_t *firstblock, |
| 5818 | struct xfs_defer_ops *dfops) |
| 5819 | { |
| 5820 | int whichfork = XFS_DATA_FORK; |
| 5821 | struct xfs_mount *mp = ip->i_mount; |
| 5822 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5823 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5824 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5825 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5826 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5827 | int error = 0; |
| 5828 | int logflags = 0; |
| 5829 | |
| 5830 | if (unlikely(XFS_TEST_ERROR( |
| 5831 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5832 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5833 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
| 5834 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 5835 | return -EFSCORRUPTED; |
| 5836 | } |
| 5837 | |
| 5838 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5839 | return -EIO; |
| 5840 | |
| 5841 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5842 | |
| 5843 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5844 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5845 | if (error) |
| 5846 | return error; |
| 5847 | } |
| 5848 | |
| 5849 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5850 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5851 | cur->bc_private.b.firstblock = *firstblock; |
| 5852 | cur->bc_private.b.dfops = dfops; |
| 5853 | cur->bc_private.b.flags = 0; |
| 5854 | } |
| 5855 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5856 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5857 | xfs_iext_last(ifp, &icur); |
| 5858 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5859 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5860 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5861 | goto del_cursor; |
| 5862 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5863 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5864 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5865 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5866 | goto del_cursor; |
| 5867 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5868 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5869 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5870 | del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5871 | |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5872 | if (stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5873 | error = -EIO; |
| 5874 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5875 | } |
| 5876 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5877 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5878 | if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5879 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5880 | error = -EINVAL; |
| 5881 | goto del_cursor; |
| 5882 | } |
| 5883 | |
| 5884 | /* |
| 5885 | * Unlike a left shift (which involves a hole punch), a right |
| 5886 | * shift does not modify extent neighbors in any way. We should |
| 5887 | * never find mergeable extents in this scenario. Check anyways |
| 5888 | * and warn if we encounter two extents that could be one. |
| 5889 | */ |
| 5890 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5891 | WARN_ON_ONCE(1); |
| 5892 | } |
| 5893 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5894 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
| 5895 | &logflags, dfops, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5896 | if (error) |
| 5897 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5898 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5899 | if (!xfs_iext_prev_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5900 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5901 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5902 | goto del_cursor; |
| 5903 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5904 | |
| 5905 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5906 | del_cursor: |
| 5907 | if (cur) |
| 5908 | xfs_btree_del_cursor(cur, |
| 5909 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5910 | if (logflags) |
| 5911 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5912 | return error; |
| 5913 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5914 | |
| 5915 | /* |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5916 | * Splits an extent into two extents at split_fsb block such that it is the |
| 5917 | * first block of the current_ext. @ext is a target extent to be split. |
| 5918 | * @split_fsb is a block where the extents is split. If split_fsb lies in a |
| 5919 | * hole or the first block of extents, just return 0. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5920 | */ |
| 5921 | STATIC int |
| 5922 | xfs_bmap_split_extent_at( |
| 5923 | struct xfs_trans *tp, |
| 5924 | struct xfs_inode *ip, |
| 5925 | xfs_fileoff_t split_fsb, |
| 5926 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5927 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5928 | { |
| 5929 | int whichfork = XFS_DATA_FORK; |
| 5930 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5931 | struct xfs_bmbt_irec got; |
| 5932 | struct xfs_bmbt_irec new; /* split extent */ |
| 5933 | struct xfs_mount *mp = ip->i_mount; |
| 5934 | struct xfs_ifork *ifp; |
| 5935 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5936 | struct xfs_iext_cursor icur; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5937 | int error = 0; |
| 5938 | int logflags = 0; |
| 5939 | int i = 0; |
| 5940 | |
| 5941 | if (unlikely(XFS_TEST_ERROR( |
| 5942 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5943 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5944 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5945 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5946 | XFS_ERRLEVEL_LOW, mp); |
| 5947 | return -EFSCORRUPTED; |
| 5948 | } |
| 5949 | |
| 5950 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5951 | return -EIO; |
| 5952 | |
| 5953 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5954 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5955 | /* Read in all the extents */ |
| 5956 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5957 | if (error) |
| 5958 | return error; |
| 5959 | } |
| 5960 | |
| 5961 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5962 | * 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] | 5963 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5964 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5965 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5966 | return 0; |
| 5967 | |
| 5968 | gotblkcnt = split_fsb - got.br_startoff; |
| 5969 | new.br_startoff = split_fsb; |
| 5970 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5971 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5972 | new.br_state = got.br_state; |
| 5973 | |
| 5974 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5975 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5976 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5977 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5978 | cur->bc_private.b.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5979 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5980 | if (error) |
| 5981 | goto del_cursor; |
| 5982 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5983 | } |
| 5984 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5985 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5986 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, |
| 5987 | &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5988 | |
| 5989 | logflags = XFS_ILOG_CORE; |
| 5990 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5991 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5992 | if (error) |
| 5993 | goto del_cursor; |
| 5994 | } else |
| 5995 | logflags |= XFS_ILOG_DEXT; |
| 5996 | |
| 5997 | /* Add new extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5998 | xfs_iext_next(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5999 | xfs_iext_insert(ip, &icur, &new, 0); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6000 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 6001 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 6002 | |
| 6003 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 6004 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6005 | if (error) |
| 6006 | goto del_cursor; |
| 6007 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6008 | error = xfs_btree_insert(cur, &i); |
| 6009 | if (error) |
| 6010 | goto del_cursor; |
| 6011 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6012 | } |
| 6013 | |
| 6014 | /* |
| 6015 | * Convert to a btree if necessary. |
| 6016 | */ |
| 6017 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 6018 | int tmp_logflags; /* partial log flag return val */ |
| 6019 | |
| 6020 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6021 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6022 | &cur, 0, &tmp_logflags, whichfork); |
| 6023 | logflags |= tmp_logflags; |
| 6024 | } |
| 6025 | |
| 6026 | del_cursor: |
| 6027 | if (cur) { |
| 6028 | cur->bc_private.b.allocated = 0; |
| 6029 | xfs_btree_del_cursor(cur, |
| 6030 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6031 | } |
| 6032 | |
| 6033 | if (logflags) |
| 6034 | xfs_trans_log_inode(tp, ip, logflags); |
| 6035 | return error; |
| 6036 | } |
| 6037 | |
| 6038 | int |
| 6039 | xfs_bmap_split_extent( |
| 6040 | struct xfs_inode *ip, |
| 6041 | xfs_fileoff_t split_fsb) |
| 6042 | { |
| 6043 | struct xfs_mount *mp = ip->i_mount; |
| 6044 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6045 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6046 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6047 | int error; |
| 6048 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6049 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6050 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6051 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6052 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6053 | |
| 6054 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6055 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6056 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6057 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6058 | |
| 6059 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6060 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6061 | if (error) |
| 6062 | goto out; |
| 6063 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 6064 | error = xfs_defer_finish(&tp, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6065 | if (error) |
| 6066 | goto out; |
| 6067 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6068 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6069 | |
| 6070 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6071 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6072 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6073 | return error; |
| 6074 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6075 | |
| 6076 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6077 | static bool |
| 6078 | xfs_bmap_is_update_needed( |
| 6079 | struct xfs_bmbt_irec *bmap) |
| 6080 | { |
| 6081 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6082 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6083 | } |
| 6084 | |
| 6085 | /* Record a bmap intent. */ |
| 6086 | static int |
| 6087 | __xfs_bmap_add( |
| 6088 | struct xfs_mount *mp, |
| 6089 | struct xfs_defer_ops *dfops, |
| 6090 | enum xfs_bmap_intent_type type, |
| 6091 | struct xfs_inode *ip, |
| 6092 | int whichfork, |
| 6093 | struct xfs_bmbt_irec *bmap) |
| 6094 | { |
| 6095 | int error; |
| 6096 | struct xfs_bmap_intent *bi; |
| 6097 | |
| 6098 | trace_xfs_bmap_defer(mp, |
| 6099 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6100 | type, |
| 6101 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6102 | ip->i_ino, whichfork, |
| 6103 | bmap->br_startoff, |
| 6104 | bmap->br_blockcount, |
| 6105 | bmap->br_state); |
| 6106 | |
| 6107 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6108 | INIT_LIST_HEAD(&bi->bi_list); |
| 6109 | bi->bi_type = type; |
| 6110 | bi->bi_owner = ip; |
| 6111 | bi->bi_whichfork = whichfork; |
| 6112 | bi->bi_bmap = *bmap; |
| 6113 | |
Christoph Hellwig | 882d878 | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 6114 | error = xfs_defer_ijoin(dfops, bi->bi_owner); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6115 | if (error) { |
| 6116 | kmem_free(bi); |
| 6117 | return error; |
| 6118 | } |
| 6119 | |
| 6120 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6121 | return 0; |
| 6122 | } |
| 6123 | |
| 6124 | /* Map an extent into a file. */ |
| 6125 | int |
| 6126 | xfs_bmap_map_extent( |
| 6127 | struct xfs_mount *mp, |
| 6128 | struct xfs_defer_ops *dfops, |
| 6129 | struct xfs_inode *ip, |
| 6130 | struct xfs_bmbt_irec *PREV) |
| 6131 | { |
| 6132 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6133 | return 0; |
| 6134 | |
| 6135 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6136 | XFS_DATA_FORK, PREV); |
| 6137 | } |
| 6138 | |
| 6139 | /* Unmap an extent out of a file. */ |
| 6140 | int |
| 6141 | xfs_bmap_unmap_extent( |
| 6142 | struct xfs_mount *mp, |
| 6143 | struct xfs_defer_ops *dfops, |
| 6144 | struct xfs_inode *ip, |
| 6145 | struct xfs_bmbt_irec *PREV) |
| 6146 | { |
| 6147 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6148 | return 0; |
| 6149 | |
| 6150 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6151 | XFS_DATA_FORK, PREV); |
| 6152 | } |
| 6153 | |
| 6154 | /* |
| 6155 | * Process one of the deferred bmap operations. We pass back the |
| 6156 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6157 | */ |
| 6158 | int |
| 6159 | xfs_bmap_finish_one( |
| 6160 | struct xfs_trans *tp, |
| 6161 | struct xfs_defer_ops *dfops, |
| 6162 | struct xfs_inode *ip, |
| 6163 | enum xfs_bmap_intent_type type, |
| 6164 | int whichfork, |
| 6165 | xfs_fileoff_t startoff, |
| 6166 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6167 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6168 | xfs_exntst_t state) |
| 6169 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6170 | xfs_fsblock_t firstfsb; |
| 6171 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6172 | |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6173 | /* |
| 6174 | * firstfsb is tied to the transaction lifetime and is used to |
| 6175 | * ensure correct AG locking order and schedule work item |
| 6176 | * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us |
| 6177 | * to only making one bmap call per transaction, so it should |
| 6178 | * be safe to have it as a local variable here. |
| 6179 | */ |
| 6180 | firstfsb = NULLFSBLOCK; |
| 6181 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6182 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6183 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6184 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6185 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6186 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6187 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6188 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6189 | |
| 6190 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6191 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6192 | return -EIO; |
| 6193 | |
| 6194 | switch (type) { |
| 6195 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6196 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Darrick J. Wong | 7cf199b | 2018-05-14 06:34:34 -0700 | [diff] [blame] | 6197 | startblock, dfops, 0); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6198 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6199 | break; |
| 6200 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6201 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
| 6202 | XFS_BMAPI_REMAP, 1, &firstfsb, dfops); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6203 | break; |
| 6204 | default: |
| 6205 | ASSERT(0); |
| 6206 | error = -EFSCORRUPTED; |
| 6207 | } |
| 6208 | |
| 6209 | return error; |
| 6210 | } |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6211 | |
| 6212 | /* Check that an inode's extent does not have invalid flags or bad ranges. */ |
| 6213 | xfs_failaddr_t |
| 6214 | xfs_bmap_validate_extent( |
| 6215 | struct xfs_inode *ip, |
| 6216 | int whichfork, |
| 6217 | struct xfs_bmbt_irec *irec) |
| 6218 | { |
| 6219 | struct xfs_mount *mp = ip->i_mount; |
| 6220 | xfs_fsblock_t endfsb; |
| 6221 | bool isrt; |
| 6222 | |
| 6223 | isrt = XFS_IS_REALTIME_INODE(ip); |
| 6224 | endfsb = irec->br_startblock + irec->br_blockcount - 1; |
| 6225 | if (isrt) { |
| 6226 | if (!xfs_verify_rtbno(mp, irec->br_startblock)) |
| 6227 | return __this_address; |
| 6228 | if (!xfs_verify_rtbno(mp, endfsb)) |
| 6229 | return __this_address; |
| 6230 | } else { |
| 6231 | if (!xfs_verify_fsbno(mp, irec->br_startblock)) |
| 6232 | return __this_address; |
| 6233 | if (!xfs_verify_fsbno(mp, endfsb)) |
| 6234 | return __this_address; |
| 6235 | if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) != |
| 6236 | XFS_FSB_TO_AGNO(mp, endfsb)) |
| 6237 | return __this_address; |
| 6238 | } |
| 6239 | if (irec->br_state != XFS_EXT_NORM) { |
| 6240 | if (whichfork != XFS_DATA_FORK) |
| 6241 | return __this_address; |
| 6242 | if (!xfs_sb_version_hasextflgbit(&mp->m_sb)) |
| 6243 | return __this_address; |
| 6244 | } |
| 6245 | return NULL; |
| 6246 | } |