Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
| 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_sb.h" |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 27 | #include "xfs_defer.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 28 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_da_btree.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 30 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_btree.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 33 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_extfree_item.h" |
| 36 | #include "xfs_alloc.h" |
| 37 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 38 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 39 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_rtalloc.h" |
Darrick J. Wong | e9e899a | 2017-10-31 12:04:49 -0700 | [diff] [blame] | 41 | #include "xfs_errortag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "xfs_quota.h" |
| 44 | #include "xfs_trans_space.h" |
| 45 | #include "xfs_buf_item.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 46 | #include "xfs_trace.h" |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 47 | #include "xfs_symlink.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 48 | #include "xfs_attr_leaf.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 49 | #include "xfs_filestream.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 50 | #include "xfs_rmap.h" |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 51 | #include "xfs_ag_resv.h" |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 52 | #include "xfs_refcount.h" |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 53 | #include "xfs_icache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 57 | |
| 58 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 59 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 63 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 64 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 66 | void |
| 67 | xfs_bmap_compute_maxlevels( |
| 68 | xfs_mount_t *mp, /* file system mount structure */ |
| 69 | int whichfork) /* data or attr fork */ |
| 70 | { |
| 71 | int level; /* btree level */ |
| 72 | uint maxblocks; /* max blocks at this level */ |
| 73 | uint maxleafents; /* max leaf entries possible */ |
| 74 | int maxrootrecs; /* max records in root block */ |
| 75 | int minleafrecs; /* min records in leaf block */ |
| 76 | int minnoderecs; /* min records in node block */ |
| 77 | int sz; /* root block size */ |
| 78 | |
| 79 | /* |
| 80 | * The maximum number of extents in a file, hence the maximum |
| 81 | * number of leaf entries, is controlled by the type of di_nextents |
| 82 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 83 | * (a signed 16-bit number, xfs_aextnum_t). |
| 84 | * |
| 85 | * Note that we can no longer assume that if we are in ATTR1 that |
| 86 | * the fork offset of all the inodes will be |
| 87 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 88 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 89 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 90 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 91 | * of a minimum size available. |
| 92 | */ |
| 93 | if (whichfork == XFS_DATA_FORK) { |
| 94 | maxleafents = MAXEXTNUM; |
| 95 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 96 | } else { |
| 97 | maxleafents = MAXAEXTNUM; |
| 98 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 99 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 100 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 101 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 102 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 103 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 104 | for (level = 1; maxblocks > 1; level++) { |
| 105 | if (maxblocks <= maxrootrecs) |
| 106 | maxblocks = 1; |
| 107 | else |
| 108 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 109 | } |
| 110 | mp->m_bm_maxlevels[whichfork] = level; |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 113 | STATIC int /* error */ |
| 114 | xfs_bmbt_lookup_eq( |
| 115 | struct xfs_btree_cur *cur, |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 116 | struct xfs_bmbt_irec *irec, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 117 | int *stat) /* success/failure */ |
| 118 | { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 119 | cur->bc_rec.b = *irec; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 120 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 121 | } |
| 122 | |
| 123 | STATIC int /* error */ |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 124 | xfs_bmbt_lookup_first( |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 125 | struct xfs_btree_cur *cur, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 126 | int *stat) /* success/failure */ |
| 127 | { |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 128 | cur->bc_rec.b.br_startoff = 0; |
| 129 | cur->bc_rec.b.br_startblock = 0; |
| 130 | cur->bc_rec.b.br_blockcount = 0; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 131 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 132 | } |
| 133 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 134 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 135 | * Check if the inode needs to be converted to btree format. |
| 136 | */ |
| 137 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 138 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 139 | return whichfork != XFS_COW_FORK && |
| 140 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 141 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 142 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Check if the inode should be converted to extent format. |
| 147 | */ |
| 148 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 149 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 150 | return whichfork != XFS_COW_FORK && |
| 151 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 152 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 153 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 154 | } |
| 155 | |
| 156 | /* |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 157 | * 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] | 158 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 159 | */ |
| 160 | STATIC int |
| 161 | xfs_bmbt_update( |
| 162 | struct xfs_btree_cur *cur, |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 163 | struct xfs_bmbt_irec *irec) |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 164 | { |
| 165 | union xfs_btree_rec rec; |
| 166 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 167 | xfs_bmbt_disk_set_all(&rec.bmbt, irec); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 168 | return xfs_btree_update(cur, &rec); |
| 169 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 172 | * Compute the worst-case number of indirect blocks that will be used |
| 173 | * for ip's delayed extent of length "len". |
| 174 | */ |
| 175 | STATIC xfs_filblks_t |
| 176 | xfs_bmap_worst_indlen( |
| 177 | xfs_inode_t *ip, /* incore inode pointer */ |
| 178 | xfs_filblks_t len) /* delayed extent length */ |
| 179 | { |
| 180 | int level; /* btree level number */ |
| 181 | int maxrecs; /* maximum record count at this level */ |
| 182 | xfs_mount_t *mp; /* mount structure */ |
| 183 | xfs_filblks_t rval; /* return value */ |
| 184 | |
| 185 | mp = ip->i_mount; |
| 186 | maxrecs = mp->m_bmap_dmxr[0]; |
| 187 | for (level = 0, rval = 0; |
| 188 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 189 | level++) { |
| 190 | len += maxrecs - 1; |
| 191 | do_div(len, maxrecs); |
| 192 | rval += len; |
Darrick J. Wong | 5e5c943 | 2017-09-18 09:41:17 -0700 | [diff] [blame] | 193 | if (len == 1) |
| 194 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 195 | level - 1; |
| 196 | if (level == 0) |
| 197 | maxrecs = mp->m_bmap_dmxr[1]; |
| 198 | } |
| 199 | return rval; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Calculate the default attribute fork offset for newly created inodes. |
| 204 | */ |
| 205 | uint |
| 206 | xfs_default_attroffset( |
| 207 | struct xfs_inode *ip) |
| 208 | { |
| 209 | struct xfs_mount *mp = ip->i_mount; |
| 210 | uint offset; |
| 211 | |
| 212 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 213 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 214 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 215 | } else { |
| 216 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 217 | } |
| 218 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 219 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 220 | return offset; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Helper routine to reset inode di_forkoff field when switching |
| 225 | * attribute fork from local to extent format - we reset it where |
| 226 | * possible to make space available for inline data fork extents. |
| 227 | */ |
| 228 | STATIC void |
| 229 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 230 | xfs_inode_t *ip, |
| 231 | int whichfork) |
| 232 | { |
| 233 | if (whichfork == XFS_ATTR_FORK && |
| 234 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 235 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 236 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 237 | |
| 238 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 239 | ip->i_d.di_forkoff = dfl_forkoff; |
| 240 | } |
| 241 | } |
| 242 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 243 | #ifdef DEBUG |
| 244 | STATIC struct xfs_buf * |
| 245 | xfs_bmap_get_bp( |
| 246 | struct xfs_btree_cur *cur, |
| 247 | xfs_fsblock_t bno) |
| 248 | { |
| 249 | struct xfs_log_item_desc *lidp; |
| 250 | int i; |
| 251 | |
| 252 | if (!cur) |
| 253 | return NULL; |
| 254 | |
| 255 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 256 | if (!cur->bc_bufs[i]) |
| 257 | break; |
| 258 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 259 | return cur->bc_bufs[i]; |
| 260 | } |
| 261 | |
| 262 | /* Chase down all the log items to see if the bp is there */ |
| 263 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 264 | struct xfs_buf_log_item *bip; |
| 265 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 266 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 267 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 268 | return bip->bli_buf; |
| 269 | } |
| 270 | |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | STATIC void |
| 275 | xfs_check_block( |
| 276 | struct xfs_btree_block *block, |
| 277 | xfs_mount_t *mp, |
| 278 | int root, |
| 279 | short sz) |
| 280 | { |
| 281 | int i, j, dmxr; |
| 282 | __be64 *pp, *thispa; /* pointer to block address */ |
| 283 | xfs_bmbt_key_t *prevp, *keyp; |
| 284 | |
| 285 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 286 | |
| 287 | prevp = NULL; |
| 288 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 289 | dmxr = mp->m_bmap_dmxr[0]; |
| 290 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 291 | |
| 292 | if (prevp) { |
| 293 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 294 | be64_to_cpu(keyp->br_startoff)); |
| 295 | } |
| 296 | prevp = keyp; |
| 297 | |
| 298 | /* |
| 299 | * Compare the block numbers to see if there are dups. |
| 300 | */ |
| 301 | if (root) |
| 302 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 303 | else |
| 304 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 305 | |
| 306 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 307 | if (root) |
| 308 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 309 | else |
| 310 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 311 | if (*thispa == *pp) { |
| 312 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 313 | __func__, j, i, |
| 314 | (unsigned long long)be64_to_cpu(*thispa)); |
| 315 | panic("%s: ptrs are equal in node\n", |
| 316 | __func__); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * 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] | 324 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 325 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 326 | * them. The btree record ordering checks will still be done, so for such large |
| 327 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 328 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 329 | STATIC void |
| 330 | xfs_bmap_check_leaf_extents( |
| 331 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 332 | xfs_inode_t *ip, /* incore inode pointer */ |
| 333 | int whichfork) /* data or attr fork */ |
| 334 | { |
| 335 | struct xfs_btree_block *block; /* current btree block */ |
| 336 | xfs_fsblock_t bno; /* block # of "block" */ |
| 337 | xfs_buf_t *bp; /* buffer for "block" */ |
| 338 | int error; /* error return value */ |
| 339 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 340 | xfs_ifork_t *ifp; /* fork structure */ |
| 341 | int level; /* btree level, for checking */ |
| 342 | xfs_mount_t *mp; /* file system mount structure */ |
| 343 | __be64 *pp; /* pointer to block address */ |
| 344 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 345 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 346 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 347 | int bp_release = 0; |
| 348 | |
| 349 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 350 | return; |
| 351 | } |
| 352 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 353 | /* skip large extent count inodes */ |
| 354 | if (ip->i_d.di_nextents > 10000) |
| 355 | return; |
| 356 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 357 | bno = NULLFSBLOCK; |
| 358 | mp = ip->i_mount; |
| 359 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 360 | block = ifp->if_broot; |
| 361 | /* |
| 362 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 363 | */ |
| 364 | level = be16_to_cpu(block->bb_level); |
| 365 | ASSERT(level > 0); |
| 366 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 367 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 368 | bno = be64_to_cpu(*pp); |
| 369 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 370 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 371 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 372 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 373 | |
| 374 | /* |
| 375 | * Go down the tree until leaf level is reached, following the first |
| 376 | * pointer (leftmost) at each level. |
| 377 | */ |
| 378 | while (level-- > 0) { |
| 379 | /* See if buf is in cur first */ |
| 380 | bp_release = 0; |
| 381 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 382 | if (!bp) { |
| 383 | bp_release = 1; |
| 384 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 385 | XFS_BMAP_BTREE_REF, |
| 386 | &xfs_bmbt_buf_ops); |
| 387 | if (error) |
| 388 | goto error_norelse; |
| 389 | } |
| 390 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 391 | if (level == 0) |
| 392 | break; |
| 393 | |
| 394 | /* |
| 395 | * Check this block for basic sanity (increasing keys and |
| 396 | * no duplicate blocks). |
| 397 | */ |
| 398 | |
| 399 | xfs_check_block(block, mp, 0, 0); |
| 400 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 401 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 402 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 403 | xfs_verify_fsbno(mp, bno), error0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 404 | if (bp_release) { |
| 405 | bp_release = 0; |
| 406 | xfs_trans_brelse(NULL, bp); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 412 | */ |
| 413 | i = 0; |
| 414 | |
| 415 | /* |
| 416 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 417 | */ |
| 418 | for (;;) { |
| 419 | xfs_fsblock_t nextbno; |
| 420 | xfs_extnum_t num_recs; |
| 421 | |
| 422 | |
| 423 | num_recs = xfs_btree_get_numrecs(block); |
| 424 | |
| 425 | /* |
| 426 | * Read-ahead the next leaf block, if any. |
| 427 | */ |
| 428 | |
| 429 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 430 | |
| 431 | /* |
| 432 | * Check all the extents to make sure they are OK. |
| 433 | * If we had a previous block, the last entry should |
| 434 | * conform with the first entry in this one. |
| 435 | */ |
| 436 | |
| 437 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 438 | if (i) { |
| 439 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 440 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 441 | xfs_bmbt_disk_get_startoff(ep)); |
| 442 | } |
| 443 | for (j = 1; j < num_recs; j++) { |
| 444 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 445 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 446 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 447 | xfs_bmbt_disk_get_startoff(nextp)); |
| 448 | ep = nextp; |
| 449 | } |
| 450 | |
| 451 | last = *ep; |
| 452 | i += num_recs; |
| 453 | if (bp_release) { |
| 454 | bp_release = 0; |
| 455 | xfs_trans_brelse(NULL, bp); |
| 456 | } |
| 457 | bno = nextbno; |
| 458 | /* |
| 459 | * If we've reached the end, stop. |
| 460 | */ |
| 461 | if (bno == NULLFSBLOCK) |
| 462 | break; |
| 463 | |
| 464 | bp_release = 0; |
| 465 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 466 | if (!bp) { |
| 467 | bp_release = 1; |
| 468 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 469 | XFS_BMAP_BTREE_REF, |
| 470 | &xfs_bmbt_buf_ops); |
| 471 | if (error) |
| 472 | goto error_norelse; |
| 473 | } |
| 474 | block = XFS_BUF_TO_BLOCK(bp); |
| 475 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 476 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 477 | return; |
| 478 | |
| 479 | error0: |
| 480 | xfs_warn(mp, "%s: at error0", __func__); |
| 481 | if (bp_release) |
| 482 | xfs_trans_brelse(NULL, bp); |
| 483 | error_norelse: |
| 484 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 485 | __func__, i); |
| 486 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 491 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 492 | * given the caller's original parameters. Specifically check the |
| 493 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 494 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 495 | */ |
| 496 | STATIC void |
| 497 | xfs_bmap_validate_ret( |
| 498 | xfs_fileoff_t bno, |
| 499 | xfs_filblks_t len, |
| 500 | int flags, |
| 501 | xfs_bmbt_irec_t *mval, |
| 502 | int nmap, |
| 503 | int ret_nmap) |
| 504 | { |
| 505 | int i; /* index to map values */ |
| 506 | |
| 507 | ASSERT(ret_nmap <= nmap); |
| 508 | |
| 509 | for (i = 0; i < ret_nmap; i++) { |
| 510 | ASSERT(mval[i].br_blockcount > 0); |
| 511 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 512 | ASSERT(mval[i].br_startoff >= bno); |
| 513 | ASSERT(mval[i].br_blockcount <= len); |
| 514 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 515 | bno + len); |
| 516 | } else { |
| 517 | ASSERT(mval[i].br_startoff < bno + len); |
| 518 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 519 | bno); |
| 520 | } |
| 521 | ASSERT(i == 0 || |
| 522 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 523 | mval[i].br_startoff); |
| 524 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 525 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 526 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 527 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | #else |
| 532 | #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] | 533 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 534 | #endif /* DEBUG */ |
| 535 | |
| 536 | /* |
| 537 | * bmap free list manipulation functions |
| 538 | */ |
| 539 | |
| 540 | /* |
| 541 | * Add the extent to the list of extents to be free at transaction end. |
| 542 | * The list is maintained sorted (by block number). |
| 543 | */ |
| 544 | void |
| 545 | xfs_bmap_add_free( |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 546 | struct xfs_mount *mp, |
| 547 | struct xfs_defer_ops *dfops, |
| 548 | xfs_fsblock_t bno, |
| 549 | xfs_filblks_t len, |
| 550 | struct xfs_owner_info *oinfo) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 551 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 552 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 553 | #ifdef DEBUG |
| 554 | xfs_agnumber_t agno; |
| 555 | xfs_agblock_t agbno; |
| 556 | |
| 557 | ASSERT(bno != NULLFSBLOCK); |
| 558 | ASSERT(len > 0); |
| 559 | ASSERT(len <= MAXEXTLEN); |
| 560 | ASSERT(!isnullstartblock(bno)); |
| 561 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 562 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 563 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 564 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 565 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 566 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 567 | #endif |
| 568 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 569 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 570 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 571 | new->xefi_startblock = bno; |
| 572 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 573 | if (oinfo) |
| 574 | new->xefi_oinfo = *oinfo; |
| 575 | else |
| 576 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 577 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
| 578 | XFS_FSB_TO_AGBNO(mp, bno), len); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 579 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Inode fork format manipulation functions |
| 584 | */ |
| 585 | |
| 586 | /* |
| 587 | * Transform a btree format file with only one leaf node, where the |
| 588 | * extents list will fit in the inode, into an extents format file. |
| 589 | * Since the file extents are already in-core, all we have to do is |
| 590 | * give up the space for the btree root and pitch the leaf block. |
| 591 | */ |
| 592 | STATIC int /* error */ |
| 593 | xfs_bmap_btree_to_extents( |
| 594 | xfs_trans_t *tp, /* transaction pointer */ |
| 595 | xfs_inode_t *ip, /* incore inode pointer */ |
| 596 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 597 | int *logflagsp, /* inode logging flags */ |
| 598 | int whichfork) /* data or attr fork */ |
| 599 | { |
| 600 | /* REFERENCED */ |
| 601 | struct xfs_btree_block *cblock;/* child btree block */ |
| 602 | xfs_fsblock_t cbno; /* child block number */ |
| 603 | xfs_buf_t *cbp; /* child block's buffer */ |
| 604 | int error; /* error return value */ |
| 605 | xfs_ifork_t *ifp; /* inode fork data */ |
| 606 | xfs_mount_t *mp; /* mount point structure */ |
| 607 | __be64 *pp; /* ptr to block address */ |
| 608 | struct xfs_btree_block *rblock;/* root btree block */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 609 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 610 | |
| 611 | mp = ip->i_mount; |
| 612 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 613 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 614 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 615 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 616 | rblock = ifp->if_broot; |
| 617 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 618 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 619 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 620 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 621 | cbno = be64_to_cpu(*pp); |
| 622 | *logflagsp = 0; |
| 623 | #ifdef DEBUG |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 624 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, |
| 625 | xfs_btree_check_lptr(cur, cbno, 1)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 626 | #endif |
| 627 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 628 | &xfs_bmbt_buf_ops); |
| 629 | if (error) |
| 630 | return error; |
| 631 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 632 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 633 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 634 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
| 635 | xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 636 | ip->i_d.di_nblocks--; |
| 637 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 638 | xfs_trans_binval(tp, cbp); |
| 639 | if (cur->bc_bufs[0] == cbp) |
| 640 | cur->bc_bufs[0] = NULL; |
| 641 | xfs_iroot_realloc(ip, -1, whichfork); |
| 642 | ASSERT(ifp->if_broot == NULL); |
| 643 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 644 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 645 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Convert an extents-format file into a btree-format file. |
| 651 | * The new file will have a root block (in the inode) and a single child block. |
| 652 | */ |
| 653 | STATIC int /* error */ |
| 654 | xfs_bmap_extents_to_btree( |
| 655 | xfs_trans_t *tp, /* transaction pointer */ |
| 656 | xfs_inode_t *ip, /* incore inode pointer */ |
| 657 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 658 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 659 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 660 | int wasdel, /* converting a delayed alloc */ |
| 661 | int *logflagsp, /* inode logging flags */ |
| 662 | int whichfork) /* data or attr fork */ |
| 663 | { |
| 664 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 665 | xfs_buf_t *abp; /* buffer for ablock */ |
| 666 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 667 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 668 | struct xfs_btree_block *block; /* btree root block */ |
| 669 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 670 | int error; /* error return value */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 671 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 672 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 673 | xfs_mount_t *mp; /* mount structure */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 674 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 675 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 676 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 677 | xfs_extnum_t cnt = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 678 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 679 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 680 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 681 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 682 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 683 | |
| 684 | /* |
| 685 | * Make space in the inode incore. |
| 686 | */ |
| 687 | xfs_iroot_realloc(ip, 1, whichfork); |
| 688 | ifp->if_flags |= XFS_IFBROOT; |
| 689 | |
| 690 | /* |
| 691 | * Fill in the root. |
| 692 | */ |
| 693 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 694 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 695 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 696 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 697 | /* |
| 698 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 699 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 700 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 701 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 702 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 703 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 704 | /* |
| 705 | * Convert to a btree with two levels, one record in root. |
| 706 | */ |
| 707 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 708 | memset(&args, 0, sizeof(args)); |
| 709 | args.tp = tp; |
| 710 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 711 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 712 | args.firstblock = *firstblock; |
| 713 | if (*firstblock == NULLFSBLOCK) { |
| 714 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 715 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 716 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 717 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 718 | args.fsbno = *firstblock; |
| 719 | } else { |
| 720 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 721 | args.fsbno = *firstblock; |
| 722 | } |
| 723 | args.minlen = args.maxlen = args.prod = 1; |
| 724 | args.wasdel = wasdel; |
| 725 | *logflagsp = 0; |
| 726 | if ((error = xfs_alloc_vextent(&args))) { |
| 727 | xfs_iroot_realloc(ip, -1, whichfork); |
| 728 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 729 | return error; |
| 730 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 731 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 732 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
| 733 | xfs_iroot_realloc(ip, -1, whichfork); |
| 734 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 735 | return -ENOSPC; |
| 736 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 737 | /* |
| 738 | * Allocation can't fail, the space was reserved. |
| 739 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 740 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 741 | args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 742 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 743 | cur->bc_private.b.allocated++; |
| 744 | ip->i_d.di_nblocks++; |
| 745 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 746 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 747 | /* |
| 748 | * Fill in the child block. |
| 749 | */ |
| 750 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 751 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 752 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 753 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 754 | XFS_BTREE_LONG_PTRS); |
| 755 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 756 | for_each_xfs_iext(ifp, &icur, &rec) { |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 757 | if (isnullstartblock(rec.br_startblock)) |
| 758 | continue; |
| 759 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt); |
| 760 | xfs_bmbt_disk_set_all(arp, &rec); |
| 761 | cnt++; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 762 | } |
| 763 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 764 | xfs_btree_set_numrecs(ablock, cnt); |
| 765 | |
| 766 | /* |
| 767 | * Fill in the root key and pointer. |
| 768 | */ |
| 769 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 770 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 771 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 772 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 773 | be16_to_cpu(block->bb_level))); |
| 774 | *pp = cpu_to_be64(args.fsbno); |
| 775 | |
| 776 | /* |
| 777 | * Do all this logging at the end so that |
| 778 | * the root is at the right level. |
| 779 | */ |
| 780 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 781 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 782 | ASSERT(*curp == NULL); |
| 783 | *curp = cur; |
| 784 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Convert a local file to an extents file. |
| 790 | * This code is out of bounds for data forks of regular files, |
| 791 | * since the file data needs to get logged so things will stay consistent. |
| 792 | * (The bmap-level manipulations are ok, though). |
| 793 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 794 | void |
| 795 | xfs_bmap_local_to_extents_empty( |
| 796 | struct xfs_inode *ip, |
| 797 | int whichfork) |
| 798 | { |
| 799 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 800 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 801 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 802 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 803 | ASSERT(ifp->if_bytes == 0); |
| 804 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 805 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 806 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 807 | ifp->if_flags &= ~XFS_IFINLINE; |
| 808 | ifp->if_flags |= XFS_IFEXTENTS; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 809 | ifp->if_u1.if_root = NULL; |
| 810 | ifp->if_height = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 811 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 812 | } |
| 813 | |
| 814 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 815 | STATIC int /* error */ |
| 816 | xfs_bmap_local_to_extents( |
| 817 | xfs_trans_t *tp, /* transaction pointer */ |
| 818 | xfs_inode_t *ip, /* incore inode pointer */ |
| 819 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 820 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 821 | int *logflagsp, /* inode logging flags */ |
| 822 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 823 | void (*init_fn)(struct xfs_trans *tp, |
| 824 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 825 | struct xfs_inode *ip, |
| 826 | struct xfs_ifork *ifp)) |
| 827 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 828 | int error = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 829 | int flags; /* logging flags returned */ |
| 830 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 831 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 832 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 833 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 834 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 835 | |
| 836 | /* |
| 837 | * We don't want to deal with the case of keeping inode data inline yet. |
| 838 | * So sending the data fork of a regular inode is invalid. |
| 839 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 840 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 841 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 842 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 843 | |
| 844 | if (!ifp->if_bytes) { |
| 845 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 846 | flags = XFS_ILOG_CORE; |
| 847 | goto done; |
| 848 | } |
| 849 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 850 | flags = 0; |
| 851 | error = 0; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 852 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 853 | memset(&args, 0, sizeof(args)); |
| 854 | args.tp = tp; |
| 855 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 856 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 857 | args.firstblock = *firstblock; |
| 858 | /* |
| 859 | * Allocate a block. We know we need only one, since the |
| 860 | * file currently fits in an inode. |
| 861 | */ |
| 862 | if (*firstblock == NULLFSBLOCK) { |
| 863 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 864 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 865 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 866 | args.fsbno = *firstblock; |
| 867 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 868 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 869 | args.total = total; |
| 870 | args.minlen = args.maxlen = args.prod = 1; |
| 871 | error = xfs_alloc_vextent(&args); |
| 872 | if (error) |
| 873 | goto done; |
| 874 | |
| 875 | /* Can't fail, the space was reserved. */ |
| 876 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 877 | ASSERT(args.len == 1); |
| 878 | *firstblock = args.fsbno; |
| 879 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 880 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 881 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 882 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 883 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 884 | * The callout is responsible for logging because the remote format |
| 885 | * might differ from the local format and thus we don't know how much to |
| 886 | * log here. Note that init_fn must also set the buffer log item type |
| 887 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 888 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 889 | init_fn(tp, bp, ip, ifp); |
| 890 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 891 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 892 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 893 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 894 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 895 | |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 896 | ifp->if_u1.if_root = NULL; |
| 897 | ifp->if_height = 0; |
| 898 | |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 899 | rec.br_startoff = 0; |
| 900 | rec.br_startblock = args.fsbno; |
| 901 | rec.br_blockcount = 1; |
| 902 | rec.br_state = XFS_EXT_NORM; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 903 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 904 | xfs_iext_insert(ip, &icur, &rec, 0); |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 905 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 906 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 907 | ip->i_d.di_nblocks = 1; |
| 908 | xfs_trans_mod_dquot_byino(tp, ip, |
| 909 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 910 | flags |= xfs_ilog_fext(whichfork); |
| 911 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 912 | done: |
| 913 | *logflagsp = flags; |
| 914 | return error; |
| 915 | } |
| 916 | |
| 917 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 919 | */ |
| 920 | STATIC int /* error */ |
| 921 | xfs_bmap_add_attrfork_btree( |
| 922 | xfs_trans_t *tp, /* transaction pointer */ |
| 923 | xfs_inode_t *ip, /* incore inode pointer */ |
| 924 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 925 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | int *flags) /* inode logging flags */ |
| 927 | { |
| 928 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 929 | int error; /* error return value */ |
| 930 | xfs_mount_t *mp; /* file system mount struct */ |
| 931 | int stat; /* newroot status */ |
| 932 | |
| 933 | mp = ip->i_mount; |
| 934 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 935 | *flags |= XFS_ILOG_DBROOT; |
| 936 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 937 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 938 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | cur->bc_private.b.firstblock = *firstblock; |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 940 | error = xfs_bmbt_lookup_first(cur, &stat); |
| 941 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 943 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 944 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 945 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | goto error0; |
| 947 | if (stat == 0) { |
| 948 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 949 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
| 951 | *firstblock = cur->bc_private.b.firstblock; |
| 952 | cur->bc_private.b.allocated = 0; |
| 953 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 954 | } |
| 955 | return 0; |
| 956 | error0: |
| 957 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 958 | return error; |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 963 | */ |
| 964 | STATIC int /* error */ |
| 965 | xfs_bmap_add_attrfork_extents( |
| 966 | xfs_trans_t *tp, /* transaction pointer */ |
| 967 | xfs_inode_t *ip, /* incore inode pointer */ |
| 968 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 969 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | int *flags) /* inode logging flags */ |
| 971 | { |
| 972 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 973 | int error; /* error return value */ |
| 974 | |
| 975 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 976 | return 0; |
| 977 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 978 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | flags, XFS_DATA_FORK); |
| 980 | if (cur) { |
| 981 | cur->bc_private.b.allocated = 0; |
| 982 | xfs_btree_del_cursor(cur, |
| 983 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 984 | } |
| 985 | return error; |
| 986 | } |
| 987 | |
| 988 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 989 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 990 | * different data fork content type needs a different callout to do the |
| 991 | * conversion. Some are basic and only require special block initialisation |
| 992 | * callouts for the data formating, others (directories) are so specialised they |
| 993 | * handle everything themselves. |
| 994 | * |
| 995 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 996 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 997 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | */ |
| 999 | STATIC int /* error */ |
| 1000 | xfs_bmap_add_attrfork_local( |
| 1001 | xfs_trans_t *tp, /* transaction pointer */ |
| 1002 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1003 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1004 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | int *flags) /* inode logging flags */ |
| 1006 | { |
| 1007 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1010 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1011 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1012 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1014 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | dargs.dp = ip; |
| 1016 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1017 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1018 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | dargs.whichfork = XFS_DATA_FORK; |
| 1020 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1021 | return xfs_dir2_sf_to_block(&dargs); |
| 1022 | } |
| 1023 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1024 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1025 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1026 | flags, XFS_DATA_FORK, |
| 1027 | xfs_symlink_local_to_remote); |
| 1028 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1029 | /* should only be called for types that support local format data */ |
| 1030 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1031 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1035 | * Convert inode from non-attributed to attributed. |
| 1036 | * Must not be in a transaction, ip must not be locked. |
| 1037 | */ |
| 1038 | int /* error code */ |
| 1039 | xfs_bmap_add_attrfork( |
| 1040 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1041 | int size, /* space new attribute needs */ |
| 1042 | int rsvd) /* xact may use reserved blks */ |
| 1043 | { |
| 1044 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1045 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1046 | xfs_mount_t *mp; /* mount structure */ |
| 1047 | xfs_trans_t *tp; /* transaction pointer */ |
| 1048 | int blks; /* space reservation */ |
| 1049 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1050 | int logflags; /* logging flags */ |
| 1051 | int error; /* error return value */ |
| 1052 | |
| 1053 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1054 | |
| 1055 | mp = ip->i_mount; |
| 1056 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1057 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1058 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1059 | |
| 1060 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1061 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1062 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1063 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1064 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1065 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1066 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1067 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1068 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1069 | if (error) |
| 1070 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1071 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1072 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1073 | if (ip->i_d.di_anextents != 0) { |
| 1074 | error = -EFSCORRUPTED; |
| 1075 | goto trans_cancel; |
| 1076 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1077 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1078 | /* |
| 1079 | * For inodes coming from pre-6.2 filesystems. |
| 1080 | */ |
| 1081 | ASSERT(ip->i_d.di_aformat == 0); |
| 1082 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1083 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1084 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1085 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1086 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1087 | |
| 1088 | switch (ip->i_d.di_format) { |
| 1089 | case XFS_DINODE_FMT_DEV: |
| 1090 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1091 | break; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1092 | case XFS_DINODE_FMT_LOCAL: |
| 1093 | case XFS_DINODE_FMT_EXTENTS: |
| 1094 | case XFS_DINODE_FMT_BTREE: |
| 1095 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1096 | if (!ip->i_d.di_forkoff) |
| 1097 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1098 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1099 | version = 2; |
| 1100 | break; |
| 1101 | default: |
| 1102 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1103 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1104 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | ASSERT(ip->i_afp == NULL); |
| 1108 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1109 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1110 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1111 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1112 | switch (ip->i_d.di_format) { |
| 1113 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1114 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1115 | &logflags); |
| 1116 | break; |
| 1117 | case XFS_DINODE_FMT_EXTENTS: |
| 1118 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1119 | &dfops, &logflags); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1120 | break; |
| 1121 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1122 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1123 | &logflags); |
| 1124 | break; |
| 1125 | default: |
| 1126 | error = 0; |
| 1127 | break; |
| 1128 | } |
| 1129 | if (logflags) |
| 1130 | xfs_trans_log_inode(tp, ip, logflags); |
| 1131 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1132 | goto bmap_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1133 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1134 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1135 | bool log_sb = false; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1136 | |
| 1137 | spin_lock(&mp->m_sb_lock); |
| 1138 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1139 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1140 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1141 | } |
| 1142 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1143 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1144 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1145 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1146 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1147 | if (log_sb) |
| 1148 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1149 | } |
| 1150 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 1151 | error = xfs_defer_finish(&tp, &dfops); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1152 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1153 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1154 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1155 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1156 | return error; |
| 1157 | |
| 1158 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1159 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1160 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1161 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1162 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1163 | return error; |
| 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | * Internal and external extent tree search functions. |
| 1168 | */ |
| 1169 | |
| 1170 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1171 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1172 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1173 | int |
| 1174 | xfs_iread_extents( |
| 1175 | struct xfs_trans *tp, |
| 1176 | struct xfs_inode *ip, |
| 1177 | int whichfork) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1178 | { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1179 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | e8e0e17 | 2017-10-19 11:06:29 -0700 | [diff] [blame] | 1180 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1181 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1182 | xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
| 1183 | struct xfs_btree_block *block = ifp->if_broot; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1184 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1185 | struct xfs_bmbt_irec new; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1186 | xfs_fsblock_t bno; |
| 1187 | struct xfs_buf *bp; |
| 1188 | xfs_extnum_t i, j; |
| 1189 | int level; |
| 1190 | __be64 *pp; |
| 1191 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1192 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1193 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1194 | |
| 1195 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 1196 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 1197 | return -EFSCORRUPTED; |
| 1198 | } |
| 1199 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1200 | /* |
| 1201 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1202 | */ |
| 1203 | level = be16_to_cpu(block->bb_level); |
| 1204 | ASSERT(level > 0); |
| 1205 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1206 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1207 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1208 | /* |
| 1209 | * Go down the tree until leaf level is reached, following the first |
| 1210 | * pointer (leftmost) at each level. |
| 1211 | */ |
| 1212 | while (level-- > 0) { |
| 1213 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1214 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1215 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1216 | goto out; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1217 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1218 | if (level == 0) |
| 1219 | break; |
| 1220 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1221 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1222 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 1223 | xfs_verify_fsbno(mp, bno), out_brelse); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1224 | xfs_trans_brelse(tp, bp); |
| 1225 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1226 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1227 | /* |
| 1228 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1229 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1230 | i = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1231 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1232 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1233 | /* |
| 1234 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1235 | */ |
| 1236 | for (;;) { |
| 1237 | xfs_bmbt_rec_t *frp; |
| 1238 | xfs_fsblock_t nextbno; |
| 1239 | xfs_extnum_t num_recs; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1240 | |
| 1241 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1242 | if (unlikely(i + num_recs > nextents)) { |
| 1243 | ASSERT(i + num_recs <= nextents); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1244 | xfs_warn(ip->i_mount, |
| 1245 | "corrupt dinode %Lu, (btree extents).", |
| 1246 | (unsigned long long) ip->i_ino); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1247 | XFS_CORRUPTION_ERROR(__func__, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1248 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1249 | error = -EFSCORRUPTED; |
| 1250 | goto out_brelse; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1251 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1252 | /* |
| 1253 | * Read-ahead the next leaf block, if any. |
| 1254 | */ |
| 1255 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1256 | if (nextbno != NULLFSBLOCK) |
| 1257 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1258 | &xfs_bmbt_buf_ops); |
| 1259 | /* |
| 1260 | * Copy records into the extent records. |
| 1261 | */ |
| 1262 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1263 | for (j = 0; j < num_recs; j++, frp++, i++) { |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame^] | 1264 | xfs_failaddr_t fa; |
| 1265 | |
Christoph Hellwig | dac9c9b | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1266 | xfs_bmbt_disk_get_all(frp, &new); |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame^] | 1267 | fa = xfs_bmap_validate_extent(ip, whichfork, &new); |
| 1268 | if (fa) { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1269 | error = -EFSCORRUPTED; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame^] | 1270 | xfs_inode_verifier_error(ip, error, |
| 1271 | "xfs_iread_extents(2)", |
| 1272 | frp, sizeof(*frp), fa); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1273 | goto out_brelse; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1274 | } |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1275 | xfs_iext_insert(ip, &icur, &new, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1276 | trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); |
| 1277 | xfs_iext_next(ifp, &icur); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1278 | } |
| 1279 | xfs_trans_brelse(tp, bp); |
| 1280 | bno = nextbno; |
| 1281 | /* |
| 1282 | * If we've reached the end, stop. |
| 1283 | */ |
| 1284 | if (bno == NULLFSBLOCK) |
| 1285 | break; |
| 1286 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1287 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1288 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1289 | goto out; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1290 | block = XFS_BUF_TO_BLOCK(bp); |
| 1291 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1292 | |
| 1293 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) { |
| 1294 | error = -EFSCORRUPTED; |
| 1295 | goto out; |
| 1296 | } |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1297 | ASSERT(i == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1298 | |
| 1299 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1300 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1301 | |
| 1302 | out_brelse: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1303 | xfs_trans_brelse(tp, bp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1304 | out: |
| 1305 | xfs_iext_destroy(ifp); |
| 1306 | return error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1307 | } |
| 1308 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1309 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1310 | * Returns the relative block number of the first unused block(s) in the given |
| 1311 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1312 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1313 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1314 | */ |
| 1315 | int /* error */ |
| 1316 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1317 | struct xfs_trans *tp, /* transaction pointer */ |
| 1318 | struct xfs_inode *ip, /* incore inode */ |
| 1319 | xfs_extlen_t len, /* size of hole to find */ |
| 1320 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1321 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1322 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1323 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1324 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1325 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1326 | xfs_fileoff_t lastaddr = 0; |
| 1327 | xfs_fileoff_t lowest, max; |
| 1328 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1329 | |
| 1330 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1331 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1332 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1333 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1334 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1335 | *first_unused = 0; |
| 1336 | return 0; |
| 1337 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1338 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1339 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1340 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1341 | if (error) |
| 1342 | return error; |
| 1343 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1344 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1345 | lowest = max = *first_unused; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1346 | for_each_xfs_iext(ifp, &icur, &got) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1347 | /* |
| 1348 | * See if the hole before this extent will work. |
| 1349 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1350 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1351 | got.br_startoff - max >= len) |
| 1352 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1353 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1354 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1355 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1356 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1357 | *first_unused = max; |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1362 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1363 | * last_block (input value) in the file. |
| 1364 | * This is not based on i_size, it is based on the extent records. |
| 1365 | * Returns 0 for local files, as they do not have extent records. |
| 1366 | */ |
| 1367 | int /* error */ |
| 1368 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1369 | struct xfs_trans *tp, /* transaction pointer */ |
| 1370 | struct xfs_inode *ip, /* incore inode */ |
| 1371 | xfs_fileoff_t *last_block, /* last block */ |
| 1372 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1373 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1374 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1375 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1376 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1377 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1378 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1379 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1380 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1381 | *last_block = 0; |
| 1382 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1383 | case XFS_DINODE_FMT_BTREE: |
| 1384 | case XFS_DINODE_FMT_EXTENTS: |
| 1385 | break; |
| 1386 | default: |
| 1387 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1388 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1389 | |
| 1390 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1391 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1392 | if (error) |
| 1393 | return error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1394 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1395 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1396 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1397 | *last_block = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1398 | return 0; |
| 1399 | } |
| 1400 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1401 | int |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1402 | xfs_bmap_last_extent( |
| 1403 | struct xfs_trans *tp, |
| 1404 | struct xfs_inode *ip, |
| 1405 | int whichfork, |
| 1406 | struct xfs_bmbt_irec *rec, |
| 1407 | int *is_empty) |
| 1408 | { |
| 1409 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1410 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1411 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1412 | |
| 1413 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1414 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1415 | if (error) |
| 1416 | return error; |
| 1417 | } |
| 1418 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1419 | xfs_iext_last(ifp, &icur); |
| 1420 | if (!xfs_iext_get_extent(ifp, &icur, rec)) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1421 | *is_empty = 1; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1422 | else |
| 1423 | *is_empty = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1424 | return 0; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * Check the last inode extent to determine whether this allocation will result |
| 1429 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1430 | * blocks at the end of the file which do not start at the previous data block, |
| 1431 | * we will try to align the new blocks at stripe unit boundaries. |
| 1432 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1433 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1434 | * at, or past the EOF. |
| 1435 | */ |
| 1436 | STATIC int |
| 1437 | xfs_bmap_isaeof( |
| 1438 | struct xfs_bmalloca *bma, |
| 1439 | int whichfork) |
| 1440 | { |
| 1441 | struct xfs_bmbt_irec rec; |
| 1442 | int is_empty; |
| 1443 | int error; |
| 1444 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1445 | bma->aeof = false; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1446 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1447 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1448 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1449 | return error; |
| 1450 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1451 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1452 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1456 | /* |
| 1457 | * Check if we are allocation or past the last extent, or at least into |
| 1458 | * the last delayed allocated extent. |
| 1459 | */ |
| 1460 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1461 | (bma->offset >= rec.br_startoff && |
| 1462 | isnullstartblock(rec.br_startblock)); |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1467 | * Returns the file-relative block number of the first block past eof in |
| 1468 | * the file. This is not based on i_size, it is based on the extent records. |
| 1469 | * Returns 0 for local files, as they do not have extent records. |
| 1470 | */ |
| 1471 | int |
| 1472 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1473 | struct xfs_inode *ip, |
| 1474 | xfs_fileoff_t *last_block, |
| 1475 | int whichfork) |
| 1476 | { |
| 1477 | struct xfs_bmbt_irec rec; |
| 1478 | int is_empty; |
| 1479 | int error; |
| 1480 | |
| 1481 | *last_block = 0; |
| 1482 | |
| 1483 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1484 | return 0; |
| 1485 | |
| 1486 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1487 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1488 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1489 | |
| 1490 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1491 | if (error || is_empty) |
| 1492 | return error; |
| 1493 | |
| 1494 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
| 1498 | /* |
| 1499 | * Returns whether the selected fork of the inode has exactly one |
| 1500 | * block or not. For the data fork we check this matches di_size, |
| 1501 | * implying the file's range is 0..bsize-1. |
| 1502 | */ |
| 1503 | int /* 1=>1 block, 0=>otherwise */ |
| 1504 | xfs_bmap_one_block( |
| 1505 | xfs_inode_t *ip, /* incore inode */ |
| 1506 | int whichfork) /* data or attr fork */ |
| 1507 | { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1508 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1509 | int rval; /* return value */ |
| 1510 | xfs_bmbt_irec_t s; /* internal version of extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1511 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1512 | |
| 1513 | #ifndef DEBUG |
| 1514 | if (whichfork == XFS_DATA_FORK) |
| 1515 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1516 | #endif /* !DEBUG */ |
| 1517 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1518 | return 0; |
| 1519 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1520 | return 0; |
| 1521 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1522 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1523 | xfs_iext_first(ifp, &icur); |
| 1524 | xfs_iext_get_extent(ifp, &icur, &s); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1525 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1526 | if (rval && whichfork == XFS_DATA_FORK) |
| 1527 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1528 | return rval; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * Extent tree manipulation functions used during allocation. |
| 1533 | */ |
| 1534 | |
| 1535 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1536 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | */ |
| 1538 | STATIC int /* error */ |
| 1539 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1540 | struct xfs_bmalloca *bma, |
| 1541 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1543 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1546 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1548 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1549 | /* left is 0, right is 1, prev is 2 */ |
| 1550 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1551 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1552 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1553 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1554 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1556 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1557 | xfs_extnum_t *nextents; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1558 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1560 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1561 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1562 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1563 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1564 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1565 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1566 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1567 | ASSERT(!bma->cur || |
| 1568 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1569 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1570 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | #define LEFT r[0] |
| 1573 | #define RIGHT r[1] |
| 1574 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | |
| 1576 | /* |
| 1577 | * Set up a bunch of variables to make the tests simpler. |
| 1578 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1579 | xfs_iext_get_extent(ifp, &bma->icur, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1581 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1583 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1584 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1585 | da_old = startblockval(PREV.br_startblock); |
| 1586 | da_new = 0; |
| 1587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | /* |
| 1589 | * Set flags determining what part of the previous delayed allocation |
| 1590 | * extent is being replaced by a real allocation. |
| 1591 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1592 | if (PREV.br_startoff == new->br_startoff) |
| 1593 | state |= BMAP_LEFT_FILLING; |
| 1594 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1595 | state |= BMAP_RIGHT_FILLING; |
| 1596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | /* |
| 1598 | * Check and set flags if this segment has a left neighbor. |
| 1599 | * Don't set contiguous if the combined extent would be too large. |
| 1600 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1601 | if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1602 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1603 | if (isnullstartblock(LEFT.br_startblock)) |
| 1604 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1606 | |
| 1607 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1608 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1609 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1610 | LEFT.br_state == new->br_state && |
| 1611 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1612 | state |= BMAP_LEFT_CONTIG; |
| 1613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | /* |
| 1615 | * Check and set flags if this segment has a right neighbor. |
| 1616 | * Don't set contiguous if the combined extent would be too large. |
| 1617 | * Also check for all-three-contiguous being too large. |
| 1618 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1619 | if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1620 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1621 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1622 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1624 | |
| 1625 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1626 | new_endoff == RIGHT.br_startoff && |
| 1627 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1628 | new->br_state == RIGHT.br_state && |
| 1629 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1630 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1631 | BMAP_RIGHT_FILLING)) != |
| 1632 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1633 | BMAP_RIGHT_FILLING) || |
| 1634 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1635 | <= MAXEXTLEN)) |
| 1636 | state |= BMAP_RIGHT_CONTIG; |
| 1637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | error = 0; |
| 1639 | /* |
| 1640 | * Switch out based on the FILLING and CONTIG state bits. |
| 1641 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1642 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1643 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1644 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1645 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | /* |
| 1647 | * Filling in all of a previously delayed allocation extent. |
| 1648 | * The left and right neighbors are both contiguous with new. |
| 1649 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1650 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1651 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1652 | xfs_iext_remove(bma->ip, &bma->icur, state); |
| 1653 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1654 | xfs_iext_prev(ifp, &bma->icur); |
| 1655 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1656 | (*nextents)--; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1657 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1658 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1660 | else { |
| 1661 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1662 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1663 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | goto done; |
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_delete(bma->cur, &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 | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1670 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1671 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1673 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1674 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1675 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | goto done; |
| 1677 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | break; |
| 1679 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1680 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | /* |
| 1682 | * Filling in all of a previously delayed allocation extent. |
| 1683 | * The left neighbor is contiguous, the right is not. |
| 1684 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1685 | old = LEFT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1686 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1687 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1688 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1689 | xfs_iext_prev(ifp, &bma->icur); |
| 1690 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1691 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1692 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | rval = XFS_ILOG_DEXT; |
| 1694 | else { |
| 1695 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1696 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
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; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1699 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1700 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1701 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | goto done; |
| 1703 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | break; |
| 1705 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1706 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | /* |
| 1708 | * Filling in all of a previously delayed allocation extent. |
| 1709 | * The right neighbor is contiguous, the left is not. |
| 1710 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1711 | PREV.br_startblock = new->br_startblock; |
| 1712 | PREV.br_blockcount += RIGHT.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1713 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1714 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1715 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1716 | xfs_iext_prev(ifp, &bma->icur); |
| 1717 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1718 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1719 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | rval = XFS_ILOG_DEXT; |
| 1721 | else { |
| 1722 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1723 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
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; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1726 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1727 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1728 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | goto done; |
| 1730 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | break; |
| 1732 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1733 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | /* |
| 1735 | * Filling in all of a previously delayed allocation extent. |
| 1736 | * Neither the left nor right neighbors are contiguous with |
| 1737 | * the new one. |
| 1738 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1739 | PREV.br_startblock = new->br_startblock; |
| 1740 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1741 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1742 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1743 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1744 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1746 | else { |
| 1747 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1748 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 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 == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1752 | error = xfs_btree_insert(bma->cur, &i); |
| 1753 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1755 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | break; |
| 1758 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1759 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | /* |
| 1761 | * Filling in the first part of a previous delayed allocation. |
| 1762 | * The left neighbor is contiguous. |
| 1763 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1764 | old = LEFT; |
| 1765 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1766 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1767 | startblockval(PREV.br_startblock)); |
| 1768 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1769 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1770 | |
Christoph Hellwig | bf99971 | 2017-11-03 10:34:38 -0700 | [diff] [blame] | 1771 | PREV.br_blockcount = temp; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1772 | PREV.br_startoff += new->br_blockcount; |
| 1773 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1774 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1775 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1776 | xfs_iext_prev(ifp, &bma->icur); |
| 1777 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1778 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1779 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | rval = XFS_ILOG_DEXT; |
| 1781 | else { |
| 1782 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1783 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
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; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1786 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1787 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1788 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | goto done; |
| 1790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | break; |
| 1792 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1793 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | /* |
| 1795 | * Filling in the first part of a previous delayed allocation. |
| 1796 | * The left neighbor is not contiguous. |
| 1797 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1798 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1799 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1800 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1802 | else { |
| 1803 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1804 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 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 == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1808 | error = xfs_btree_insert(bma->cur, &i); |
| 1809 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1811 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1813 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1814 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1815 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1816 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1817 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | rval |= tmp_rval; |
| 1819 | if (error) |
| 1820 | goto done; |
| 1821 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1822 | |
| 1823 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1824 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1825 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1826 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1827 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1828 | PREV.br_startoff = new_endoff; |
| 1829 | PREV.br_blockcount = temp; |
| 1830 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1831 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1832 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1833 | xfs_iext_prev(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | break; |
| 1835 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1836 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | /* |
| 1838 | * Filling in the last part of a previous delayed allocation. |
| 1839 | * The right neighbor is contiguous with the new allocation. |
| 1840 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1841 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1842 | RIGHT.br_startoff = new->br_startoff; |
| 1843 | RIGHT.br_startblock = new->br_startblock; |
| 1844 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1845 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1846 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | rval = XFS_ILOG_DEXT; |
| 1848 | else { |
| 1849 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1850 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
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; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1853 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1854 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1855 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | goto done; |
| 1857 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1858 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1859 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1860 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1861 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1862 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1863 | PREV.br_blockcount = temp; |
| 1864 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1865 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1866 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1867 | xfs_iext_next(ifp, &bma->icur); |
| 1868 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | break; |
| 1870 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1871 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | /* |
| 1873 | * Filling in the last part of a previous delayed allocation. |
| 1874 | * The right neighbor is not contiguous. |
| 1875 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1876 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1877 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1878 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1880 | else { |
| 1881 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1882 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 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 == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1886 | error = xfs_btree_insert(bma->cur, &i); |
| 1887 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1889 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1891 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1892 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1893 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1894 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1895 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | rval |= tmp_rval; |
| 1897 | if (error) |
| 1898 | goto done; |
| 1899 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1900 | |
| 1901 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1902 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1903 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1904 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1905 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1906 | PREV.br_startblock = nullstartblock(da_new); |
| 1907 | PREV.br_blockcount = temp; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1908 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1909 | xfs_iext_next(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | break; |
| 1911 | |
| 1912 | case 0: |
| 1913 | /* |
| 1914 | * Filling in the middle part of a previous delayed allocation. |
| 1915 | * Contiguity is impossible here. |
| 1916 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1917 | * |
| 1918 | * We start with a delayed allocation: |
| 1919 | * |
| 1920 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1921 | * PREV @ idx |
| 1922 | * |
| 1923 | * and we are allocating: |
| 1924 | * +rrrrrrrrrrrrrrrrr+ |
| 1925 | * new |
| 1926 | * |
| 1927 | * and we set it up for insertion as: |
| 1928 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1929 | * new |
| 1930 | * PREV @ idx LEFT RIGHT |
| 1931 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1933 | old = PREV; |
| 1934 | |
| 1935 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1936 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1937 | |
| 1938 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1939 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1940 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1941 | RIGHT.br_blockcount = |
| 1942 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1943 | RIGHT.br_startblock = |
| 1944 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1945 | RIGHT.br_blockcount)); |
| 1946 | |
| 1947 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1948 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1949 | PREV.br_startblock = |
| 1950 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1951 | PREV.br_blockcount)); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1952 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1953 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1954 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1955 | xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); |
| 1956 | xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1957 | (*nextents)++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1958 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1959 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1961 | else { |
| 1962 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1963 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 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 == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1967 | error = xfs_btree_insert(bma->cur, &i); |
| 1968 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1970 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1972 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1973 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1974 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1975 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1976 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | rval |= tmp_rval; |
| 1978 | if (error) |
| 1979 | goto done; |
| 1980 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1981 | |
| 1982 | da_new = startblockval(PREV.br_startblock) + |
| 1983 | startblockval(RIGHT.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | break; |
| 1985 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1986 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1987 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1988 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1989 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1990 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1991 | case BMAP_LEFT_CONTIG: |
| 1992 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | /* |
| 1994 | * These cases are all impossible. |
| 1995 | */ |
| 1996 | ASSERT(0); |
| 1997 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1998 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 1999 | /* add reverse mapping */ |
| 2000 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 2001 | if (error) |
| 2002 | goto done; |
| 2003 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2004 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2005 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2006 | int tmp_logflags; /* partial log flag return val */ |
| 2007 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2008 | ASSERT(bma->cur == NULL); |
| 2009 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2010 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2011 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2012 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2013 | if (error) |
| 2014 | goto done; |
| 2015 | } |
| 2016 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2017 | if (bma->cur) { |
| 2018 | da_new += bma->cur->bc_private.b.allocated; |
| 2019 | bma->cur->bc_private.b.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2022 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2023 | if (da_new != da_old) { |
| 2024 | ASSERT(state == 0 || da_new < da_old); |
| 2025 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2026 | false); |
| 2027 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2028 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2029 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2031 | if (whichfork != XFS_COW_FORK) |
| 2032 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | return error; |
| 2034 | #undef LEFT |
| 2035 | #undef RIGHT |
| 2036 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2040 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | */ |
| 2042 | STATIC int /* error */ |
| 2043 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2044 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2046 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2047 | struct xfs_iext_cursor *icur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2049 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2050 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2051 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2052 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2057 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2060 | /* left is 0, right is 1, prev is 2 */ |
| 2061 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2062 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2063 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2064 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2066 | *logflagsp = 0; |
| 2067 | |
| 2068 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2069 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2070 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2071 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2072 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2073 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | #define LEFT r[0] |
| 2076 | #define RIGHT r[1] |
| 2077 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | /* |
| 2080 | * Set up a bunch of variables to make the tests simpler. |
| 2081 | */ |
| 2082 | error = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2083 | xfs_iext_get_extent(ifp, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2084 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2086 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2087 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | /* |
| 2090 | * Set flags determining what part of the previous oldext allocation |
| 2091 | * extent is being replaced by a newext allocation. |
| 2092 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2093 | if (PREV.br_startoff == new->br_startoff) |
| 2094 | state |= BMAP_LEFT_FILLING; |
| 2095 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2096 | state |= BMAP_RIGHT_FILLING; |
| 2097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | /* |
| 2099 | * Check and set flags if this segment has a left neighbor. |
| 2100 | * Don't set contiguous if the combined extent would be too large. |
| 2101 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2102 | if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2103 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2104 | if (isnullstartblock(LEFT.br_startblock)) |
| 2105 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2107 | |
| 2108 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2109 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2110 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2111 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2112 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2113 | state |= BMAP_LEFT_CONTIG; |
| 2114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | /* |
| 2116 | * Check and set flags if this segment has a right neighbor. |
| 2117 | * Don't set contiguous if the combined extent would be too large. |
| 2118 | * Also check for all-three-contiguous being too large. |
| 2119 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2120 | if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2121 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2122 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2123 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2125 | |
| 2126 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2127 | new_endoff == RIGHT.br_startoff && |
| 2128 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2129 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2130 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2131 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2132 | BMAP_RIGHT_FILLING)) != |
| 2133 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2134 | BMAP_RIGHT_FILLING) || |
| 2135 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2136 | <= MAXEXTLEN)) |
| 2137 | state |= BMAP_RIGHT_CONTIG; |
| 2138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | /* |
| 2140 | * Switch out based on the FILLING and CONTIG state bits. |
| 2141 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2142 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2143 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2144 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2145 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | /* |
| 2147 | * Setting all of a previous oldext extent to newext. |
| 2148 | * The left and right neighbors are both contiguous with new. |
| 2149 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2150 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2151 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2152 | xfs_iext_remove(ip, icur, state); |
| 2153 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2154 | xfs_iext_prev(ifp, icur); |
| 2155 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2156 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2157 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | if (cur == NULL) |
| 2159 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2160 | else { |
| 2161 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2162 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2163 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2165 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2166 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2168 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2169 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2171 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2172 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2174 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2175 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2177 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2178 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2179 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | goto done; |
| 2181 | } |
| 2182 | break; |
| 2183 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2184 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | /* |
| 2186 | * Setting all of a previous oldext extent to newext. |
| 2187 | * The left neighbor is contiguous, the right is not. |
| 2188 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2189 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2190 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2191 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2192 | xfs_iext_prev(ifp, icur); |
| 2193 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2194 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2195 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | if (cur == NULL) |
| 2197 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2198 | else { |
| 2199 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2200 | error = xfs_bmbt_lookup_eq(cur, &PREV, &i); |
| 2201 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2203 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2204 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2206 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2207 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2209 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2210 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2211 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | goto done; |
| 2213 | } |
| 2214 | break; |
| 2215 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2216 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | /* |
| 2218 | * Setting all of a previous oldext extent to newext. |
| 2219 | * The right neighbor is contiguous, the left is not. |
| 2220 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2221 | PREV.br_blockcount += RIGHT.br_blockcount; |
| 2222 | PREV.br_state = new->br_state; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2223 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2224 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2225 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2226 | xfs_iext_prev(ifp, icur); |
| 2227 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2228 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2229 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2230 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | if (cur == NULL) |
| 2232 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2233 | else { |
| 2234 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2235 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2236 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2238 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2239 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2241 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2242 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2244 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2245 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2246 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | goto done; |
| 2248 | } |
| 2249 | break; |
| 2250 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2251 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | /* |
| 2253 | * Setting all of a previous oldext extent to newext. |
| 2254 | * Neither the left nor right neighbors are contiguous with |
| 2255 | * the new one. |
| 2256 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2257 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2258 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | if (cur == NULL) |
| 2261 | rval = XFS_ILOG_DEXT; |
| 2262 | else { |
| 2263 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2264 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2265 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2267 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2268 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2269 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | goto done; |
| 2271 | } |
| 2272 | break; |
| 2273 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2274 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | /* |
| 2276 | * Setting the first part of a previous oldext extent to newext. |
| 2277 | * The left neighbor is contiguous. |
| 2278 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2279 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2280 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2281 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2282 | PREV.br_startoff += new->br_blockcount; |
| 2283 | PREV.br_startblock += new->br_blockcount; |
| 2284 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2285 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2286 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2287 | xfs_iext_prev(ifp, icur); |
| 2288 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | if (cur == NULL) |
| 2291 | rval = XFS_ILOG_DEXT; |
| 2292 | else { |
| 2293 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2294 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2295 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2297 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2298 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2299 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2301 | error = xfs_btree_decrement(cur, 0, &i); |
| 2302 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2304 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2305 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | goto done; |
| 2307 | } |
| 2308 | break; |
| 2309 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2310 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | /* |
| 2312 | * Setting the first part of a previous oldext extent to newext. |
| 2313 | * The left neighbor is not contiguous. |
| 2314 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2315 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2316 | PREV.br_startoff += new->br_blockcount; |
| 2317 | PREV.br_startblock += new->br_blockcount; |
| 2318 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2319 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2320 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2321 | xfs_iext_insert(ip, icur, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2322 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2323 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | if (cur == NULL) |
| 2325 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2326 | else { |
| 2327 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2328 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2329 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2331 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2332 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2333 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | goto done; |
| 2335 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2336 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2338 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | } |
| 2340 | break; |
| 2341 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2342 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | /* |
| 2344 | * Setting the last part of a previous oldext extent to newext. |
| 2345 | * The right neighbor is contiguous with the new allocation. |
| 2346 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2347 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2348 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2349 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2350 | RIGHT.br_startoff = new->br_startoff; |
| 2351 | RIGHT.br_startblock = new->br_startblock; |
| 2352 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2353 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2354 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2355 | xfs_iext_next(ifp, icur); |
| 2356 | xfs_iext_update_extent(ip, state, icur, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | if (cur == NULL) |
| 2359 | rval = XFS_ILOG_DEXT; |
| 2360 | else { |
| 2361 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2362 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2363 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2365 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2366 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2367 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2369 | error = xfs_btree_increment(cur, 0, &i); |
| 2370 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2372 | error = xfs_bmbt_update(cur, &RIGHT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2373 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | goto done; |
| 2375 | } |
| 2376 | break; |
| 2377 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2378 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | /* |
| 2380 | * Setting the last part of a previous oldext extent to newext. |
| 2381 | * The right neighbor is not contiguous. |
| 2382 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2383 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2384 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2385 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2386 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2387 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2388 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2389 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2390 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2391 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | if (cur == NULL) |
| 2393 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2394 | else { |
| 2395 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2396 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2397 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2399 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2400 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2401 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2403 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2404 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2406 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2407 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2409 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | } |
| 2411 | break; |
| 2412 | |
| 2413 | case 0: |
| 2414 | /* |
| 2415 | * Setting the middle part of a previous oldext extent to |
| 2416 | * newext. Contiguity is impossible here. |
| 2417 | * One extent becomes three extents. |
| 2418 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2419 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2420 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | r[0] = *new; |
| 2423 | r[1].br_startoff = new_endoff; |
| 2424 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2425 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2427 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2428 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2429 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2430 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2431 | xfs_iext_insert(ip, icur, &r[1], state); |
| 2432 | xfs_iext_insert(ip, icur, &r[0], state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2433 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2434 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2435 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | if (cur == NULL) |
| 2437 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2438 | else { |
| 2439 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2440 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2441 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2443 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2445 | error = xfs_bmbt_update(cur, &r[1]); |
| 2446 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | goto done; |
| 2448 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2450 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2452 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2453 | /* |
| 2454 | * Reset the cursor to the position of the new extent |
| 2455 | * we are about to insert as we can't trust it after |
| 2456 | * the previous insert. |
| 2457 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2458 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2459 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2461 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2463 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2465 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | } |
| 2467 | break; |
| 2468 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2469 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2470 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2471 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2472 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2473 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2474 | case BMAP_LEFT_CONTIG: |
| 2475 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | /* |
| 2477 | * These cases are all impossible. |
| 2478 | */ |
| 2479 | ASSERT(0); |
| 2480 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2481 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2482 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2483 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2484 | if (error) |
| 2485 | goto done; |
| 2486 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2487 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2488 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2489 | int tmp_logflags; /* partial log flag return val */ |
| 2490 | |
| 2491 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2492 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2493 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2494 | *logflagsp |= tmp_logflags; |
| 2495 | if (error) |
| 2496 | goto done; |
| 2497 | } |
| 2498 | |
| 2499 | /* clear out the allocated field, done with it now in any case. */ |
| 2500 | if (cur) { |
| 2501 | cur->bc_private.b.allocated = 0; |
| 2502 | *curp = cur; |
| 2503 | } |
| 2504 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2505 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2507 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | return error; |
| 2509 | #undef LEFT |
| 2510 | #undef RIGHT |
| 2511 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2515 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2517 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | xfs_bmap_add_extent_hole_delay( |
| 2519 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2520 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2521 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2522 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2524 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2526 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2527 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2528 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2529 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2530 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2532 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2533 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | /* |
| 2536 | * Check and set flags if this segment has a left neighbor |
| 2537 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2538 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2539 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2540 | if (isnullstartblock(left.br_startblock)) |
| 2541 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | /* |
| 2545 | * Check and set flags if the current (right) segment exists. |
| 2546 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2547 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2548 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2549 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2550 | if (isnullstartblock(right.br_startblock)) |
| 2551 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | /* |
| 2555 | * Set contiguity flags on the left and right neighbors. |
| 2556 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2557 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2558 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2559 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2560 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2561 | state |= BMAP_LEFT_CONTIG; |
| 2562 | |
| 2563 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2564 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2565 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2566 | (!(state & BMAP_LEFT_CONTIG) || |
| 2567 | (left.br_blockcount + new->br_blockcount + |
| 2568 | right.br_blockcount <= MAXEXTLEN))) |
| 2569 | state |= BMAP_RIGHT_CONTIG; |
| 2570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | /* |
| 2572 | * Switch out based on the contiguity flags. |
| 2573 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2574 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2575 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | /* |
| 2577 | * New allocation is contiguous with delayed allocations |
| 2578 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2579 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | */ |
| 2581 | temp = left.br_blockcount + new->br_blockcount + |
| 2582 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2583 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2584 | oldlen = startblockval(left.br_startblock) + |
| 2585 | startblockval(new->br_startblock) + |
| 2586 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2587 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2588 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2589 | left.br_startblock = nullstartblock(newlen); |
| 2590 | left.br_blockcount = temp; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2591 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2592 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2593 | xfs_iext_prev(ifp, icur); |
| 2594 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | break; |
| 2596 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2597 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | /* |
| 2599 | * New allocation is contiguous with a delayed allocation |
| 2600 | * on the left. |
| 2601 | * Merge the new allocation with the left neighbor. |
| 2602 | */ |
| 2603 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2604 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2605 | oldlen = startblockval(left.br_startblock) + |
| 2606 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2607 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2608 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2609 | left.br_blockcount = temp; |
| 2610 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | 41d196f | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 2611 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2612 | xfs_iext_prev(ifp, icur); |
| 2613 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | break; |
| 2615 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2616 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | /* |
| 2618 | * New allocation is contiguous with a delayed allocation |
| 2619 | * on the right. |
| 2620 | * Merge the new allocation with the right neighbor. |
| 2621 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2623 | oldlen = startblockval(new->br_startblock) + |
| 2624 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2625 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2626 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2627 | right.br_startoff = new->br_startoff; |
| 2628 | right.br_startblock = nullstartblock(newlen); |
| 2629 | right.br_blockcount = temp; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2630 | xfs_iext_update_extent(ip, state, icur, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | break; |
| 2632 | |
| 2633 | case 0: |
| 2634 | /* |
| 2635 | * New allocation is not contiguous with another |
| 2636 | * delayed allocation. |
| 2637 | * Insert a new entry. |
| 2638 | */ |
| 2639 | oldlen = newlen = 0; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2640 | xfs_iext_insert(ip, icur, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | break; |
| 2642 | } |
| 2643 | if (oldlen != newlen) { |
| 2644 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2645 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2646 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | /* |
| 2648 | * Nothing to do for disk quota accounting here. |
| 2649 | */ |
| 2650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2654 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | */ |
| 2656 | STATIC int /* error */ |
| 2657 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2658 | struct xfs_trans *tp, |
| 2659 | struct xfs_inode *ip, |
| 2660 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2661 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2662 | struct xfs_btree_cur **curp, |
| 2663 | struct xfs_bmbt_irec *new, |
| 2664 | xfs_fsblock_t *first, |
| 2665 | struct xfs_defer_ops *dfops, |
| 2666 | int *logflagsp) |
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 | ca5d8e5 | 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 | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2843 | /* add reverse mapping */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2844 | error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2845 | if (error) |
| 2846 | goto done; |
| 2847 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2848 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2849 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2850 | int tmp_logflags; /* partial log flag return val */ |
| 2851 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2852 | ASSERT(cur == NULL); |
| 2853 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2854 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2855 | *logflagsp |= tmp_logflags; |
| 2856 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2857 | if (error) |
| 2858 | goto done; |
| 2859 | } |
| 2860 | |
| 2861 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2862 | if (cur) |
| 2863 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2864 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2865 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2866 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2867 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2868 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2871 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2872 | * Functions used in the extent read, allocate and remove paths |
| 2873 | */ |
| 2874 | |
| 2875 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2876 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2877 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2878 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2879 | xfs_bmap_extsize_align( |
| 2880 | xfs_mount_t *mp, |
| 2881 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2882 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2883 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2884 | int rt, /* is this a realtime inode? */ |
| 2885 | int eof, /* is extent at end-of-file? */ |
| 2886 | int delay, /* creating delalloc extent? */ |
| 2887 | int convert, /* overwriting unwritten extent? */ |
| 2888 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2889 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2890 | { |
| 2891 | xfs_fileoff_t orig_off; /* original offset */ |
| 2892 | xfs_extlen_t orig_alen; /* original length */ |
| 2893 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2894 | xfs_fileoff_t nexto; /* next file offset */ |
| 2895 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2896 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2897 | xfs_extlen_t align_alen; /* temp for length */ |
| 2898 | xfs_extlen_t temp; /* temp for calculations */ |
| 2899 | |
| 2900 | if (convert) |
| 2901 | return 0; |
| 2902 | |
| 2903 | orig_off = align_off = *offp; |
| 2904 | orig_alen = align_alen = *lenp; |
| 2905 | orig_end = orig_off + orig_alen; |
| 2906 | |
| 2907 | /* |
| 2908 | * If this request overlaps an existing extent, then don't |
| 2909 | * attempt to perform any additional alignment. |
| 2910 | */ |
| 2911 | if (!delay && !eof && |
| 2912 | (orig_off >= gotp->br_startoff) && |
| 2913 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2914 | return 0; |
| 2915 | } |
| 2916 | |
| 2917 | /* |
| 2918 | * If the file offset is unaligned vs. the extent size |
| 2919 | * we need to align it. This will be possible unless |
| 2920 | * the file was previously written with a kernel that didn't |
| 2921 | * perform this alignment, or if a truncate shot us in the |
| 2922 | * foot. |
| 2923 | */ |
| 2924 | temp = do_mod(orig_off, extsz); |
| 2925 | if (temp) { |
| 2926 | align_alen += temp; |
| 2927 | align_off -= temp; |
| 2928 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2929 | |
| 2930 | /* Same adjustment for the end of the requested area. */ |
| 2931 | temp = (align_alen % extsz); |
| 2932 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2933 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2934 | |
| 2935 | /* |
| 2936 | * For large extent hint sizes, the aligned extent might be larger than |
| 2937 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 2938 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 2939 | * short allocation just fine, so it is safe to do this. We only want to |
| 2940 | * do it when we are forced to, though, because it means more allocation |
| 2941 | * operations are required. |
| 2942 | */ |
| 2943 | while (align_alen > MAXEXTLEN) |
| 2944 | align_alen -= extsz; |
| 2945 | ASSERT(align_alen <= MAXEXTLEN); |
| 2946 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2947 | /* |
| 2948 | * If the previous block overlaps with this proposed allocation |
| 2949 | * then move the start forward without adjusting the length. |
| 2950 | */ |
| 2951 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2952 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2953 | prevo = prevp->br_startoff; |
| 2954 | else |
| 2955 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2956 | } else |
| 2957 | prevo = 0; |
| 2958 | if (align_off != orig_off && align_off < prevo) |
| 2959 | align_off = prevo; |
| 2960 | /* |
| 2961 | * If the next block overlaps with this proposed allocation |
| 2962 | * then move the start back without adjusting the length, |
| 2963 | * but not before offset 0. |
| 2964 | * This may of course make the start overlap previous block, |
| 2965 | * and if we hit the offset 0 limit then the next block |
| 2966 | * can still overlap too. |
| 2967 | */ |
| 2968 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2969 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2970 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2971 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2972 | else |
| 2973 | nexto = gotp->br_startoff; |
| 2974 | } else |
| 2975 | nexto = NULLFILEOFF; |
| 2976 | if (!eof && |
| 2977 | align_off + align_alen != orig_end && |
| 2978 | align_off + align_alen > nexto) |
| 2979 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2980 | /* |
| 2981 | * If we're now overlapping the next or previous extent that |
| 2982 | * means we can't fit an extsz piece in this hole. Just move |
| 2983 | * the start forward to the first valid spot and set |
| 2984 | * the length so we hit the end. |
| 2985 | */ |
| 2986 | if (align_off != orig_off && align_off < prevo) |
| 2987 | align_off = prevo; |
| 2988 | if (align_off + align_alen != orig_end && |
| 2989 | align_off + align_alen > nexto && |
| 2990 | nexto != NULLFILEOFF) { |
| 2991 | ASSERT(nexto > prevo); |
| 2992 | align_alen = nexto - align_off; |
| 2993 | } |
| 2994 | |
| 2995 | /* |
| 2996 | * If realtime, and the result isn't a multiple of the realtime |
| 2997 | * extent size we need to remove blocks until it is. |
| 2998 | */ |
| 2999 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3000 | /* |
| 3001 | * We're not covering the original request, or |
| 3002 | * we won't be able to once we fix the length. |
| 3003 | */ |
| 3004 | if (orig_off < align_off || |
| 3005 | orig_end > align_off + align_alen || |
| 3006 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3007 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3008 | /* |
| 3009 | * Try to fix it by moving the start up. |
| 3010 | */ |
| 3011 | if (align_off + temp <= orig_off) { |
| 3012 | align_alen -= temp; |
| 3013 | align_off += temp; |
| 3014 | } |
| 3015 | /* |
| 3016 | * Try to fix it by moving the end in. |
| 3017 | */ |
| 3018 | else if (align_off + align_alen - temp >= orig_end) |
| 3019 | align_alen -= temp; |
| 3020 | /* |
| 3021 | * Set the start to the minimum then trim the length. |
| 3022 | */ |
| 3023 | else { |
| 3024 | align_alen -= orig_off - align_off; |
| 3025 | align_off = orig_off; |
| 3026 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3027 | } |
| 3028 | /* |
| 3029 | * Result doesn't cover the request, fail it. |
| 3030 | */ |
| 3031 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3032 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3033 | } else { |
| 3034 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3035 | /* see MAXEXTLEN handling above */ |
| 3036 | ASSERT(orig_end <= align_off + align_alen || |
| 3037 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | #ifdef DEBUG |
| 3041 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3042 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3043 | if (prevp->br_startoff != NULLFILEOFF) |
| 3044 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3045 | #endif |
| 3046 | |
| 3047 | *lenp = align_alen; |
| 3048 | *offp = align_off; |
| 3049 | return 0; |
| 3050 | } |
| 3051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3053 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3054 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3055 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3056 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | { |
| 3058 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3060 | xfs_mount_t *mp; /* mount point structure */ |
| 3061 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3062 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | |
| 3064 | #define ISVALID(x,y) \ |
| 3065 | (rt ? \ |
| 3066 | (x) < mp->m_sb.sb_rblocks : \ |
| 3067 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3068 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3069 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3072 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3073 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3074 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3075 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | /* |
| 3077 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3078 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3080 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3081 | !isnullstartblock(ap->prev.br_startblock) && |
| 3082 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3083 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3084 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | /* |
| 3086 | * Adjust for the gap between prevp and us. |
| 3087 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3088 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3089 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3091 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3092 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | } |
| 3094 | /* |
| 3095 | * If not at eof, then compare the two neighbor blocks. |
| 3096 | * Figure out whether either one gives us a good starting point, |
| 3097 | * and pick the better one. |
| 3098 | */ |
| 3099 | else if (!ap->eof) { |
| 3100 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3101 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3102 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3103 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3104 | |
| 3105 | /* |
| 3106 | * If there's a previous (left) block, select a requested |
| 3107 | * start block based on it. |
| 3108 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3109 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3110 | !isnullstartblock(ap->prev.br_startblock) && |
| 3111 | (prevbno = ap->prev.br_startblock + |
| 3112 | ap->prev.br_blockcount) && |
| 3113 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | /* |
| 3115 | * Calculate gap to end of previous block. |
| 3116 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3117 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3118 | (ap->prev.br_startoff + |
| 3119 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | /* |
| 3121 | * Figure the startblock based on the previous block's |
| 3122 | * end and the gap size. |
| 3123 | * Heuristic! |
| 3124 | * If the gap is large relative to the piece we're |
| 3125 | * allocating, or using it gives us an invalid block |
| 3126 | * number, then just use the end of the previous block. |
| 3127 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3128 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3130 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | prevbno += adjust; |
| 3132 | else |
| 3133 | prevdiff += adjust; |
| 3134 | /* |
| 3135 | * If the firstblock forbids it, can't use it, |
| 3136 | * must use default. |
| 3137 | */ |
| 3138 | if (!rt && !nullfb && |
| 3139 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3140 | prevbno = NULLFSBLOCK; |
| 3141 | } |
| 3142 | /* |
| 3143 | * No previous block or can't follow it, just default. |
| 3144 | */ |
| 3145 | else |
| 3146 | prevbno = NULLFSBLOCK; |
| 3147 | /* |
| 3148 | * If there's a following (right) block, select a requested |
| 3149 | * start block based on it. |
| 3150 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3151 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | /* |
| 3153 | * Calculate gap to start of next block. |
| 3154 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3155 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | /* |
| 3157 | * Figure the startblock based on the next block's |
| 3158 | * start and the gap size. |
| 3159 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3160 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | /* |
| 3162 | * Heuristic! |
| 3163 | * If the gap is large relative to the piece we're |
| 3164 | * allocating, or using it gives us an invalid block |
| 3165 | * number, then just use the start of the next block |
| 3166 | * offset by our length. |
| 3167 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3168 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3170 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3171 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3172 | gotbno -= ap->length; |
| 3173 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | } else |
| 3175 | gotdiff += adjust; |
| 3176 | /* |
| 3177 | * If the firstblock forbids it, can't use it, |
| 3178 | * must use default. |
| 3179 | */ |
| 3180 | if (!rt && !nullfb && |
| 3181 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3182 | gotbno = NULLFSBLOCK; |
| 3183 | } |
| 3184 | /* |
| 3185 | * No next block, just default. |
| 3186 | */ |
| 3187 | else |
| 3188 | gotbno = NULLFSBLOCK; |
| 3189 | /* |
| 3190 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3191 | * 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] | 3192 | */ |
| 3193 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3194 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3196 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3198 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3200 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3201 | } |
| 3202 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3203 | static int |
| 3204 | xfs_bmap_longest_free_extent( |
| 3205 | struct xfs_trans *tp, |
| 3206 | xfs_agnumber_t ag, |
| 3207 | xfs_extlen_t *blen, |
| 3208 | int *notinit) |
| 3209 | { |
| 3210 | struct xfs_mount *mp = tp->t_mountp; |
| 3211 | struct xfs_perag *pag; |
| 3212 | xfs_extlen_t longest; |
| 3213 | int error = 0; |
| 3214 | |
| 3215 | pag = xfs_perag_get(mp, ag); |
| 3216 | if (!pag->pagf_init) { |
| 3217 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3218 | if (error) |
| 3219 | goto out; |
| 3220 | |
| 3221 | if (!pag->pagf_init) { |
| 3222 | *notinit = 1; |
| 3223 | goto out; |
| 3224 | } |
| 3225 | } |
| 3226 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 3227 | longest = xfs_alloc_longest_free_extent(mp, pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3228 | xfs_alloc_min_freelist(mp, pag), |
| 3229 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3230 | if (*blen < longest) |
| 3231 | *blen = longest; |
| 3232 | |
| 3233 | out: |
| 3234 | xfs_perag_put(pag); |
| 3235 | return error; |
| 3236 | } |
| 3237 | |
| 3238 | static void |
| 3239 | xfs_bmap_select_minlen( |
| 3240 | struct xfs_bmalloca *ap, |
| 3241 | struct xfs_alloc_arg *args, |
| 3242 | xfs_extlen_t *blen, |
| 3243 | int notinit) |
| 3244 | { |
| 3245 | if (notinit || *blen < ap->minlen) { |
| 3246 | /* |
| 3247 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3248 | * there is space for this request. |
| 3249 | */ |
| 3250 | args->minlen = ap->minlen; |
| 3251 | } else if (*blen < args->maxlen) { |
| 3252 | /* |
| 3253 | * If the best seen length is less than the request length, |
| 3254 | * use the best as the minimum. |
| 3255 | */ |
| 3256 | args->minlen = *blen; |
| 3257 | } else { |
| 3258 | /* |
| 3259 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3260 | * as the minimum. |
| 3261 | */ |
| 3262 | args->minlen = args->maxlen; |
| 3263 | } |
| 3264 | } |
| 3265 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3266 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3267 | xfs_bmap_btalloc_nullfb( |
| 3268 | struct xfs_bmalloca *ap, |
| 3269 | struct xfs_alloc_arg *args, |
| 3270 | xfs_extlen_t *blen) |
| 3271 | { |
| 3272 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3273 | xfs_agnumber_t ag, startag; |
| 3274 | int notinit = 0; |
| 3275 | int error; |
| 3276 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3277 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3278 | args->total = ap->total; |
| 3279 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3280 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3281 | if (startag == NULLAGNUMBER) |
| 3282 | startag = ag = 0; |
| 3283 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3284 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3285 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3286 | ¬init); |
| 3287 | if (error) |
| 3288 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3289 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3290 | if (++ag == mp->m_sb.sb_agcount) |
| 3291 | ag = 0; |
| 3292 | if (ag == startag) |
| 3293 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3294 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3295 | |
| 3296 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3297 | return 0; |
| 3298 | } |
| 3299 | |
| 3300 | STATIC int |
| 3301 | xfs_bmap_btalloc_filestreams( |
| 3302 | struct xfs_bmalloca *ap, |
| 3303 | struct xfs_alloc_arg *args, |
| 3304 | xfs_extlen_t *blen) |
| 3305 | { |
| 3306 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3307 | xfs_agnumber_t ag; |
| 3308 | int notinit = 0; |
| 3309 | int error; |
| 3310 | |
| 3311 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3312 | args->total = ap->total; |
| 3313 | |
| 3314 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3315 | if (ag == NULLAGNUMBER) |
| 3316 | ag = 0; |
| 3317 | |
| 3318 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3319 | if (error) |
| 3320 | return error; |
| 3321 | |
| 3322 | if (*blen < args->maxlen) { |
| 3323 | error = xfs_filestream_new_ag(ap, &ag); |
| 3324 | if (error) |
| 3325 | return error; |
| 3326 | |
| 3327 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3328 | ¬init); |
| 3329 | if (error) |
| 3330 | return error; |
| 3331 | |
| 3332 | } |
| 3333 | |
| 3334 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3335 | |
| 3336 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3337 | * Set the failure fallback case to look in the selected AG as stream |
| 3338 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3339 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3340 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3341 | return 0; |
| 3342 | } |
| 3343 | |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3344 | /* Update all inode and quota accounting for the allocation we just did. */ |
| 3345 | static void |
| 3346 | xfs_bmap_btalloc_accounting( |
| 3347 | struct xfs_bmalloca *ap, |
| 3348 | struct xfs_alloc_arg *args) |
| 3349 | { |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3350 | if (ap->flags & XFS_BMAPI_COWFORK) { |
| 3351 | /* |
| 3352 | * COW fork blocks are in-core only and thus are treated as |
| 3353 | * in-core quota reservation (like delalloc blocks) even when |
| 3354 | * converted to real blocks. The quota reservation is not |
| 3355 | * accounted to disk until blocks are remapped to the data |
| 3356 | * fork. So if these blocks were previously delalloc, we |
| 3357 | * already have quota reservation and there's nothing to do |
| 3358 | * yet. |
| 3359 | */ |
| 3360 | if (ap->wasdel) |
| 3361 | return; |
| 3362 | |
| 3363 | /* |
| 3364 | * Otherwise, we've allocated blocks in a hole. The transaction |
| 3365 | * has acquired in-core quota reservation for this extent. |
| 3366 | * Rather than account these as real blocks, however, we reduce |
| 3367 | * the transaction quota reservation based on the allocation. |
| 3368 | * This essentially transfers the transaction quota reservation |
| 3369 | * to that of a delalloc extent. |
| 3370 | */ |
| 3371 | ap->ip->i_delayed_blks += args->len; |
| 3372 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS, |
| 3373 | -(long)args->len); |
| 3374 | return; |
| 3375 | } |
| 3376 | |
| 3377 | /* data/attr fork only */ |
| 3378 | ap->ip->i_d.di_nblocks += args->len; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3379 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3380 | if (ap->wasdel) |
| 3381 | ap->ip->i_delayed_blks -= args->len; |
| 3382 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 3383 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT, |
| 3384 | args->len); |
| 3385 | } |
| 3386 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3387 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3388 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3389 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3390 | { |
| 3391 | xfs_mount_t *mp; /* mount point structure */ |
| 3392 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3393 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3394 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3395 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3396 | xfs_alloc_arg_t args; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3397 | xfs_fileoff_t orig_offset; |
| 3398 | xfs_extlen_t orig_length; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3399 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3400 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3401 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3402 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3403 | int tryagain; |
| 3404 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3405 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3406 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3407 | ASSERT(ap->length); |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3408 | orig_offset = ap->offset; |
| 3409 | orig_length = ap->length; |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3410 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3411 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3412 | |
| 3413 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3414 | stripe_align = 0; |
| 3415 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3416 | stripe_align = mp->m_swidth; |
| 3417 | else if (mp->m_dalign) |
| 3418 | stripe_align = mp->m_dalign; |
| 3419 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3420 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3421 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3422 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3423 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3424 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3425 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3426 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3427 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3428 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3429 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3430 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3431 | |
| 3432 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3433 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3434 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3435 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3436 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3437 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3438 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3439 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3440 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3441 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3442 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3443 | } |
| 3444 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3445 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3446 | |
| 3447 | xfs_bmap_adjacent(ap); |
| 3448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3450 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | * it's in the right allocation group. |
| 3452 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3453 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | ; |
| 3455 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3456 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | * Normal allocation, done through xfs_alloc_vextent. |
| 3459 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3460 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3461 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3462 | args.tp = ap->tp; |
| 3463 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3464 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3465 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3466 | |
| 3467 | /* Trim the allocation back to the maximum an AG can fit. */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 3468 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3469 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3470 | blen = 0; |
| 3471 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3472 | /* |
| 3473 | * Search for an allocation group with a single extent large |
| 3474 | * enough for the request. If one isn't found, then adjust |
| 3475 | * the minimum allocation size to the largest space found. |
| 3476 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3477 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3478 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3479 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3480 | else |
| 3481 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3482 | if (error) |
| 3483 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3484 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3485 | if (xfs_inode_is_filestream(ap->ip)) |
| 3486 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3487 | else |
| 3488 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3489 | args.total = args.minlen = ap->minlen; |
| 3490 | } else { |
| 3491 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3492 | args.total = ap->total; |
| 3493 | args.minlen = ap->minlen; |
| 3494 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3495 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3496 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3497 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3498 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3499 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3500 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3501 | args.prod = 1; |
| 3502 | args.mod = 0; |
| 3503 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3504 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3505 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3506 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3507 | } |
| 3508 | /* |
| 3509 | * If we are not low on available data blocks, and the |
| 3510 | * underlying logical volume manager is a stripe, and |
| 3511 | * the file offset is zero then try to allocate data |
| 3512 | * blocks on stripe unit boundary. |
| 3513 | * NOTE: ap->aeof is only set if the allocation length |
| 3514 | * is >= the stripe unit and the allocation offset is |
| 3515 | * at the end of file. |
| 3516 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3517 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3518 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3519 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3520 | atype = args.type; |
| 3521 | isaligned = 1; |
| 3522 | /* |
| 3523 | * Adjust for alignment |
| 3524 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3525 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3526 | args.minlen = blen - args.alignment; |
| 3527 | args.minalignslop = 0; |
| 3528 | } else { |
| 3529 | /* |
| 3530 | * First try an exact bno allocation. |
| 3531 | * If it fails then do a near or start bno |
| 3532 | * allocation with alignment turned on. |
| 3533 | */ |
| 3534 | atype = args.type; |
| 3535 | tryagain = 1; |
| 3536 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3537 | args.alignment = 1; |
| 3538 | /* |
| 3539 | * Compute the minlen+alignment for the |
| 3540 | * next case. Set slop so that the value |
| 3541 | * of minlen+alignment+slop doesn't go up |
| 3542 | * between the calls. |
| 3543 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3544 | if (blen > stripe_align && blen <= args.maxlen) |
| 3545 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3546 | else |
| 3547 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3548 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3549 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3550 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3551 | args.minlen - 1; |
| 3552 | else |
| 3553 | args.minalignslop = 0; |
| 3554 | } |
| 3555 | } else { |
| 3556 | args.alignment = 1; |
| 3557 | args.minalignslop = 0; |
| 3558 | } |
| 3559 | args.minleft = ap->minleft; |
| 3560 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3561 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3562 | args.datatype = ap->datatype; |
| 3563 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3564 | args.ip = ap->ip; |
| 3565 | |
| 3566 | error = xfs_alloc_vextent(&args); |
| 3567 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3568 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3569 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3570 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3571 | /* |
| 3572 | * Exact allocation failed. Now try with alignment |
| 3573 | * turned on. |
| 3574 | */ |
| 3575 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3576 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3577 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3578 | args.minlen = nextminlen; |
| 3579 | args.minalignslop = 0; |
| 3580 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | if ((error = xfs_alloc_vextent(&args))) |
| 3582 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3583 | } |
| 3584 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3585 | /* |
| 3586 | * allocation failed, so turn off alignment and |
| 3587 | * try again. |
| 3588 | */ |
| 3589 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3590 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3591 | args.alignment = 0; |
| 3592 | if ((error = xfs_alloc_vextent(&args))) |
| 3593 | return error; |
| 3594 | } |
| 3595 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3596 | args.minlen > ap->minlen) { |
| 3597 | args.minlen = ap->minlen; |
| 3598 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3599 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3600 | if ((error = xfs_alloc_vextent(&args))) |
| 3601 | return error; |
| 3602 | } |
| 3603 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3604 | args.fsbno = 0; |
| 3605 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3606 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3607 | if ((error = xfs_alloc_vextent(&args))) |
| 3608 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3609 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3610 | } |
| 3611 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3612 | /* |
| 3613 | * check the allocation happened at the same or higher AG than |
| 3614 | * the first block that was allocated. |
| 3615 | */ |
| 3616 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3617 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) <= |
| 3618 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3619 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3620 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3621 | if (*ap->firstblock == NULLFSBLOCK) |
| 3622 | *ap->firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3623 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3624 | ap->length = args.len; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3625 | /* |
| 3626 | * If the extent size hint is active, we tried to round the |
| 3627 | * caller's allocation request offset down to extsz and the |
| 3628 | * length up to another extsz boundary. If we found a free |
| 3629 | * extent we mapped it in starting at this new offset. If the |
| 3630 | * newly mapped space isn't long enough to cover any of the |
| 3631 | * range of offsets that was originally requested, move the |
| 3632 | * mapping up so that we can fill as much of the caller's |
| 3633 | * original request as possible. Free space is apparently |
| 3634 | * very fragmented so we're unlikely to be able to satisfy the |
| 3635 | * hints anyway. |
| 3636 | */ |
| 3637 | if (ap->length <= orig_length) |
| 3638 | ap->offset = orig_offset; |
| 3639 | else if (ap->offset + ap->length < orig_offset + orig_length) |
| 3640 | ap->offset = orig_offset + orig_length - ap->length; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3641 | xfs_bmap_btalloc_accounting(ap, &args); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3642 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3643 | ap->blkno = NULLFSBLOCK; |
| 3644 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | } |
| 3646 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | /* |
| 3650 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3651 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3652 | */ |
| 3653 | STATIC int |
| 3654 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3655 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3656 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3657 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3658 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3659 | return xfs_bmap_rtalloc(ap); |
| 3660 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | } |
| 3662 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3663 | /* Trim extent to fit a logical block range. */ |
| 3664 | void |
| 3665 | xfs_trim_extent( |
| 3666 | struct xfs_bmbt_irec *irec, |
| 3667 | xfs_fileoff_t bno, |
| 3668 | xfs_filblks_t len) |
| 3669 | { |
| 3670 | xfs_fileoff_t distance; |
| 3671 | xfs_fileoff_t end = bno + len; |
| 3672 | |
| 3673 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3674 | irec->br_startoff >= end) { |
| 3675 | irec->br_blockcount = 0; |
| 3676 | return; |
| 3677 | } |
| 3678 | |
| 3679 | if (irec->br_startoff < bno) { |
| 3680 | distance = bno - irec->br_startoff; |
| 3681 | if (isnullstartblock(irec->br_startblock)) |
| 3682 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3683 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3684 | irec->br_startblock != HOLESTARTBLOCK) |
| 3685 | irec->br_startblock += distance; |
| 3686 | irec->br_startoff += distance; |
| 3687 | irec->br_blockcount -= distance; |
| 3688 | } |
| 3689 | |
| 3690 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3691 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3692 | irec->br_blockcount -= distance; |
| 3693 | } |
| 3694 | } |
| 3695 | |
Brian Foster | 40214d1 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 3696 | /* trim extent to within eof */ |
| 3697 | void |
| 3698 | xfs_trim_extent_eof( |
| 3699 | struct xfs_bmbt_irec *irec, |
| 3700 | struct xfs_inode *ip) |
| 3701 | |
| 3702 | { |
| 3703 | xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount, |
| 3704 | i_size_read(VFS_I(ip)))); |
| 3705 | } |
| 3706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3708 | * Trim the returned map to the required bounds |
| 3709 | */ |
| 3710 | STATIC void |
| 3711 | xfs_bmapi_trim_map( |
| 3712 | struct xfs_bmbt_irec *mval, |
| 3713 | struct xfs_bmbt_irec *got, |
| 3714 | xfs_fileoff_t *bno, |
| 3715 | xfs_filblks_t len, |
| 3716 | xfs_fileoff_t obno, |
| 3717 | xfs_fileoff_t end, |
| 3718 | int n, |
| 3719 | int flags) |
| 3720 | { |
| 3721 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3722 | got->br_startoff + got->br_blockcount <= obno) { |
| 3723 | *mval = *got; |
| 3724 | if (isnullstartblock(got->br_startblock)) |
| 3725 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3726 | return; |
| 3727 | } |
| 3728 | |
| 3729 | if (obno > *bno) |
| 3730 | *bno = obno; |
| 3731 | ASSERT((*bno >= obno) || (n == 0)); |
| 3732 | ASSERT(*bno < end); |
| 3733 | mval->br_startoff = *bno; |
| 3734 | if (isnullstartblock(got->br_startblock)) |
| 3735 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3736 | else |
| 3737 | mval->br_startblock = got->br_startblock + |
| 3738 | (*bno - got->br_startoff); |
| 3739 | /* |
| 3740 | * Return the minimum of what we got and what we asked for for |
| 3741 | * the length. We can use the len variable here because it is |
| 3742 | * modified below and we could have been there before coming |
| 3743 | * here if the first part of the allocation didn't overlap what |
| 3744 | * was asked for. |
| 3745 | */ |
| 3746 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3747 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3748 | mval->br_state = got->br_state; |
| 3749 | ASSERT(mval->br_blockcount <= len); |
| 3750 | return; |
| 3751 | } |
| 3752 | |
| 3753 | /* |
| 3754 | * Update and validate the extent map to return |
| 3755 | */ |
| 3756 | STATIC void |
| 3757 | xfs_bmapi_update_map( |
| 3758 | struct xfs_bmbt_irec **map, |
| 3759 | xfs_fileoff_t *bno, |
| 3760 | xfs_filblks_t *len, |
| 3761 | xfs_fileoff_t obno, |
| 3762 | xfs_fileoff_t end, |
| 3763 | int *n, |
| 3764 | int flags) |
| 3765 | { |
| 3766 | xfs_bmbt_irec_t *mval = *map; |
| 3767 | |
| 3768 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3769 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3770 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3771 | (mval->br_startoff < obno)); |
| 3772 | |
| 3773 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3774 | *len = end - *bno; |
| 3775 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3776 | /* update previous map with new information */ |
| 3777 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3778 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3779 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3780 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3781 | mval[-1].br_state = mval->br_state; |
| 3782 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3783 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3784 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3785 | mval->br_startblock == mval[-1].br_startblock + |
| 3786 | mval[-1].br_blockcount && |
| 3787 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3788 | mval[-1].br_state == mval->br_state)) { |
| 3789 | ASSERT(mval->br_startoff == |
| 3790 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3791 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3792 | } else if (*n > 0 && |
| 3793 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3794 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3795 | mval->br_startoff == |
| 3796 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3797 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3798 | mval[-1].br_state = mval->br_state; |
| 3799 | } else if (!((*n == 0) && |
| 3800 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3801 | obno))) { |
| 3802 | mval++; |
| 3803 | (*n)++; |
| 3804 | } |
| 3805 | *map = mval; |
| 3806 | } |
| 3807 | |
| 3808 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3809 | * Map file blocks to filesystem blocks without allocation. |
| 3810 | */ |
| 3811 | int |
| 3812 | xfs_bmapi_read( |
| 3813 | struct xfs_inode *ip, |
| 3814 | xfs_fileoff_t bno, |
| 3815 | xfs_filblks_t len, |
| 3816 | struct xfs_bmbt_irec *mval, |
| 3817 | int *nmap, |
| 3818 | int flags) |
| 3819 | { |
| 3820 | struct xfs_mount *mp = ip->i_mount; |
| 3821 | struct xfs_ifork *ifp; |
| 3822 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3823 | xfs_fileoff_t obno; |
| 3824 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3825 | struct xfs_iext_cursor icur; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3826 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3827 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3828 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3829 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3830 | |
| 3831 | ASSERT(*nmap >= 1); |
| 3832 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3833 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3834 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3835 | |
| 3836 | if (unlikely(XFS_TEST_ERROR( |
| 3837 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3838 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 3839 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3840 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3841 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3845 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3846 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3847 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3848 | |
| 3849 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3850 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3851 | /* No CoW fork? Return a hole. */ |
| 3852 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 3853 | mval->br_startoff = bno; |
| 3854 | mval->br_startblock = HOLESTARTBLOCK; |
| 3855 | mval->br_blockcount = len; |
| 3856 | mval->br_state = XFS_EXT_NORM; |
| 3857 | *nmap = 1; |
| 3858 | return 0; |
| 3859 | } |
| 3860 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3861 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3862 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3863 | if (error) |
| 3864 | return error; |
| 3865 | } |
| 3866 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3867 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3868 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3869 | end = bno + len; |
| 3870 | obno = bno; |
| 3871 | |
| 3872 | while (bno < end && n < *nmap) { |
| 3873 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3874 | if (eof) |
| 3875 | got.br_startoff = end; |
| 3876 | if (got.br_startoff > bno) { |
| 3877 | /* Reading in a hole. */ |
| 3878 | mval->br_startoff = bno; |
| 3879 | mval->br_startblock = HOLESTARTBLOCK; |
| 3880 | mval->br_blockcount = |
| 3881 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3882 | mval->br_state = XFS_EXT_NORM; |
| 3883 | bno += mval->br_blockcount; |
| 3884 | len -= mval->br_blockcount; |
| 3885 | mval++; |
| 3886 | n++; |
| 3887 | continue; |
| 3888 | } |
| 3889 | |
| 3890 | /* set up the extent map to return. */ |
| 3891 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3892 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3893 | |
| 3894 | /* If we're done, stop now. */ |
| 3895 | if (bno >= end || n >= *nmap) |
| 3896 | break; |
| 3897 | |
| 3898 | /* Else go on to the next record. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3899 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3900 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3901 | } |
| 3902 | *nmap = n; |
| 3903 | return 0; |
| 3904 | } |
| 3905 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3906 | /* |
| 3907 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3908 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3909 | * |
| 3910 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3911 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3912 | * to the extent record that was inserted to the inode fork. |
| 3913 | * |
| 3914 | * Note that the allocated extent may have been merged with contiguous extents |
| 3915 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3916 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3917 | * look up the updated record in the inode fork. |
| 3918 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3919 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3920 | xfs_bmapi_reserve_delalloc( |
| 3921 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3922 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3923 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3924 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3925 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3926 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3927 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3928 | int eof) |
| 3929 | { |
| 3930 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3931 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3932 | xfs_extlen_t alen; |
| 3933 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3934 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3935 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3936 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3937 | /* |
| 3938 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3939 | * tag the inode before we return. |
| 3940 | */ |
| 3941 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3942 | if (!eof) |
| 3943 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3944 | if (prealloc && alen >= len) |
| 3945 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3946 | |
| 3947 | /* Figure out the extent size, adjust alen */ |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3948 | if (whichfork == XFS_COW_FORK) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3949 | struct xfs_bmbt_irec prev; |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3950 | xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3951 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3952 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3953 | prev.br_startoff = NULLFILEOFF; |
| 3954 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3955 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3956 | 1, 0, &aoff, &alen); |
| 3957 | ASSERT(!error); |
| 3958 | } |
| 3959 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3960 | /* |
| 3961 | * Make a transaction-less quota reservation for delayed allocation |
| 3962 | * blocks. This number gets adjusted later. We return if we haven't |
| 3963 | * allocated blocks already inside this loop. |
| 3964 | */ |
| 3965 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3966 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3967 | if (error) |
| 3968 | return error; |
| 3969 | |
| 3970 | /* |
| 3971 | * Split changing sb for alen and indlen since they could be coming |
| 3972 | * from different places. |
| 3973 | */ |
| 3974 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 3975 | ASSERT(indlen > 0); |
| 3976 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3977 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3978 | if (error) |
| 3979 | goto out_unreserve_quota; |
| 3980 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3981 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3982 | if (error) |
| 3983 | goto out_unreserve_blocks; |
| 3984 | |
| 3985 | |
| 3986 | ip->i_delayed_blks += alen; |
| 3987 | |
| 3988 | got->br_startoff = aoff; |
| 3989 | got->br_startblock = nullstartblock(indlen); |
| 3990 | got->br_blockcount = alen; |
| 3991 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3992 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3993 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3994 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3995 | /* |
| 3996 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 3997 | * preallocation can occur at the start or end of the extent, even when |
| 3998 | * prealloc == 0, so we must also check the aligned offset and length. |
| 3999 | */ |
| 4000 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4001 | xfs_inode_set_eofblocks_tag(ip); |
| 4002 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4003 | xfs_inode_set_cowblocks_tag(ip); |
| 4004 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4005 | return 0; |
| 4006 | |
| 4007 | out_unreserve_blocks: |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4008 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4009 | out_unreserve_quota: |
| 4010 | if (XFS_IS_QUOTA_ON(mp)) |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4011 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4012 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4013 | return error; |
| 4014 | } |
| 4015 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4016 | static int |
| 4017 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4018 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4019 | { |
| 4020 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4021 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4022 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4023 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4024 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4025 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4026 | ASSERT(bma->length > 0); |
| 4027 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4028 | /* |
| 4029 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4030 | * for in this bmap call but that wouldn't be as good. |
| 4031 | */ |
| 4032 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4033 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4034 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4035 | xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4036 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4037 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4038 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4039 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4040 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4044 | * Set the data type being allocated. For the data fork, the first data |
| 4045 | * in the file is treated differently to all other allocations. For the |
| 4046 | * attribute fork, we only need to ensure the allocated range is not on |
| 4047 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4048 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4049 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4050 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4051 | if (whichfork == XFS_DATA_FORK) { |
| 4052 | if (bma->offset == 0) |
| 4053 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4054 | else |
| 4055 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4056 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4057 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4058 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4061 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4062 | |
| 4063 | /* |
| 4064 | * Only want to do the alignment at the eof if it is userdata and |
| 4065 | * allocation length is larger than a stripe unit. |
| 4066 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4067 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4068 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4069 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4070 | if (error) |
| 4071 | return error; |
| 4072 | } |
| 4073 | |
| 4074 | error = xfs_bmap_alloc(bma); |
| 4075 | if (error) |
| 4076 | return error; |
| 4077 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4078 | if (bma->cur) |
| 4079 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4080 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4081 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4082 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4083 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4084 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4085 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4086 | } |
| 4087 | /* |
| 4088 | * Bump the number of extents we've allocated |
| 4089 | * in this call. |
| 4090 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4091 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4092 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4093 | if (bma->cur) |
| 4094 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4095 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4096 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4097 | bma->got.br_startoff = bma->offset; |
| 4098 | bma->got.br_startblock = bma->blkno; |
| 4099 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4100 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4101 | |
| 4102 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4103 | * In the data fork, a wasdelay extent has been initialized, so |
| 4104 | * shouldn't be flagged as unwritten. |
| 4105 | * |
| 4106 | * For the cow fork, however, we convert delalloc reservations |
| 4107 | * (extents allocated for speculative preallocation) to |
| 4108 | * allocated unwritten extents, and only convert the unwritten |
| 4109 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4110 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4111 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4112 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4113 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4114 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4115 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4116 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4117 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4118 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4119 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4120 | whichfork, &bma->icur, &bma->cur, &bma->got, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4121 | bma->firstblock, bma->dfops, &bma->logflags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4122 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4123 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4124 | if (error) |
| 4125 | return error; |
| 4126 | |
| 4127 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4128 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4129 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4130 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4131 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4132 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4133 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4134 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4135 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4136 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4137 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4138 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4139 | return 0; |
| 4140 | } |
| 4141 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4142 | STATIC int |
| 4143 | xfs_bmapi_convert_unwritten( |
| 4144 | struct xfs_bmalloca *bma, |
| 4145 | struct xfs_bmbt_irec *mval, |
| 4146 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4147 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4148 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4149 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4150 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4151 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4152 | int error; |
| 4153 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4154 | /* check if we need to do unwritten->real conversion */ |
| 4155 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4156 | (flags & XFS_BMAPI_PREALLOC)) |
| 4157 | return 0; |
| 4158 | |
| 4159 | /* check if we need to do real->unwritten conversion */ |
| 4160 | if (mval->br_state == XFS_EXT_NORM && |
| 4161 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4162 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4163 | return 0; |
| 4164 | |
| 4165 | /* |
| 4166 | * Modify (by adding) the state flag, if writing. |
| 4167 | */ |
| 4168 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4169 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4170 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4171 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4172 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4173 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4174 | } |
| 4175 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4176 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4177 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4178 | /* |
| 4179 | * Before insertion into the bmbt, zero the range being converted |
| 4180 | * if required. |
| 4181 | */ |
| 4182 | if (flags & XFS_BMAPI_ZERO) { |
| 4183 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4184 | mval->br_blockcount); |
| 4185 | if (error) |
| 4186 | return error; |
| 4187 | } |
| 4188 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4189 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4190 | &bma->icur, &bma->cur, mval, bma->firstblock, |
| 4191 | bma->dfops, &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4192 | /* |
| 4193 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4194 | * path because the conversion might not have done so (e.g., if the |
| 4195 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4196 | * in the transaction for the sake of fsync(), even if nothing has |
| 4197 | * changed, because fsync() will not force the log for this transaction |
| 4198 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4199 | * |
| 4200 | * Note: If we're only converting cow fork extents, there aren't |
| 4201 | * 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] | 4202 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4203 | if (whichfork != XFS_COW_FORK) |
| 4204 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4205 | if (error) |
| 4206 | return error; |
| 4207 | |
| 4208 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4209 | * Update our extent pointer, given that |
| 4210 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4211 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4212 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4213 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4214 | |
| 4215 | /* |
| 4216 | * We may have combined previously unwritten space with written space, |
| 4217 | * so generate another request. |
| 4218 | */ |
| 4219 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4220 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4221 | return 0; |
| 4222 | } |
| 4223 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4224 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4225 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4226 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4227 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4228 | * locking problems. |
| 4229 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | * The returned value in "firstblock" from the first call in a transaction |
| 4231 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4232 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4233 | * the first call in "total"; if no allocation group has that many free |
| 4234 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4235 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4236 | int |
| 4237 | xfs_bmapi_write( |
| 4238 | struct xfs_trans *tp, /* transaction pointer */ |
| 4239 | struct xfs_inode *ip, /* incore inode */ |
| 4240 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4241 | xfs_filblks_t len, /* length to map in file */ |
| 4242 | int flags, /* XFS_BMAPI_... */ |
| 4243 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4244 | controls a.g. for allocs */ |
| 4245 | xfs_extlen_t total, /* total blocks needed */ |
| 4246 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4247 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4248 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4250 | struct xfs_mount *mp = ip->i_mount; |
| 4251 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4252 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4253 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4254 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4255 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4256 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4257 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4258 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4261 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4262 | int orig_flags; /* original flags arg value */ |
| 4263 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4264 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4265 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | |
| 4267 | orig_bno = bno; |
| 4268 | orig_len = len; |
| 4269 | orig_flags = flags; |
| 4270 | orig_mval = mval; |
| 4271 | orig_nmap = *nmap; |
| 4272 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4273 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4276 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4277 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4278 | ASSERT(tp != NULL || |
| 4279 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4280 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4281 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4282 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4283 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4284 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4285 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4286 | /* zeroing is for currently only for data extents, not metadata */ |
| 4287 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4288 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4289 | /* |
| 4290 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4291 | * but it makes no sense to do both at once. This would result in |
| 4292 | * zeroing the unwritten extent twice, but it still being an |
| 4293 | * unwritten extent.... |
| 4294 | */ |
| 4295 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4296 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | if (unlikely(XFS_TEST_ERROR( |
| 4299 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4300 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4301 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4302 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4303 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4307 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4310 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4311 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4312 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4313 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4315 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4317 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4318 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4319 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4323 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4324 | if (error) |
| 4325 | goto error0; |
| 4326 | } |
| 4327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | n = 0; |
| 4329 | end = bno + len; |
| 4330 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4331 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4332 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4333 | eof = true; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4334 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4335 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4336 | bma.tp = tp; |
| 4337 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4338 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4339 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4340 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4341 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4344 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4345 | |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4346 | /* in hole or beyond EOF? */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4347 | if (eof || bma.got.br_startoff > bno) { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4348 | /* |
| 4349 | * CoW fork conversions should /never/ hit EOF or |
| 4350 | * holes. There should always be something for us |
| 4351 | * to work on. |
| 4352 | */ |
| 4353 | ASSERT(!((flags & XFS_BMAPI_CONVERT) && |
| 4354 | (flags & XFS_BMAPI_COWFORK))); |
| 4355 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4356 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4357 | /* |
| 4358 | * For the COW fork we can reasonably get a |
| 4359 | * request for converting an extent that races |
| 4360 | * with other threads already having converted |
| 4361 | * part of it, as there converting COW to |
| 4362 | * regular blocks is not protected using the |
| 4363 | * IOLOCK. |
| 4364 | */ |
| 4365 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4366 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4367 | error = -EIO; |
| 4368 | goto error0; |
| 4369 | } |
| 4370 | |
| 4371 | if (eof || bno >= end) |
| 4372 | break; |
| 4373 | } else { |
| 4374 | need_alloc = true; |
| 4375 | } |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4376 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4377 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4378 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4379 | |
| 4380 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | * First, deal with the hole before the allocated space |
| 4382 | * that we found, if any. |
| 4383 | */ |
Christoph Hellwig | b121459 | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 4384 | if ((need_alloc || wasdelay) && |
| 4385 | !(flags & XFS_BMAPI_CONVERT_ONLY)) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4386 | bma.eof = eof; |
| 4387 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4388 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4389 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4390 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4391 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4392 | /* |
| 4393 | * There's a 32/64 bit type mismatch between the |
| 4394 | * allocation length request (which can be 64 bits in |
| 4395 | * length) and the bma length request, which is |
| 4396 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4397 | * check for 32-bit overflows and handle them here. |
| 4398 | */ |
| 4399 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4400 | bma.length = MAXEXTLEN; |
| 4401 | else |
| 4402 | bma.length = len; |
| 4403 | |
| 4404 | ASSERT(len > 0); |
| 4405 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4406 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | if (error) |
| 4408 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4409 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4410 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4411 | |
| 4412 | /* |
| 4413 | * If this is a CoW allocation, record the data in |
| 4414 | * the refcount btree for orphan recovery. |
| 4415 | */ |
| 4416 | if (whichfork == XFS_COW_FORK) { |
| 4417 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4418 | bma.blkno, bma.length); |
| 4419 | if (error) |
| 4420 | goto error0; |
| 4421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4423 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4424 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4425 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4426 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4428 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4429 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4430 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4431 | continue; |
| 4432 | if (error) |
| 4433 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4435 | /* update the extent map to return */ |
| 4436 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | /* |
| 4439 | * If we're done, stop now. Stop when we've allocated |
| 4440 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4441 | * the transaction may get too big. |
| 4442 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4443 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4445 | |
| 4446 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4447 | bma.prev = bma.got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4448 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4449 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | /* |
| 4454 | * Transform from btree to extents, give it cur. |
| 4455 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4456 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4457 | int tmp_logflags = 0; |
| 4458 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4459 | ASSERT(bma.cur); |
| 4460 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4462 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | if (error) |
| 4464 | goto error0; |
| 4465 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4468 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4469 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | error0: |
| 4472 | /* |
| 4473 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4474 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4476 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4478 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4479 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4481 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | /* |
| 4483 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4484 | * detecting a case where the data is changed, there's an error, |
| 4485 | * and it's not logged so we don't shutdown when we should. |
| 4486 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4487 | if (bma.logflags) |
| 4488 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4489 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4490 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | if (!error) { |
| 4492 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4493 | XFS_FSB_TO_AGNO(mp, *firstblock) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | XFS_FSB_TO_AGNO(mp, |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4495 | bma.cur->bc_private.b.firstblock)); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4496 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4498 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4500 | } |
| 4501 | if (!error) |
| 4502 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4503 | orig_nmap, *nmap); |
| 4504 | return error; |
| 4505 | } |
| 4506 | |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4507 | static int |
| 4508 | xfs_bmapi_remap( |
| 4509 | struct xfs_trans *tp, |
| 4510 | struct xfs_inode *ip, |
| 4511 | xfs_fileoff_t bno, |
| 4512 | xfs_filblks_t len, |
| 4513 | xfs_fsblock_t startblock, |
| 4514 | struct xfs_defer_ops *dfops) |
| 4515 | { |
| 4516 | struct xfs_mount *mp = ip->i_mount; |
| 4517 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4518 | struct xfs_btree_cur *cur = NULL; |
| 4519 | xfs_fsblock_t firstblock = NULLFSBLOCK; |
| 4520 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4521 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4522 | int logflags = 0, error; |
| 4523 | |
| 4524 | ASSERT(len > 0); |
| 4525 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4526 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 4527 | |
| 4528 | if (unlikely(XFS_TEST_ERROR( |
| 4529 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4530 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4531 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4532 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4533 | return -EFSCORRUPTED; |
| 4534 | } |
| 4535 | |
| 4536 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4537 | return -EIO; |
| 4538 | |
| 4539 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4540 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4541 | if (error) |
| 4542 | return error; |
| 4543 | } |
| 4544 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4545 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4546 | /* make sure we only reflink into a hole. */ |
| 4547 | ASSERT(got.br_startoff > bno); |
| 4548 | ASSERT(got.br_startoff - bno >= len); |
| 4549 | } |
| 4550 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4551 | ip->i_d.di_nblocks += len; |
| 4552 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4553 | |
| 4554 | if (ifp->if_flags & XFS_IFBROOT) { |
| 4555 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
| 4556 | cur->bc_private.b.firstblock = firstblock; |
| 4557 | cur->bc_private.b.dfops = dfops; |
| 4558 | cur->bc_private.b.flags = 0; |
| 4559 | } |
| 4560 | |
| 4561 | got.br_startoff = bno; |
| 4562 | got.br_startblock = startblock; |
| 4563 | got.br_blockcount = len; |
| 4564 | got.br_state = XFS_EXT_NORM; |
| 4565 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4566 | error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &icur, |
| 4567 | &cur, &got, &firstblock, dfops, &logflags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4568 | if (error) |
| 4569 | goto error0; |
| 4570 | |
| 4571 | if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) { |
| 4572 | int tmp_logflags = 0; |
| 4573 | |
| 4574 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4575 | &tmp_logflags, XFS_DATA_FORK); |
| 4576 | logflags |= tmp_logflags; |
| 4577 | } |
| 4578 | |
| 4579 | error0: |
| 4580 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4581 | logflags &= ~XFS_ILOG_DEXT; |
| 4582 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4583 | logflags &= ~XFS_ILOG_DBROOT; |
| 4584 | |
| 4585 | if (logflags) |
| 4586 | xfs_trans_log_inode(tp, ip, logflags); |
| 4587 | if (cur) { |
| 4588 | xfs_btree_del_cursor(cur, |
| 4589 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4590 | } |
| 4591 | return error; |
| 4592 | } |
| 4593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4595 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4596 | * indlen reservation must be shared across the two new extents that are left |
| 4597 | * behind. |
| 4598 | * |
| 4599 | * Given the original reservation and the worst case indlen for the two new |
| 4600 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4601 | * reservation fairly across the two new extents. If necessary, steal available |
| 4602 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4603 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4604 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4605 | */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4606 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4607 | xfs_bmap_split_indlen( |
| 4608 | xfs_filblks_t ores, /* original res. */ |
| 4609 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4610 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4611 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4612 | { |
| 4613 | xfs_filblks_t len1 = *indlen1; |
| 4614 | xfs_filblks_t len2 = *indlen2; |
| 4615 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4616 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4617 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4618 | |
| 4619 | /* |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4620 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4621 | * indlen for both new extents. |
| 4622 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4623 | if (ores < nres && avail) |
| 4624 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4625 | ores += stolen; |
| 4626 | |
| 4627 | /* nothing else to do if we've satisfied the new reservation */ |
| 4628 | if (ores >= nres) |
| 4629 | return stolen; |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4630 | |
| 4631 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4632 | * We can't meet the total required reservation for the two extents. |
| 4633 | * Calculate the percent of the overall shortage between both extents |
| 4634 | * and apply this percentage to each of the requested indlen values. |
| 4635 | * This distributes the shortage fairly and reduces the chances that one |
| 4636 | * of the two extents is left with nothing when extents are repeatedly |
| 4637 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4638 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4639 | resfactor = (ores * 100); |
| 4640 | do_div(resfactor, nres); |
| 4641 | len1 *= resfactor; |
| 4642 | do_div(len1, 100); |
| 4643 | len2 *= resfactor; |
| 4644 | do_div(len2, 100); |
| 4645 | ASSERT(len1 + len2 <= ores); |
| 4646 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4647 | |
| 4648 | /* |
| 4649 | * Hand out the remainder to each extent. If one of the two reservations |
| 4650 | * is zero, we want to make sure that one gets a block first. The loop |
| 4651 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4652 | * is zero. |
| 4653 | */ |
| 4654 | ores -= (len1 + len2); |
| 4655 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4656 | if (ores && !len2 && *indlen2) { |
| 4657 | len2++; |
| 4658 | ores--; |
| 4659 | } |
| 4660 | while (ores) { |
| 4661 | if (len1 < *indlen1) { |
| 4662 | len1++; |
| 4663 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4664 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4665 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4666 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4667 | if (len2 < *indlen2) { |
| 4668 | len2++; |
| 4669 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4670 | } |
| 4671 | } |
| 4672 | |
| 4673 | *indlen1 = len1; |
| 4674 | *indlen2 = len2; |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4675 | |
| 4676 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4677 | } |
| 4678 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4679 | int |
| 4680 | xfs_bmap_del_extent_delay( |
| 4681 | struct xfs_inode *ip, |
| 4682 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4683 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4684 | struct xfs_bmbt_irec *got, |
| 4685 | struct xfs_bmbt_irec *del) |
| 4686 | { |
| 4687 | struct xfs_mount *mp = ip->i_mount; |
| 4688 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4689 | struct xfs_bmbt_irec new; |
| 4690 | int64_t da_old, da_new, da_diff = 0; |
| 4691 | xfs_fileoff_t del_endoff, got_endoff; |
| 4692 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4693 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4694 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4695 | bool isrt; |
| 4696 | |
| 4697 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4698 | |
| 4699 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4700 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4701 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4702 | da_old = startblockval(got->br_startblock); |
| 4703 | da_new = 0; |
| 4704 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4705 | ASSERT(del->br_blockcount > 0); |
| 4706 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4707 | ASSERT(got_endoff >= del_endoff); |
| 4708 | |
| 4709 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4710 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4711 | |
| 4712 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4713 | xfs_mod_frextents(mp, rtexts); |
| 4714 | } |
| 4715 | |
| 4716 | /* |
| 4717 | * Update the inode delalloc counter now and wait to update the |
| 4718 | * sb counters as we might have to borrow some blocks for the |
| 4719 | * indirect block accounting. |
| 4720 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4721 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4722 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4723 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4724 | if (error) |
| 4725 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4726 | ip->i_delayed_blks -= del->br_blockcount; |
| 4727 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4728 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4729 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4730 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4731 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4732 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4733 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4734 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4735 | /* |
| 4736 | * Matches the whole extent. Delete the entry. |
| 4737 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4738 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4739 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4740 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4741 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4742 | /* |
| 4743 | * Deleting the first part of the extent. |
| 4744 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4745 | got->br_startoff = del_endoff; |
| 4746 | got->br_blockcount -= del->br_blockcount; |
| 4747 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4748 | got->br_blockcount), da_old); |
| 4749 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4750 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4751 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4752 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4753 | /* |
| 4754 | * Deleting the last part of the extent. |
| 4755 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4756 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4757 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4758 | got->br_blockcount), da_old); |
| 4759 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4760 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4761 | break; |
| 4762 | case 0: |
| 4763 | /* |
| 4764 | * Deleting the middle of the extent. |
| 4765 | * |
| 4766 | * Distribute the original indlen reservation across the two new |
| 4767 | * extents. Steal blocks from the deleted extent if necessary. |
| 4768 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4769 | * Warn if either of the new indlen reservations is zero as this |
| 4770 | * can lead to delalloc problems. |
| 4771 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4772 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4773 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4774 | |
| 4775 | new.br_blockcount = got_endoff - del_endoff; |
| 4776 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4777 | |
| 4778 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4779 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4780 | del->br_blockcount); |
| 4781 | |
| 4782 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4783 | |
| 4784 | new.br_startoff = del_endoff; |
| 4785 | new.br_state = got->br_state; |
| 4786 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4787 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4788 | xfs_iext_update_extent(ip, state, icur, got); |
| 4789 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4790 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4791 | |
| 4792 | da_new = got_indlen + new_indlen - stolen; |
| 4793 | del->br_blockcount -= stolen; |
| 4794 | break; |
| 4795 | } |
| 4796 | |
| 4797 | ASSERT(da_old >= da_new); |
| 4798 | da_diff = da_old - da_new; |
| 4799 | if (!isrt) |
| 4800 | da_diff += del->br_blockcount; |
| 4801 | if (da_diff) |
| 4802 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4803 | return error; |
| 4804 | } |
| 4805 | |
| 4806 | void |
| 4807 | xfs_bmap_del_extent_cow( |
| 4808 | struct xfs_inode *ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4809 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4810 | struct xfs_bmbt_irec *got, |
| 4811 | struct xfs_bmbt_irec *del) |
| 4812 | { |
| 4813 | struct xfs_mount *mp = ip->i_mount; |
| 4814 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4815 | struct xfs_bmbt_irec new; |
| 4816 | xfs_fileoff_t del_endoff, got_endoff; |
| 4817 | int state = BMAP_COWFORK; |
| 4818 | |
| 4819 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4820 | |
| 4821 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4822 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4823 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4824 | ASSERT(del->br_blockcount > 0); |
| 4825 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4826 | ASSERT(got_endoff >= del_endoff); |
| 4827 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4828 | |
| 4829 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4830 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4831 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4832 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4833 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4834 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4835 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4836 | /* |
| 4837 | * Matches the whole extent. Delete the entry. |
| 4838 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4839 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4840 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4841 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4842 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4843 | /* |
| 4844 | * Deleting the first part of the extent. |
| 4845 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4846 | got->br_startoff = del_endoff; |
| 4847 | got->br_blockcount -= del->br_blockcount; |
| 4848 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4849 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4850 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4851 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4852 | /* |
| 4853 | * Deleting the last part of the extent. |
| 4854 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4855 | got->br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4856 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4857 | break; |
| 4858 | case 0: |
| 4859 | /* |
| 4860 | * Deleting the middle of the extent. |
| 4861 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4862 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4863 | |
| 4864 | new.br_startoff = del_endoff; |
| 4865 | new.br_blockcount = got_endoff - del_endoff; |
| 4866 | new.br_state = got->br_state; |
| 4867 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4868 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4869 | xfs_iext_update_extent(ip, state, icur, got); |
| 4870 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4871 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4872 | break; |
| 4873 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 4874 | ip->i_delayed_blks -= del->br_blockcount; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4875 | } |
| 4876 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4877 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4878 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4879 | * after removing space. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4880 | */ |
| 4881 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4882 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4883 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4884 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4885 | struct xfs_iext_cursor *icur, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4886 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4887 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4888 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4889 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4890 | int whichfork, /* data or attr fork */ |
| 4891 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4892 | { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4893 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4894 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4895 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4896 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4897 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4898 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4899 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4900 | int i; /* temp state */ |
| 4901 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4902 | xfs_mount_t *mp; /* mount structure */ |
| 4903 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4904 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4905 | /* REFERENCED */ |
| 4906 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4907 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4908 | struct xfs_bmbt_irec old; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4909 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4910 | mp = ip->i_mount; |
| 4911 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4912 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4913 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4914 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4915 | xfs_iext_get_extent(ifp, icur, &got); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4916 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4917 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4918 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4919 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4920 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4921 | qfield = 0; |
| 4922 | error = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4923 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4924 | /* |
| 4925 | * If it's the case where the directory code is running with no block |
| 4926 | * reservation, and the deleted block is in the middle of its extent, |
| 4927 | * and the resulting insert of an extent would cause transformation to |
| 4928 | * btree format, then reject it. The calling code will then swap blocks |
| 4929 | * around instead. We have to do this now, rather than waiting for the |
| 4930 | * conversion to btree format, since the transaction will be dirty then. |
| 4931 | */ |
| 4932 | if (tp->t_blk_res == 0 && |
| 4933 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 4934 | XFS_IFORK_NEXTENTS(ip, whichfork) >= |
| 4935 | XFS_IFORK_MAXEXT(ip, whichfork) && |
| 4936 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 4937 | return -ENOSPC; |
| 4938 | |
| 4939 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4940 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4941 | xfs_fsblock_t bno; |
| 4942 | xfs_filblks_t len; |
| 4943 | |
| 4944 | ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0); |
| 4945 | ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0); |
| 4946 | bno = del->br_startblock; |
| 4947 | len = del->br_blockcount; |
| 4948 | do_div(bno, mp->m_sb.sb_rextsize); |
| 4949 | do_div(len, mp->m_sb.sb_rextsize); |
| 4950 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4951 | if (error) |
| 4952 | goto done; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4953 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4954 | nblks = len * mp->m_sb.sb_rextsize; |
| 4955 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4956 | } else { |
| 4957 | do_fx = 1; |
| 4958 | nblks = del->br_blockcount; |
| 4959 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4960 | } |
| 4961 | |
| 4962 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4963 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4964 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4965 | if (error) |
| 4966 | goto done; |
| 4967 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4968 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 4969 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4970 | if (got.br_startoff == del->br_startoff) |
| 4971 | state |= BMAP_LEFT_FILLING; |
| 4972 | if (got_endoff == del_endoff) |
| 4973 | state |= BMAP_RIGHT_FILLING; |
| 4974 | |
| 4975 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4976 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4977 | /* |
| 4978 | * Matches the whole extent. Delete the entry. |
| 4979 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4980 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4981 | xfs_iext_prev(ifp, icur); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4982 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4983 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 4984 | flags |= XFS_ILOG_CORE; |
| 4985 | if (!cur) { |
| 4986 | flags |= xfs_ilog_fext(whichfork); |
| 4987 | break; |
| 4988 | } |
| 4989 | if ((error = xfs_btree_delete(cur, &i))) |
| 4990 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4991 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4992 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4993 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4994 | /* |
| 4995 | * Deleting the first part of the extent. |
| 4996 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4997 | got.br_startoff = del_endoff; |
| 4998 | got.br_startblock = del_endblock; |
| 4999 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5000 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5001 | if (!cur) { |
| 5002 | flags |= xfs_ilog_fext(whichfork); |
| 5003 | break; |
| 5004 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5005 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5006 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5007 | goto done; |
| 5008 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5009 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5010 | /* |
| 5011 | * Deleting the last part of the extent. |
| 5012 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5013 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5014 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5015 | if (!cur) { |
| 5016 | flags |= xfs_ilog_fext(whichfork); |
| 5017 | break; |
| 5018 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5019 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5020 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5021 | goto done; |
| 5022 | break; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5023 | case 0: |
| 5024 | /* |
| 5025 | * Deleting the middle of the extent. |
| 5026 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5027 | old = got; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5028 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5029 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5030 | xfs_iext_update_extent(ip, state, icur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5031 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5032 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5033 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5034 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5035 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5036 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5037 | flags |= XFS_ILOG_CORE; |
| 5038 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5039 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5040 | if (error) |
| 5041 | goto done; |
| 5042 | error = xfs_btree_increment(cur, 0, &i); |
| 5043 | if (error) |
| 5044 | goto done; |
| 5045 | cur->bc_rec.b = new; |
| 5046 | error = xfs_btree_insert(cur, &i); |
| 5047 | if (error && error != -ENOSPC) |
| 5048 | goto done; |
| 5049 | /* |
| 5050 | * If get no-space back from btree insert, it tried a |
| 5051 | * split, and we have a zero block reservation. Fix up |
| 5052 | * our state and return the error. |
| 5053 | */ |
| 5054 | if (error == -ENOSPC) { |
| 5055 | /* |
| 5056 | * Reset the cursor, don't trust it after any |
| 5057 | * insert operation. |
| 5058 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5059 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5060 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5061 | goto done; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5062 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5063 | /* |
| 5064 | * Update the btree record back |
| 5065 | * to the original value. |
| 5066 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5067 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5068 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5069 | goto done; |
| 5070 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5071 | * Reset the extent record back |
| 5072 | * to the original value. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5073 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5074 | xfs_iext_update_extent(ip, state, icur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5075 | flags = 0; |
| 5076 | error = -ENOSPC; |
| 5077 | goto done; |
| 5078 | } |
| 5079 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5080 | } else |
| 5081 | flags |= xfs_ilog_fext(whichfork); |
| 5082 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5083 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5084 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5085 | xfs_iext_insert(ip, icur, &new, state); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5086 | break; |
| 5087 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5088 | |
| 5089 | /* remove reverse mapping */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5090 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5091 | if (error) |
| 5092 | goto done; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5093 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5094 | /* |
| 5095 | * If we need to, add to list of extents to delete. |
| 5096 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5097 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5098 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5099 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5100 | if (error) |
| 5101 | goto done; |
| 5102 | } else |
| 5103 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5104 | del->br_blockcount, NULL); |
| 5105 | } |
| 5106 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5107 | /* |
| 5108 | * Adjust inode # blocks in the file. |
| 5109 | */ |
| 5110 | if (nblks) |
| 5111 | ip->i_d.di_nblocks -= nblks; |
| 5112 | /* |
| 5113 | * Adjust quota data. |
| 5114 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5115 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5116 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5117 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5118 | done: |
| 5119 | *logflagsp = flags; |
| 5120 | return error; |
| 5121 | } |
| 5122 | |
| 5123 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | * Unmap (remove) blocks from a file. |
| 5125 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5126 | * that value. If not all extents in the block range can be removed then |
| 5127 | * *done is set. |
| 5128 | */ |
| 5129 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5130 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | xfs_trans_t *tp, /* transaction pointer */ |
| 5132 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5133 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5134 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | int flags, /* misc flags */ |
| 5136 | xfs_extnum_t nexts, /* number of extents max */ |
| 5137 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5138 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5139 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | { |
| 5141 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5142 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | int error; /* error return value */ |
| 5144 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5145 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5147 | int isrt; /* freeing in rt area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | int logflags; /* transaction logging flags */ |
| 5149 | xfs_extlen_t mod; /* rt extent offset */ |
| 5150 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | int tmp_logflags; /* partial logging flags */ |
| 5152 | int wasdel; /* was a delayed alloc extent */ |
| 5153 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5155 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5156 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5157 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5158 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5159 | struct xfs_iext_cursor icur; |
| 5160 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5162 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5163 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5164 | whichfork = xfs_bmapi_whichfork(flags); |
| 5165 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5167 | if (unlikely( |
| 5168 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5169 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5170 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5171 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5172 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | } |
| 5174 | mp = ip->i_mount; |
| 5175 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5176 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5177 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5178 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | ASSERT(len > 0); |
| 5180 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5181 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5182 | /* |
| 5183 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5184 | * blowing out the transaction with a mix of EFIs and reflink |
| 5185 | * adjustments. |
| 5186 | */ |
Darrick J. Wong | 8c57b88 | 2017-12-10 18:03:53 -0800 | [diff] [blame] | 5187 | if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5188 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5189 | else |
| 5190 | max_len = len; |
| 5191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5193 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5194 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5195 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5196 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | return 0; |
| 5198 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5199 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5200 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5201 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5202 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5203 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5204 | *rlen = 0; |
| 5205 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5206 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5207 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | logflags = 0; |
| 5210 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5211 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5212 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5214 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | cur->bc_private.b.flags = 0; |
| 5216 | } else |
| 5217 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5218 | |
| 5219 | if (isrt) { |
| 5220 | /* |
| 5221 | * Synchronize by locking the bitmap inode. |
| 5222 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5223 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5224 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5225 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5226 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5227 | } |
| 5228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | extno = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5230 | while (end != (xfs_fileoff_t)-1 && end >= start && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5231 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5233 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | * Just back up to the previous extent, if so. |
| 5235 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5236 | if (got.br_startoff > end && |
| 5237 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5238 | done = true; |
| 5239 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | } |
| 5241 | /* |
| 5242 | * Is the last block of this extent before the range |
| 5243 | * we're supposed to delete? If so, we're done. |
| 5244 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5245 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5247 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | break; |
| 5249 | /* |
| 5250 | * Then deal with the (possibly delayed) allocated space |
| 5251 | * we found. |
| 5252 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5254 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5255 | |
| 5256 | /* |
| 5257 | * Make sure we don't touch multiple AGF headers out of order |
| 5258 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5259 | */ |
| 5260 | if (!wasdel) { |
| 5261 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5262 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5263 | break; |
| 5264 | prev_agno = agno; |
| 5265 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | if (got.br_startoff < start) { |
| 5267 | del.br_startoff = start; |
| 5268 | del.br_blockcount -= start - got.br_startoff; |
| 5269 | if (!wasdel) |
| 5270 | del.br_startblock += start - got.br_startoff; |
| 5271 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5272 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5273 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5274 | |
| 5275 | /* How much can we safely unmap? */ |
| 5276 | if (max_len < del.br_blockcount) { |
| 5277 | del.br_startoff += del.br_blockcount - max_len; |
| 5278 | if (!wasdel) |
| 5279 | del.br_startblock += del.br_blockcount - max_len; |
| 5280 | del.br_blockcount = max_len; |
| 5281 | } |
| 5282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | sum = del.br_startblock + del.br_blockcount; |
| 5284 | if (isrt && |
| 5285 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5286 | /* |
| 5287 | * Realtime extent not lined up at the end. |
| 5288 | * The extent could have been split into written |
| 5289 | * and unwritten pieces, or we could just be |
| 5290 | * unmapping part of it. But we can't really |
| 5291 | * get rid of part of a realtime extent. |
| 5292 | */ |
| 5293 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5294 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | /* |
| 5296 | * This piece is unwritten, or we're not |
| 5297 | * using unwritten extents. Skip over it. |
| 5298 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5299 | ASSERT(end >= mod); |
| 5300 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | del.br_blockcount : mod; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5302 | if (end < got.br_startoff && |
| 5303 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5304 | done = true; |
| 5305 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | } |
| 5307 | continue; |
| 5308 | } |
| 5309 | /* |
| 5310 | * It's written, turn it unwritten. |
| 5311 | * This is better than zeroing it. |
| 5312 | */ |
| 5313 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5314 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | /* |
| 5316 | * If this spans a realtime extent boundary, |
| 5317 | * chop it back to the start of the one we end at. |
| 5318 | */ |
| 5319 | if (del.br_blockcount > mod) { |
| 5320 | del.br_startoff += del.br_blockcount - mod; |
| 5321 | del.br_startblock += del.br_blockcount - mod; |
| 5322 | del.br_blockcount = mod; |
| 5323 | } |
| 5324 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5325 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5326 | whichfork, &icur, &cur, &del, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5327 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | if (error) |
| 5329 | goto error0; |
| 5330 | goto nodelete; |
| 5331 | } |
| 5332 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5333 | /* |
| 5334 | * Realtime extent is lined up at the end but not |
| 5335 | * at the front. We'll get rid of full extents if |
| 5336 | * we can. |
| 5337 | */ |
| 5338 | mod = mp->m_sb.sb_rextsize - mod; |
| 5339 | if (del.br_blockcount > mod) { |
| 5340 | del.br_blockcount -= mod; |
| 5341 | del.br_startoff += mod; |
| 5342 | del.br_startblock += mod; |
| 5343 | } else if ((del.br_startoff == start && |
| 5344 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5345 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5346 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | /* |
| 5348 | * Can't make it unwritten. There isn't |
| 5349 | * a full extent here so just skip it. |
| 5350 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5351 | ASSERT(end >= del.br_blockcount); |
| 5352 | end -= del.br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5353 | if (got.br_startoff > end && |
| 5354 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5355 | done = true; |
| 5356 | break; |
| 5357 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | continue; |
| 5359 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5360 | struct xfs_bmbt_irec prev; |
| 5361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | /* |
| 5363 | * This one is already unwritten. |
| 5364 | * It must have a written left neighbor. |
| 5365 | * Unwrite the killed part of that one and |
| 5366 | * try again. |
| 5367 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5368 | if (!xfs_iext_prev_extent(ifp, &icur, &prev)) |
| 5369 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5371 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | ASSERT(del.br_startblock == |
| 5373 | prev.br_startblock + prev.br_blockcount); |
| 5374 | if (prev.br_startoff < start) { |
| 5375 | mod = start - prev.br_startoff; |
| 5376 | prev.br_blockcount -= mod; |
| 5377 | prev.br_startblock += mod; |
| 5378 | prev.br_startoff = start; |
| 5379 | } |
| 5380 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5381 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5382 | ip, whichfork, &icur, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5383 | &prev, firstblock, dfops, |
| 5384 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | if (error) |
| 5386 | goto error0; |
| 5387 | goto nodelete; |
| 5388 | } else { |
| 5389 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5390 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5391 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5392 | ip, whichfork, &icur, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5393 | &del, firstblock, dfops, |
| 5394 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | if (error) |
| 5396 | goto error0; |
| 5397 | goto nodelete; |
| 5398 | } |
| 5399 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5400 | |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5401 | if (wasdel) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5402 | error = xfs_bmap_del_extent_delay(ip, whichfork, &icur, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5403 | &got, &del); |
| 5404 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5405 | error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5406 | cur, &del, &tmp_logflags, whichfork, |
| 5407 | flags); |
| 5408 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5409 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | if (error) |
| 5412 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5413 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5414 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5415 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | /* |
| 5418 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5420 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5421 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
| 5422 | (got.br_startoff > end && |
| 5423 | !xfs_iext_prev_extent(ifp, &icur, &got))) { |
| 5424 | done = true; |
| 5425 | break; |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | extno++; |
| 5428 | } |
| 5429 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5430 | if (done || end == (xfs_fileoff_t)-1 || end < start) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5431 | *rlen = 0; |
| 5432 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5433 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5435 | /* |
| 5436 | * Convert to a btree if necessary. |
| 5437 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5438 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5440 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | &cur, 0, &tmp_logflags, whichfork); |
| 5442 | logflags |= tmp_logflags; |
| 5443 | if (error) |
| 5444 | goto error0; |
| 5445 | } |
| 5446 | /* |
| 5447 | * transform from btree to extents, give it cur |
| 5448 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5449 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | ASSERT(cur != NULL); |
| 5451 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5452 | whichfork); |
| 5453 | logflags |= tmp_logflags; |
| 5454 | if (error) |
| 5455 | goto error0; |
| 5456 | } |
| 5457 | /* |
| 5458 | * transform from extents to local? |
| 5459 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | error = 0; |
| 5461 | error0: |
| 5462 | /* |
| 5463 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5464 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5466 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5468 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5469 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5471 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | /* |
| 5473 | * Log inode even in the error case, if the transaction |
| 5474 | * is dirty we'll need to shut down the filesystem. |
| 5475 | */ |
| 5476 | if (logflags) |
| 5477 | xfs_trans_log_inode(tp, ip, logflags); |
| 5478 | if (cur) { |
| 5479 | if (!error) { |
| 5480 | *firstblock = cur->bc_private.b.firstblock; |
| 5481 | cur->bc_private.b.allocated = 0; |
| 5482 | } |
| 5483 | xfs_btree_del_cursor(cur, |
| 5484 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5485 | } |
| 5486 | return error; |
| 5487 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5488 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5489 | /* Unmap a range of a file. */ |
| 5490 | int |
| 5491 | xfs_bunmapi( |
| 5492 | xfs_trans_t *tp, |
| 5493 | struct xfs_inode *ip, |
| 5494 | xfs_fileoff_t bno, |
| 5495 | xfs_filblks_t len, |
| 5496 | int flags, |
| 5497 | xfs_extnum_t nexts, |
| 5498 | xfs_fsblock_t *firstblock, |
| 5499 | struct xfs_defer_ops *dfops, |
| 5500 | int *done) |
| 5501 | { |
| 5502 | int error; |
| 5503 | |
| 5504 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5505 | dfops); |
| 5506 | *done = (len == 0); |
| 5507 | return error; |
| 5508 | } |
| 5509 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5510 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5511 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5512 | * extent that precedes the target hole of the shift. |
| 5513 | */ |
| 5514 | STATIC bool |
| 5515 | xfs_bmse_can_merge( |
| 5516 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5517 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5518 | xfs_fileoff_t shift) /* shift fsb */ |
| 5519 | { |
| 5520 | xfs_fileoff_t startoff; |
| 5521 | |
| 5522 | startoff = got->br_startoff - shift; |
| 5523 | |
| 5524 | /* |
| 5525 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5526 | * the preceding extent. |
| 5527 | */ |
| 5528 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5529 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5530 | (left->br_state != got->br_state) || |
| 5531 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5532 | return false; |
| 5533 | |
| 5534 | return true; |
| 5535 | } |
| 5536 | |
| 5537 | /* |
| 5538 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5539 | * hole in the file. If an extent shift would result in the extent being fully |
| 5540 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5541 | * the preceding extent rather than do the shift. |
| 5542 | * |
| 5543 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5544 | * with the provided extents via xfs_bmse_can_merge(). |
| 5545 | */ |
| 5546 | STATIC int |
| 5547 | xfs_bmse_merge( |
| 5548 | struct xfs_inode *ip, |
| 5549 | int whichfork, |
| 5550 | xfs_fileoff_t shift, /* shift fsb */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5551 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5552 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5553 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5554 | struct xfs_btree_cur *cur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5555 | int *logflags, /* output */ |
| 5556 | struct xfs_defer_ops *dfops) |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5557 | { |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5558 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5559 | xfs_filblks_t blockcount; |
| 5560 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5561 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5562 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5563 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5564 | |
| 5565 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5566 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5567 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5568 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5569 | new = *left; |
| 5570 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5571 | |
| 5572 | /* |
| 5573 | * Update the on-disk extent count, the btree if necessary and log the |
| 5574 | * inode. |
| 5575 | */ |
| 5576 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5577 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5578 | *logflags |= XFS_ILOG_CORE; |
| 5579 | if (!cur) { |
| 5580 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5581 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5585 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5586 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5587 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5588 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5589 | |
| 5590 | error = xfs_btree_delete(cur, &i); |
| 5591 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5592 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5593 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5594 | |
| 5595 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5596 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5597 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5598 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5599 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5600 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5601 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5602 | if (error) |
| 5603 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5604 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5605 | done: |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5606 | xfs_iext_remove(ip, icur, 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5607 | xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur); |
| 5608 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5609 | &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5610 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5611 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5612 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got); |
| 5613 | if (error) |
| 5614 | return error; |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5615 | memcpy(&new, got, sizeof(new)); |
| 5616 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5617 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5618 | } |
| 5619 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5620 | static int |
| 5621 | xfs_bmap_shift_update_extent( |
| 5622 | struct xfs_inode *ip, |
| 5623 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5624 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5625 | struct xfs_bmbt_irec *got, |
| 5626 | struct xfs_btree_cur *cur, |
| 5627 | int *logflags, |
| 5628 | struct xfs_defer_ops *dfops, |
| 5629 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5630 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5631 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5632 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5633 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5634 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5635 | *logflags |= XFS_ILOG_CORE; |
| 5636 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5637 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5638 | |
| 5639 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5640 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5641 | if (error) |
| 5642 | return error; |
| 5643 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
| 5644 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5645 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5646 | if (error) |
| 5647 | return error; |
| 5648 | } else { |
| 5649 | *logflags |= XFS_ILOG_DEXT; |
| 5650 | } |
| 5651 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5652 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5653 | got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5654 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5655 | /* update reverse mapping */ |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5656 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5657 | if (error) |
| 5658 | return error; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5659 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5660 | } |
| 5661 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5662 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5663 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5664 | struct xfs_trans *tp, |
| 5665 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5666 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5667 | xfs_fileoff_t offset_shift_fsb, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5668 | bool *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5669 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5670 | xfs_fsblock_t *firstblock, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5671 | struct xfs_defer_ops *dfops) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5672 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5673 | int whichfork = XFS_DATA_FORK; |
| 5674 | struct xfs_mount *mp = ip->i_mount; |
| 5675 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5676 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5677 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5678 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5679 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5680 | int error = 0; |
| 5681 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5682 | |
| 5683 | if (unlikely(XFS_TEST_ERROR( |
| 5684 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5685 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5686 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5687 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5688 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5689 | } |
| 5690 | |
| 5691 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5692 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5693 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5694 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5695 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5696 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5697 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5698 | if (error) |
| 5699 | return error; |
| 5700 | } |
| 5701 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5702 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5703 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5704 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5705 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5706 | cur->bc_private.b.flags = 0; |
| 5707 | } |
| 5708 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5709 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5710 | *done = true; |
| 5711 | goto del_cursor; |
| 5712 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5713 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5714 | del_cursor); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5715 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5716 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5717 | if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5718 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5719 | error = -EINVAL; |
| 5720 | goto del_cursor; |
| 5721 | } |
| 5722 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5723 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
| 5724 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5725 | &icur, &got, &prev, cur, &logflags, |
| 5726 | dfops); |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5727 | if (error) |
| 5728 | goto del_cursor; |
| 5729 | goto done; |
| 5730 | } |
| 5731 | } else { |
| 5732 | if (got.br_startoff < offset_shift_fsb) { |
| 5733 | error = -EINVAL; |
| 5734 | goto del_cursor; |
| 5735 | } |
| 5736 | } |
| 5737 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5738 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
| 5739 | &logflags, dfops, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5740 | if (error) |
| 5741 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5742 | |
Christoph Hellwig | 4263036 | 2017-11-03 10:34:41 -0700 | [diff] [blame] | 5743 | done: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5744 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
| 5745 | *done = true; |
| 5746 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5747 | } |
| 5748 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5749 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5750 | del_cursor: |
| 5751 | if (cur) |
| 5752 | xfs_btree_del_cursor(cur, |
| 5753 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5754 | if (logflags) |
| 5755 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5756 | return error; |
| 5757 | } |
| 5758 | |
| 5759 | int |
| 5760 | xfs_bmap_insert_extents( |
| 5761 | struct xfs_trans *tp, |
| 5762 | struct xfs_inode *ip, |
| 5763 | xfs_fileoff_t *next_fsb, |
| 5764 | xfs_fileoff_t offset_shift_fsb, |
| 5765 | bool *done, |
| 5766 | xfs_fileoff_t stop_fsb, |
| 5767 | xfs_fsblock_t *firstblock, |
| 5768 | struct xfs_defer_ops *dfops) |
| 5769 | { |
| 5770 | int whichfork = XFS_DATA_FORK; |
| 5771 | struct xfs_mount *mp = ip->i_mount; |
| 5772 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5773 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5774 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5775 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5776 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5777 | int error = 0; |
| 5778 | int logflags = 0; |
| 5779 | |
| 5780 | if (unlikely(XFS_TEST_ERROR( |
| 5781 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5782 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5783 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
| 5784 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 5785 | return -EFSCORRUPTED; |
| 5786 | } |
| 5787 | |
| 5788 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5789 | return -EIO; |
| 5790 | |
| 5791 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5792 | |
| 5793 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5794 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5795 | if (error) |
| 5796 | return error; |
| 5797 | } |
| 5798 | |
| 5799 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5800 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5801 | cur->bc_private.b.firstblock = *firstblock; |
| 5802 | cur->bc_private.b.dfops = dfops; |
| 5803 | cur->bc_private.b.flags = 0; |
| 5804 | } |
| 5805 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5806 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5807 | xfs_iext_last(ifp, &icur); |
| 5808 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5809 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5810 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5811 | goto del_cursor; |
| 5812 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5813 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5814 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5815 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5816 | goto del_cursor; |
| 5817 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5818 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5819 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5820 | del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5821 | |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5822 | if (stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5823 | error = -EIO; |
| 5824 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5825 | } |
| 5826 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5827 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5828 | if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5829 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5830 | error = -EINVAL; |
| 5831 | goto del_cursor; |
| 5832 | } |
| 5833 | |
| 5834 | /* |
| 5835 | * Unlike a left shift (which involves a hole punch), a right |
| 5836 | * shift does not modify extent neighbors in any way. We should |
| 5837 | * never find mergeable extents in this scenario. Check anyways |
| 5838 | * and warn if we encounter two extents that could be one. |
| 5839 | */ |
| 5840 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5841 | WARN_ON_ONCE(1); |
| 5842 | } |
| 5843 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5844 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
| 5845 | &logflags, dfops, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5846 | if (error) |
| 5847 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5848 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5849 | if (!xfs_iext_prev_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5850 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5851 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5852 | goto del_cursor; |
| 5853 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5854 | |
| 5855 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5856 | del_cursor: |
| 5857 | if (cur) |
| 5858 | xfs_btree_del_cursor(cur, |
| 5859 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5860 | if (logflags) |
| 5861 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5862 | return error; |
| 5863 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5864 | |
| 5865 | /* |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5866 | * Splits an extent into two extents at split_fsb block such that it is the |
| 5867 | * first block of the current_ext. @ext is a target extent to be split. |
| 5868 | * @split_fsb is a block where the extents is split. If split_fsb lies in a |
| 5869 | * hole or the first block of extents, just return 0. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5870 | */ |
| 5871 | STATIC int |
| 5872 | xfs_bmap_split_extent_at( |
| 5873 | struct xfs_trans *tp, |
| 5874 | struct xfs_inode *ip, |
| 5875 | xfs_fileoff_t split_fsb, |
| 5876 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5877 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5878 | { |
| 5879 | int whichfork = XFS_DATA_FORK; |
| 5880 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5881 | struct xfs_bmbt_irec got; |
| 5882 | struct xfs_bmbt_irec new; /* split extent */ |
| 5883 | struct xfs_mount *mp = ip->i_mount; |
| 5884 | struct xfs_ifork *ifp; |
| 5885 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5886 | struct xfs_iext_cursor icur; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5887 | int error = 0; |
| 5888 | int logflags = 0; |
| 5889 | int i = 0; |
| 5890 | |
| 5891 | if (unlikely(XFS_TEST_ERROR( |
| 5892 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5893 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5894 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5895 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5896 | XFS_ERRLEVEL_LOW, mp); |
| 5897 | return -EFSCORRUPTED; |
| 5898 | } |
| 5899 | |
| 5900 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5901 | return -EIO; |
| 5902 | |
| 5903 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5904 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5905 | /* Read in all the extents */ |
| 5906 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5907 | if (error) |
| 5908 | return error; |
| 5909 | } |
| 5910 | |
| 5911 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5912 | * 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] | 5913 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5914 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5915 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5916 | return 0; |
| 5917 | |
| 5918 | gotblkcnt = split_fsb - got.br_startoff; |
| 5919 | new.br_startoff = split_fsb; |
| 5920 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5921 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5922 | new.br_state = got.br_state; |
| 5923 | |
| 5924 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5925 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5926 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5927 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5928 | cur->bc_private.b.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5929 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5930 | if (error) |
| 5931 | goto del_cursor; |
| 5932 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5933 | } |
| 5934 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5935 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5936 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, |
| 5937 | &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5938 | |
| 5939 | logflags = XFS_ILOG_CORE; |
| 5940 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5941 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5942 | if (error) |
| 5943 | goto del_cursor; |
| 5944 | } else |
| 5945 | logflags |= XFS_ILOG_DEXT; |
| 5946 | |
| 5947 | /* Add new extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5948 | xfs_iext_next(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5949 | xfs_iext_insert(ip, &icur, &new, 0); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5950 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5951 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5952 | |
| 5953 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5954 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5955 | if (error) |
| 5956 | goto del_cursor; |
| 5957 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5958 | error = xfs_btree_insert(cur, &i); |
| 5959 | if (error) |
| 5960 | goto del_cursor; |
| 5961 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5962 | } |
| 5963 | |
| 5964 | /* |
| 5965 | * Convert to a btree if necessary. |
| 5966 | */ |
| 5967 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 5968 | int tmp_logflags; /* partial log flag return val */ |
| 5969 | |
| 5970 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5971 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5972 | &cur, 0, &tmp_logflags, whichfork); |
| 5973 | logflags |= tmp_logflags; |
| 5974 | } |
| 5975 | |
| 5976 | del_cursor: |
| 5977 | if (cur) { |
| 5978 | cur->bc_private.b.allocated = 0; |
| 5979 | xfs_btree_del_cursor(cur, |
| 5980 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5981 | } |
| 5982 | |
| 5983 | if (logflags) |
| 5984 | xfs_trans_log_inode(tp, ip, logflags); |
| 5985 | return error; |
| 5986 | } |
| 5987 | |
| 5988 | int |
| 5989 | xfs_bmap_split_extent( |
| 5990 | struct xfs_inode *ip, |
| 5991 | xfs_fileoff_t split_fsb) |
| 5992 | { |
| 5993 | struct xfs_mount *mp = ip->i_mount; |
| 5994 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5995 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5996 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5997 | int error; |
| 5998 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 5999 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6000 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6001 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6002 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6003 | |
| 6004 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6005 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6006 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6007 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6008 | |
| 6009 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6010 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6011 | if (error) |
| 6012 | goto out; |
| 6013 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 6014 | error = xfs_defer_finish(&tp, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6015 | if (error) |
| 6016 | goto out; |
| 6017 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6018 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6019 | |
| 6020 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6021 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6022 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6023 | return error; |
| 6024 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6025 | |
| 6026 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6027 | static bool |
| 6028 | xfs_bmap_is_update_needed( |
| 6029 | struct xfs_bmbt_irec *bmap) |
| 6030 | { |
| 6031 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6032 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6033 | } |
| 6034 | |
| 6035 | /* Record a bmap intent. */ |
| 6036 | static int |
| 6037 | __xfs_bmap_add( |
| 6038 | struct xfs_mount *mp, |
| 6039 | struct xfs_defer_ops *dfops, |
| 6040 | enum xfs_bmap_intent_type type, |
| 6041 | struct xfs_inode *ip, |
| 6042 | int whichfork, |
| 6043 | struct xfs_bmbt_irec *bmap) |
| 6044 | { |
| 6045 | int error; |
| 6046 | struct xfs_bmap_intent *bi; |
| 6047 | |
| 6048 | trace_xfs_bmap_defer(mp, |
| 6049 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6050 | type, |
| 6051 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6052 | ip->i_ino, whichfork, |
| 6053 | bmap->br_startoff, |
| 6054 | bmap->br_blockcount, |
| 6055 | bmap->br_state); |
| 6056 | |
| 6057 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6058 | INIT_LIST_HEAD(&bi->bi_list); |
| 6059 | bi->bi_type = type; |
| 6060 | bi->bi_owner = ip; |
| 6061 | bi->bi_whichfork = whichfork; |
| 6062 | bi->bi_bmap = *bmap; |
| 6063 | |
Christoph Hellwig | 882d878 | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 6064 | error = xfs_defer_ijoin(dfops, bi->bi_owner); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6065 | if (error) { |
| 6066 | kmem_free(bi); |
| 6067 | return error; |
| 6068 | } |
| 6069 | |
| 6070 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6071 | return 0; |
| 6072 | } |
| 6073 | |
| 6074 | /* Map an extent into a file. */ |
| 6075 | int |
| 6076 | xfs_bmap_map_extent( |
| 6077 | struct xfs_mount *mp, |
| 6078 | struct xfs_defer_ops *dfops, |
| 6079 | struct xfs_inode *ip, |
| 6080 | struct xfs_bmbt_irec *PREV) |
| 6081 | { |
| 6082 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6083 | return 0; |
| 6084 | |
| 6085 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6086 | XFS_DATA_FORK, PREV); |
| 6087 | } |
| 6088 | |
| 6089 | /* Unmap an extent out of a file. */ |
| 6090 | int |
| 6091 | xfs_bmap_unmap_extent( |
| 6092 | struct xfs_mount *mp, |
| 6093 | struct xfs_defer_ops *dfops, |
| 6094 | struct xfs_inode *ip, |
| 6095 | struct xfs_bmbt_irec *PREV) |
| 6096 | { |
| 6097 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6098 | return 0; |
| 6099 | |
| 6100 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6101 | XFS_DATA_FORK, PREV); |
| 6102 | } |
| 6103 | |
| 6104 | /* |
| 6105 | * Process one of the deferred bmap operations. We pass back the |
| 6106 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6107 | */ |
| 6108 | int |
| 6109 | xfs_bmap_finish_one( |
| 6110 | struct xfs_trans *tp, |
| 6111 | struct xfs_defer_ops *dfops, |
| 6112 | struct xfs_inode *ip, |
| 6113 | enum xfs_bmap_intent_type type, |
| 6114 | int whichfork, |
| 6115 | xfs_fileoff_t startoff, |
| 6116 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6117 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6118 | xfs_exntst_t state) |
| 6119 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6120 | xfs_fsblock_t firstfsb; |
| 6121 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6122 | |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6123 | /* |
| 6124 | * firstfsb is tied to the transaction lifetime and is used to |
| 6125 | * ensure correct AG locking order and schedule work item |
| 6126 | * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us |
| 6127 | * to only making one bmap call per transaction, so it should |
| 6128 | * be safe to have it as a local variable here. |
| 6129 | */ |
| 6130 | firstfsb = NULLFSBLOCK; |
| 6131 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6132 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6133 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6134 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6135 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6136 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6137 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6138 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6139 | |
| 6140 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6141 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6142 | return -EIO; |
| 6143 | |
| 6144 | switch (type) { |
| 6145 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6146 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6147 | startblock, dfops); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6148 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6149 | break; |
| 6150 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6151 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
| 6152 | XFS_BMAPI_REMAP, 1, &firstfsb, dfops); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6153 | break; |
| 6154 | default: |
| 6155 | ASSERT(0); |
| 6156 | error = -EFSCORRUPTED; |
| 6157 | } |
| 6158 | |
| 6159 | return error; |
| 6160 | } |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame^] | 6161 | |
| 6162 | /* Check that an inode's extent does not have invalid flags or bad ranges. */ |
| 6163 | xfs_failaddr_t |
| 6164 | xfs_bmap_validate_extent( |
| 6165 | struct xfs_inode *ip, |
| 6166 | int whichfork, |
| 6167 | struct xfs_bmbt_irec *irec) |
| 6168 | { |
| 6169 | struct xfs_mount *mp = ip->i_mount; |
| 6170 | xfs_fsblock_t endfsb; |
| 6171 | bool isrt; |
| 6172 | |
| 6173 | isrt = XFS_IS_REALTIME_INODE(ip); |
| 6174 | endfsb = irec->br_startblock + irec->br_blockcount - 1; |
| 6175 | if (isrt) { |
| 6176 | if (!xfs_verify_rtbno(mp, irec->br_startblock)) |
| 6177 | return __this_address; |
| 6178 | if (!xfs_verify_rtbno(mp, endfsb)) |
| 6179 | return __this_address; |
| 6180 | } else { |
| 6181 | if (!xfs_verify_fsbno(mp, irec->br_startblock)) |
| 6182 | return __this_address; |
| 6183 | if (!xfs_verify_fsbno(mp, endfsb)) |
| 6184 | return __this_address; |
| 6185 | if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) != |
| 6186 | XFS_FSB_TO_AGNO(mp, endfsb)) |
| 6187 | return __this_address; |
| 6188 | } |
| 6189 | if (irec->br_state != XFS_EXT_NORM) { |
| 6190 | if (whichfork != XFS_DATA_FORK) |
| 6191 | return __this_address; |
| 6192 | if (!xfs_sb_version_hasextflgbit(&mp->m_sb)) |
| 6193 | return __this_address; |
| 6194 | } |
| 6195 | return NULL; |
| 6196 | } |