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" |
| 41 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_quota.h" |
| 43 | #include "xfs_trans_space.h" |
| 44 | #include "xfs_buf_item.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 45 | #include "xfs_trace.h" |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 46 | #include "xfs_symlink.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 47 | #include "xfs_attr_leaf.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 48 | #include "xfs_filestream.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 49 | #include "xfs_rmap.h" |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 50 | #include "xfs_ag_resv.h" |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 51 | #include "xfs_refcount.h" |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 52 | #include "xfs_icache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 56 | |
| 57 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 58 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | */ |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 62 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 63 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 65 | void |
| 66 | xfs_bmap_compute_maxlevels( |
| 67 | xfs_mount_t *mp, /* file system mount structure */ |
| 68 | int whichfork) /* data or attr fork */ |
| 69 | { |
| 70 | int level; /* btree level */ |
| 71 | uint maxblocks; /* max blocks at this level */ |
| 72 | uint maxleafents; /* max leaf entries possible */ |
| 73 | int maxrootrecs; /* max records in root block */ |
| 74 | int minleafrecs; /* min records in leaf block */ |
| 75 | int minnoderecs; /* min records in node block */ |
| 76 | int sz; /* root block size */ |
| 77 | |
| 78 | /* |
| 79 | * The maximum number of extents in a file, hence the maximum |
| 80 | * number of leaf entries, is controlled by the type of di_nextents |
| 81 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 82 | * (a signed 16-bit number, xfs_aextnum_t). |
| 83 | * |
| 84 | * Note that we can no longer assume that if we are in ATTR1 that |
| 85 | * the fork offset of all the inodes will be |
| 86 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 87 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 88 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 89 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 90 | * of a minimum size available. |
| 91 | */ |
| 92 | if (whichfork == XFS_DATA_FORK) { |
| 93 | maxleafents = MAXEXTNUM; |
| 94 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 95 | } else { |
| 96 | maxleafents = MAXAEXTNUM; |
| 97 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 98 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 99 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 100 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 101 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 102 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 103 | for (level = 1; maxblocks > 1; level++) { |
| 104 | if (maxblocks <= maxrootrecs) |
| 105 | maxblocks = 1; |
| 106 | else |
| 107 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 108 | } |
| 109 | mp->m_bm_maxlevels[whichfork] = level; |
| 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 112 | STATIC int /* error */ |
| 113 | xfs_bmbt_lookup_eq( |
| 114 | struct xfs_btree_cur *cur, |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 115 | struct xfs_bmbt_irec *irec, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 116 | int *stat) /* success/failure */ |
| 117 | { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 118 | cur->bc_rec.b = *irec; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 119 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 120 | } |
| 121 | |
| 122 | STATIC int /* error */ |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 123 | xfs_bmbt_lookup_first( |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 124 | struct xfs_btree_cur *cur, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 125 | int *stat) /* success/failure */ |
| 126 | { |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 127 | cur->bc_rec.b.br_startoff = 0; |
| 128 | cur->bc_rec.b.br_startblock = 0; |
| 129 | cur->bc_rec.b.br_blockcount = 0; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 130 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 131 | } |
| 132 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 133 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 134 | * Check if the inode needs to be converted to btree format. |
| 135 | */ |
| 136 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 137 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 138 | return whichfork != XFS_COW_FORK && |
| 139 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 140 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 141 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Check if the inode should be converted to extent format. |
| 146 | */ |
| 147 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 148 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 149 | return whichfork != XFS_COW_FORK && |
| 150 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 151 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 152 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 153 | } |
| 154 | |
| 155 | /* |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 156 | * 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] | 157 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 158 | */ |
| 159 | STATIC int |
| 160 | xfs_bmbt_update( |
| 161 | struct xfs_btree_cur *cur, |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 162 | struct xfs_bmbt_irec *irec) |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 163 | { |
| 164 | union xfs_btree_rec rec; |
| 165 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 166 | xfs_bmbt_disk_set_all(&rec.bmbt, irec); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 167 | return xfs_btree_update(cur, &rec); |
| 168 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 171 | * Compute the worst-case number of indirect blocks that will be used |
| 172 | * for ip's delayed extent of length "len". |
| 173 | */ |
| 174 | STATIC xfs_filblks_t |
| 175 | xfs_bmap_worst_indlen( |
| 176 | xfs_inode_t *ip, /* incore inode pointer */ |
| 177 | xfs_filblks_t len) /* delayed extent length */ |
| 178 | { |
| 179 | int level; /* btree level number */ |
| 180 | int maxrecs; /* maximum record count at this level */ |
| 181 | xfs_mount_t *mp; /* mount structure */ |
| 182 | xfs_filblks_t rval; /* return value */ |
| 183 | |
| 184 | mp = ip->i_mount; |
| 185 | maxrecs = mp->m_bmap_dmxr[0]; |
| 186 | for (level = 0, rval = 0; |
| 187 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 188 | level++) { |
| 189 | len += maxrecs - 1; |
| 190 | do_div(len, maxrecs); |
| 191 | rval += len; |
Darrick J. Wong | 5e5c943 | 2017-09-18 09:41:17 -0700 | [diff] [blame] | 192 | if (len == 1) |
| 193 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 194 | level - 1; |
| 195 | if (level == 0) |
| 196 | maxrecs = mp->m_bmap_dmxr[1]; |
| 197 | } |
| 198 | return rval; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Calculate the default attribute fork offset for newly created inodes. |
| 203 | */ |
| 204 | uint |
| 205 | xfs_default_attroffset( |
| 206 | struct xfs_inode *ip) |
| 207 | { |
| 208 | struct xfs_mount *mp = ip->i_mount; |
| 209 | uint offset; |
| 210 | |
| 211 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 212 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 213 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 214 | } else { |
| 215 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 216 | } |
| 217 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 218 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 219 | return offset; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Helper routine to reset inode di_forkoff field when switching |
| 224 | * attribute fork from local to extent format - we reset it where |
| 225 | * possible to make space available for inline data fork extents. |
| 226 | */ |
| 227 | STATIC void |
| 228 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 229 | xfs_inode_t *ip, |
| 230 | int whichfork) |
| 231 | { |
| 232 | if (whichfork == XFS_ATTR_FORK && |
| 233 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 234 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 235 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 236 | |
| 237 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 238 | ip->i_d.di_forkoff = dfl_forkoff; |
| 239 | } |
| 240 | } |
| 241 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 242 | #ifdef DEBUG |
| 243 | STATIC struct xfs_buf * |
| 244 | xfs_bmap_get_bp( |
| 245 | struct xfs_btree_cur *cur, |
| 246 | xfs_fsblock_t bno) |
| 247 | { |
| 248 | struct xfs_log_item_desc *lidp; |
| 249 | int i; |
| 250 | |
| 251 | if (!cur) |
| 252 | return NULL; |
| 253 | |
| 254 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 255 | if (!cur->bc_bufs[i]) |
| 256 | break; |
| 257 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 258 | return cur->bc_bufs[i]; |
| 259 | } |
| 260 | |
| 261 | /* Chase down all the log items to see if the bp is there */ |
| 262 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 263 | struct xfs_buf_log_item *bip; |
| 264 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 265 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 266 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 267 | return bip->bli_buf; |
| 268 | } |
| 269 | |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | STATIC void |
| 274 | xfs_check_block( |
| 275 | struct xfs_btree_block *block, |
| 276 | xfs_mount_t *mp, |
| 277 | int root, |
| 278 | short sz) |
| 279 | { |
| 280 | int i, j, dmxr; |
| 281 | __be64 *pp, *thispa; /* pointer to block address */ |
| 282 | xfs_bmbt_key_t *prevp, *keyp; |
| 283 | |
| 284 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 285 | |
| 286 | prevp = NULL; |
| 287 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 288 | dmxr = mp->m_bmap_dmxr[0]; |
| 289 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 290 | |
| 291 | if (prevp) { |
| 292 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 293 | be64_to_cpu(keyp->br_startoff)); |
| 294 | } |
| 295 | prevp = keyp; |
| 296 | |
| 297 | /* |
| 298 | * Compare the block numbers to see if there are dups. |
| 299 | */ |
| 300 | if (root) |
| 301 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 302 | else |
| 303 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 304 | |
| 305 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 306 | if (root) |
| 307 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 308 | else |
| 309 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 310 | if (*thispa == *pp) { |
| 311 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 312 | __func__, j, i, |
| 313 | (unsigned long long)be64_to_cpu(*thispa)); |
| 314 | panic("%s: ptrs are equal in node\n", |
| 315 | __func__); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * 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] | 323 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 324 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 325 | * them. The btree record ordering checks will still be done, so for such large |
| 326 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 327 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 328 | STATIC void |
| 329 | xfs_bmap_check_leaf_extents( |
| 330 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 331 | xfs_inode_t *ip, /* incore inode pointer */ |
| 332 | int whichfork) /* data or attr fork */ |
| 333 | { |
| 334 | struct xfs_btree_block *block; /* current btree block */ |
| 335 | xfs_fsblock_t bno; /* block # of "block" */ |
| 336 | xfs_buf_t *bp; /* buffer for "block" */ |
| 337 | int error; /* error return value */ |
| 338 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 339 | xfs_ifork_t *ifp; /* fork structure */ |
| 340 | int level; /* btree level, for checking */ |
| 341 | xfs_mount_t *mp; /* file system mount structure */ |
| 342 | __be64 *pp; /* pointer to block address */ |
| 343 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 344 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 345 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 346 | int bp_release = 0; |
| 347 | |
| 348 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 349 | return; |
| 350 | } |
| 351 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 352 | /* skip large extent count inodes */ |
| 353 | if (ip->i_d.di_nextents > 10000) |
| 354 | return; |
| 355 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 356 | bno = NULLFSBLOCK; |
| 357 | mp = ip->i_mount; |
| 358 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 359 | block = ifp->if_broot; |
| 360 | /* |
| 361 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 362 | */ |
| 363 | level = be16_to_cpu(block->bb_level); |
| 364 | ASSERT(level > 0); |
| 365 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 366 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 367 | bno = be64_to_cpu(*pp); |
| 368 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 369 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 370 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 371 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 372 | |
| 373 | /* |
| 374 | * Go down the tree until leaf level is reached, following the first |
| 375 | * pointer (leftmost) at each level. |
| 376 | */ |
| 377 | while (level-- > 0) { |
| 378 | /* See if buf is in cur first */ |
| 379 | bp_release = 0; |
| 380 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 381 | if (!bp) { |
| 382 | bp_release = 1; |
| 383 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 384 | XFS_BMAP_BTREE_REF, |
| 385 | &xfs_bmbt_buf_ops); |
| 386 | if (error) |
| 387 | goto error_norelse; |
| 388 | } |
| 389 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 390 | if (level == 0) |
| 391 | break; |
| 392 | |
| 393 | /* |
| 394 | * Check this block for basic sanity (increasing keys and |
| 395 | * no duplicate blocks). |
| 396 | */ |
| 397 | |
| 398 | xfs_check_block(block, mp, 0, 0); |
| 399 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 400 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 401 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 402 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 403 | if (bp_release) { |
| 404 | bp_release = 0; |
| 405 | xfs_trans_brelse(NULL, bp); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 411 | */ |
| 412 | i = 0; |
| 413 | |
| 414 | /* |
| 415 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 416 | */ |
| 417 | for (;;) { |
| 418 | xfs_fsblock_t nextbno; |
| 419 | xfs_extnum_t num_recs; |
| 420 | |
| 421 | |
| 422 | num_recs = xfs_btree_get_numrecs(block); |
| 423 | |
| 424 | /* |
| 425 | * Read-ahead the next leaf block, if any. |
| 426 | */ |
| 427 | |
| 428 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 429 | |
| 430 | /* |
| 431 | * Check all the extents to make sure they are OK. |
| 432 | * If we had a previous block, the last entry should |
| 433 | * conform with the first entry in this one. |
| 434 | */ |
| 435 | |
| 436 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 437 | if (i) { |
| 438 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 439 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 440 | xfs_bmbt_disk_get_startoff(ep)); |
| 441 | } |
| 442 | for (j = 1; j < num_recs; j++) { |
| 443 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 444 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 445 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 446 | xfs_bmbt_disk_get_startoff(nextp)); |
| 447 | ep = nextp; |
| 448 | } |
| 449 | |
| 450 | last = *ep; |
| 451 | i += num_recs; |
| 452 | if (bp_release) { |
| 453 | bp_release = 0; |
| 454 | xfs_trans_brelse(NULL, bp); |
| 455 | } |
| 456 | bno = nextbno; |
| 457 | /* |
| 458 | * If we've reached the end, stop. |
| 459 | */ |
| 460 | if (bno == NULLFSBLOCK) |
| 461 | break; |
| 462 | |
| 463 | bp_release = 0; |
| 464 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 465 | if (!bp) { |
| 466 | bp_release = 1; |
| 467 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 468 | XFS_BMAP_BTREE_REF, |
| 469 | &xfs_bmbt_buf_ops); |
| 470 | if (error) |
| 471 | goto error_norelse; |
| 472 | } |
| 473 | block = XFS_BUF_TO_BLOCK(bp); |
| 474 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 475 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 476 | return; |
| 477 | |
| 478 | error0: |
| 479 | xfs_warn(mp, "%s: at error0", __func__); |
| 480 | if (bp_release) |
| 481 | xfs_trans_brelse(NULL, bp); |
| 482 | error_norelse: |
| 483 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 484 | __func__, i); |
| 485 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 490 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 491 | * given the caller's original parameters. Specifically check the |
| 492 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 493 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 494 | */ |
| 495 | STATIC void |
| 496 | xfs_bmap_validate_ret( |
| 497 | xfs_fileoff_t bno, |
| 498 | xfs_filblks_t len, |
| 499 | int flags, |
| 500 | xfs_bmbt_irec_t *mval, |
| 501 | int nmap, |
| 502 | int ret_nmap) |
| 503 | { |
| 504 | int i; /* index to map values */ |
| 505 | |
| 506 | ASSERT(ret_nmap <= nmap); |
| 507 | |
| 508 | for (i = 0; i < ret_nmap; i++) { |
| 509 | ASSERT(mval[i].br_blockcount > 0); |
| 510 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 511 | ASSERT(mval[i].br_startoff >= bno); |
| 512 | ASSERT(mval[i].br_blockcount <= len); |
| 513 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 514 | bno + len); |
| 515 | } else { |
| 516 | ASSERT(mval[i].br_startoff < bno + len); |
| 517 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 518 | bno); |
| 519 | } |
| 520 | ASSERT(i == 0 || |
| 521 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 522 | mval[i].br_startoff); |
| 523 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 524 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 525 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 526 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | #else |
| 531 | #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] | 532 | #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] | 533 | #endif /* DEBUG */ |
| 534 | |
| 535 | /* |
| 536 | * bmap free list manipulation functions |
| 537 | */ |
| 538 | |
| 539 | /* |
| 540 | * Add the extent to the list of extents to be free at transaction end. |
| 541 | * The list is maintained sorted (by block number). |
| 542 | */ |
| 543 | void |
| 544 | xfs_bmap_add_free( |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 545 | struct xfs_mount *mp, |
| 546 | struct xfs_defer_ops *dfops, |
| 547 | xfs_fsblock_t bno, |
| 548 | xfs_filblks_t len, |
| 549 | struct xfs_owner_info *oinfo) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 550 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 551 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 552 | #ifdef DEBUG |
| 553 | xfs_agnumber_t agno; |
| 554 | xfs_agblock_t agbno; |
| 555 | |
| 556 | ASSERT(bno != NULLFSBLOCK); |
| 557 | ASSERT(len > 0); |
| 558 | ASSERT(len <= MAXEXTLEN); |
| 559 | ASSERT(!isnullstartblock(bno)); |
| 560 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 561 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 562 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 563 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 564 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 565 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 566 | #endif |
| 567 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 568 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 569 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 570 | new->xefi_startblock = bno; |
| 571 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 572 | if (oinfo) |
| 573 | new->xefi_oinfo = *oinfo; |
| 574 | else |
| 575 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 576 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
| 577 | XFS_FSB_TO_AGBNO(mp, bno), len); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 578 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Inode fork format manipulation functions |
| 583 | */ |
| 584 | |
| 585 | /* |
| 586 | * Transform a btree format file with only one leaf node, where the |
| 587 | * extents list will fit in the inode, into an extents format file. |
| 588 | * Since the file extents are already in-core, all we have to do is |
| 589 | * give up the space for the btree root and pitch the leaf block. |
| 590 | */ |
| 591 | STATIC int /* error */ |
| 592 | xfs_bmap_btree_to_extents( |
| 593 | xfs_trans_t *tp, /* transaction pointer */ |
| 594 | xfs_inode_t *ip, /* incore inode pointer */ |
| 595 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 596 | int *logflagsp, /* inode logging flags */ |
| 597 | int whichfork) /* data or attr fork */ |
| 598 | { |
| 599 | /* REFERENCED */ |
| 600 | struct xfs_btree_block *cblock;/* child btree block */ |
| 601 | xfs_fsblock_t cbno; /* child block number */ |
| 602 | xfs_buf_t *cbp; /* child block's buffer */ |
| 603 | int error; /* error return value */ |
| 604 | xfs_ifork_t *ifp; /* inode fork data */ |
| 605 | xfs_mount_t *mp; /* mount point structure */ |
| 606 | __be64 *pp; /* ptr to block address */ |
| 607 | struct xfs_btree_block *rblock;/* root btree block */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 608 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 609 | |
| 610 | mp = ip->i_mount; |
| 611 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 612 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 613 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 614 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 615 | rblock = ifp->if_broot; |
| 616 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 617 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 618 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 619 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 620 | cbno = be64_to_cpu(*pp); |
| 621 | *logflagsp = 0; |
| 622 | #ifdef DEBUG |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 623 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, |
| 624 | xfs_btree_check_lptr(cur, cbno, 1)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 625 | #endif |
| 626 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 627 | &xfs_bmbt_buf_ops); |
| 628 | if (error) |
| 629 | return error; |
| 630 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 631 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 632 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 633 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
| 634 | 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] | 635 | ip->i_d.di_nblocks--; |
| 636 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 637 | xfs_trans_binval(tp, cbp); |
| 638 | if (cur->bc_bufs[0] == cbp) |
| 639 | cur->bc_bufs[0] = NULL; |
| 640 | xfs_iroot_realloc(ip, -1, whichfork); |
| 641 | ASSERT(ifp->if_broot == NULL); |
| 642 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 643 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 644 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | /* |
| 649 | * Convert an extents-format file into a btree-format file. |
| 650 | * The new file will have a root block (in the inode) and a single child block. |
| 651 | */ |
| 652 | STATIC int /* error */ |
| 653 | xfs_bmap_extents_to_btree( |
| 654 | xfs_trans_t *tp, /* transaction pointer */ |
| 655 | xfs_inode_t *ip, /* incore inode pointer */ |
| 656 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 657 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 658 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 659 | int wasdel, /* converting a delayed alloc */ |
| 660 | int *logflagsp, /* inode logging flags */ |
| 661 | int whichfork) /* data or attr fork */ |
| 662 | { |
| 663 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 664 | xfs_buf_t *abp; /* buffer for ablock */ |
| 665 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 666 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 667 | struct xfs_btree_block *block; /* btree root block */ |
| 668 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 669 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 670 | int error; /* error return value */ |
| 671 | xfs_extnum_t i, cnt; /* extent record index */ |
| 672 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 673 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 674 | xfs_mount_t *mp; /* mount structure */ |
| 675 | xfs_extnum_t nextents; /* number of file extents */ |
| 676 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 677 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 678 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 679 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 680 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 681 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 682 | |
| 683 | /* |
| 684 | * Make space in the inode incore. |
| 685 | */ |
| 686 | xfs_iroot_realloc(ip, 1, whichfork); |
| 687 | ifp->if_flags |= XFS_IFBROOT; |
| 688 | |
| 689 | /* |
| 690 | * Fill in the root. |
| 691 | */ |
| 692 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 693 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 694 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 695 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 696 | /* |
| 697 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 698 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 699 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 700 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 701 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 702 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 703 | /* |
| 704 | * Convert to a btree with two levels, one record in root. |
| 705 | */ |
| 706 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 707 | memset(&args, 0, sizeof(args)); |
| 708 | args.tp = tp; |
| 709 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 710 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 711 | args.firstblock = *firstblock; |
| 712 | if (*firstblock == NULLFSBLOCK) { |
| 713 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 714 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 715 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 716 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 717 | args.fsbno = *firstblock; |
| 718 | } else { |
| 719 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 720 | args.fsbno = *firstblock; |
| 721 | } |
| 722 | args.minlen = args.maxlen = args.prod = 1; |
| 723 | args.wasdel = wasdel; |
| 724 | *logflagsp = 0; |
| 725 | if ((error = xfs_alloc_vextent(&args))) { |
| 726 | xfs_iroot_realloc(ip, -1, whichfork); |
| 727 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 728 | return error; |
| 729 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 730 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 731 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
| 732 | xfs_iroot_realloc(ip, -1, whichfork); |
| 733 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 734 | return -ENOSPC; |
| 735 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 736 | /* |
| 737 | * Allocation can't fail, the space was reserved. |
| 738 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 739 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 740 | args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 741 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 742 | cur->bc_private.b.allocated++; |
| 743 | ip->i_d.di_nblocks++; |
| 744 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 745 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 746 | /* |
| 747 | * Fill in the child block. |
| 748 | */ |
| 749 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 750 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 751 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 752 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 753 | XFS_BTREE_LONG_PTRS); |
| 754 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 755 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 756 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 757 | for (cnt = i = 0; i < nextents; i++) { |
| 758 | ep = xfs_iext_get_ext(ifp, i); |
| 759 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
| 760 | arp->l0 = cpu_to_be64(ep->l0); |
| 761 | arp->l1 = cpu_to_be64(ep->l1); |
| 762 | arp++; cnt++; |
| 763 | } |
| 764 | } |
| 765 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 766 | xfs_btree_set_numrecs(ablock, cnt); |
| 767 | |
| 768 | /* |
| 769 | * Fill in the root key and pointer. |
| 770 | */ |
| 771 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 772 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 773 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 774 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 775 | be16_to_cpu(block->bb_level))); |
| 776 | *pp = cpu_to_be64(args.fsbno); |
| 777 | |
| 778 | /* |
| 779 | * Do all this logging at the end so that |
| 780 | * the root is at the right level. |
| 781 | */ |
| 782 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 783 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 784 | ASSERT(*curp == NULL); |
| 785 | *curp = cur; |
| 786 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Convert a local file to an extents file. |
| 792 | * This code is out of bounds for data forks of regular files, |
| 793 | * since the file data needs to get logged so things will stay consistent. |
| 794 | * (The bmap-level manipulations are ok, though). |
| 795 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 796 | void |
| 797 | xfs_bmap_local_to_extents_empty( |
| 798 | struct xfs_inode *ip, |
| 799 | int whichfork) |
| 800 | { |
| 801 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 802 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 803 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 804 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 805 | ASSERT(ifp->if_bytes == 0); |
| 806 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 807 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 808 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 809 | ifp->if_flags &= ~XFS_IFINLINE; |
| 810 | ifp->if_flags |= XFS_IFEXTENTS; |
| 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; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 834 | |
| 835 | /* |
| 836 | * We don't want to deal with the case of keeping inode data inline yet. |
| 837 | * So sending the data fork of a regular inode is invalid. |
| 838 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 839 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 840 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 841 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 842 | |
| 843 | if (!ifp->if_bytes) { |
| 844 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 845 | flags = XFS_ILOG_CORE; |
| 846 | goto done; |
| 847 | } |
| 848 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 849 | flags = 0; |
| 850 | error = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 851 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
| 852 | XFS_IFINLINE); |
| 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 | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 896 | rec.br_startoff = 0; |
| 897 | rec.br_startblock = args.fsbno; |
| 898 | rec.br_blockcount = 1; |
| 899 | rec.br_state = XFS_EXT_NORM; |
| 900 | xfs_iext_insert(ip, 0, 1, &rec, 0); |
| 901 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 902 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 903 | ip->i_d.di_nblocks = 1; |
| 904 | xfs_trans_mod_dquot_byino(tp, ip, |
| 905 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 906 | flags |= xfs_ilog_fext(whichfork); |
| 907 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 908 | done: |
| 909 | *logflagsp = flags; |
| 910 | return error; |
| 911 | } |
| 912 | |
| 913 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 915 | */ |
| 916 | STATIC int /* error */ |
| 917 | xfs_bmap_add_attrfork_btree( |
| 918 | xfs_trans_t *tp, /* transaction pointer */ |
| 919 | xfs_inode_t *ip, /* incore inode pointer */ |
| 920 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 921 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | int *flags) /* inode logging flags */ |
| 923 | { |
| 924 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 925 | int error; /* error return value */ |
| 926 | xfs_mount_t *mp; /* file system mount struct */ |
| 927 | int stat; /* newroot status */ |
| 928 | |
| 929 | mp = ip->i_mount; |
| 930 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 931 | *flags |= XFS_ILOG_DBROOT; |
| 932 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 933 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 934 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | cur->bc_private.b.firstblock = *firstblock; |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 936 | error = xfs_bmbt_lookup_first(cur, &stat); |
| 937 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 939 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 940 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 941 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | goto error0; |
| 943 | if (stat == 0) { |
| 944 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 945 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
| 947 | *firstblock = cur->bc_private.b.firstblock; |
| 948 | cur->bc_private.b.allocated = 0; |
| 949 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 950 | } |
| 951 | return 0; |
| 952 | error0: |
| 953 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 954 | return error; |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 959 | */ |
| 960 | STATIC int /* error */ |
| 961 | xfs_bmap_add_attrfork_extents( |
| 962 | xfs_trans_t *tp, /* transaction pointer */ |
| 963 | xfs_inode_t *ip, /* incore inode pointer */ |
| 964 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 965 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | int *flags) /* inode logging flags */ |
| 967 | { |
| 968 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 969 | int error; /* error return value */ |
| 970 | |
| 971 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 972 | return 0; |
| 973 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 974 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | flags, XFS_DATA_FORK); |
| 976 | if (cur) { |
| 977 | cur->bc_private.b.allocated = 0; |
| 978 | xfs_btree_del_cursor(cur, |
| 979 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 980 | } |
| 981 | return error; |
| 982 | } |
| 983 | |
| 984 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 985 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 986 | * different data fork content type needs a different callout to do the |
| 987 | * conversion. Some are basic and only require special block initialisation |
| 988 | * callouts for the data formating, others (directories) are so specialised they |
| 989 | * handle everything themselves. |
| 990 | * |
| 991 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 992 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 993 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | */ |
| 995 | STATIC int /* error */ |
| 996 | xfs_bmap_add_attrfork_local( |
| 997 | xfs_trans_t *tp, /* transaction pointer */ |
| 998 | xfs_inode_t *ip, /* incore inode pointer */ |
| 999 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1000 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | int *flags) /* inode logging flags */ |
| 1002 | { |
| 1003 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
| 1005 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1006 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1007 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1008 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1010 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | dargs.dp = ip; |
| 1012 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1013 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1014 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | dargs.whichfork = XFS_DATA_FORK; |
| 1016 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1017 | return xfs_dir2_sf_to_block(&dargs); |
| 1018 | } |
| 1019 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1020 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1021 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1022 | flags, XFS_DATA_FORK, |
| 1023 | xfs_symlink_local_to_remote); |
| 1024 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1025 | /* should only be called for types that support local format data */ |
| 1026 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1027 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1031 | * Convert inode from non-attributed to attributed. |
| 1032 | * Must not be in a transaction, ip must not be locked. |
| 1033 | */ |
| 1034 | int /* error code */ |
| 1035 | xfs_bmap_add_attrfork( |
| 1036 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1037 | int size, /* space new attribute needs */ |
| 1038 | int rsvd) /* xact may use reserved blks */ |
| 1039 | { |
| 1040 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1041 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1042 | xfs_mount_t *mp; /* mount structure */ |
| 1043 | xfs_trans_t *tp; /* transaction pointer */ |
| 1044 | int blks; /* space reservation */ |
| 1045 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1046 | int logflags; /* logging flags */ |
| 1047 | int error; /* error return value */ |
| 1048 | |
| 1049 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1050 | |
| 1051 | mp = ip->i_mount; |
| 1052 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1053 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1054 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1055 | |
| 1056 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1057 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1058 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1059 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1060 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1061 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1062 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1063 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1064 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1065 | if (error) |
| 1066 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1067 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1068 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1069 | if (ip->i_d.di_anextents != 0) { |
| 1070 | error = -EFSCORRUPTED; |
| 1071 | goto trans_cancel; |
| 1072 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1073 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1074 | /* |
| 1075 | * For inodes coming from pre-6.2 filesystems. |
| 1076 | */ |
| 1077 | ASSERT(ip->i_d.di_aformat == 0); |
| 1078 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1079 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1080 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1081 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1082 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1083 | |
| 1084 | switch (ip->i_d.di_format) { |
| 1085 | case XFS_DINODE_FMT_DEV: |
| 1086 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1087 | break; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1088 | case XFS_DINODE_FMT_LOCAL: |
| 1089 | case XFS_DINODE_FMT_EXTENTS: |
| 1090 | case XFS_DINODE_FMT_BTREE: |
| 1091 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1092 | if (!ip->i_d.di_forkoff) |
| 1093 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1094 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1095 | version = 2; |
| 1096 | break; |
| 1097 | default: |
| 1098 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1099 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1100 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | ASSERT(ip->i_afp == NULL); |
| 1104 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1105 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1106 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1107 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1108 | switch (ip->i_d.di_format) { |
| 1109 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1110 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1111 | &logflags); |
| 1112 | break; |
| 1113 | case XFS_DINODE_FMT_EXTENTS: |
| 1114 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1115 | &dfops, &logflags); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1116 | break; |
| 1117 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1118 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1119 | &logflags); |
| 1120 | break; |
| 1121 | default: |
| 1122 | error = 0; |
| 1123 | break; |
| 1124 | } |
| 1125 | if (logflags) |
| 1126 | xfs_trans_log_inode(tp, ip, logflags); |
| 1127 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1128 | goto bmap_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1129 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1130 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1131 | bool log_sb = false; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1132 | |
| 1133 | spin_lock(&mp->m_sb_lock); |
| 1134 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1135 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1136 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1137 | } |
| 1138 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1139 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1140 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1141 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1142 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1143 | if (log_sb) |
| 1144 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1145 | } |
| 1146 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 1147 | error = xfs_defer_finish(&tp, &dfops); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1148 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1149 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1150 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1151 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1152 | return error; |
| 1153 | |
| 1154 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1155 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1156 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1157 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1158 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1159 | return error; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Internal and external extent tree search functions. |
| 1164 | */ |
| 1165 | |
| 1166 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1167 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1168 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1169 | int |
| 1170 | xfs_iread_extents( |
| 1171 | struct xfs_trans *tp, |
| 1172 | struct xfs_inode *ip, |
| 1173 | int whichfork) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1174 | { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1175 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | e8e0e17 | 2017-10-19 11:06:29 -0700 | [diff] [blame] | 1176 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1177 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1178 | xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
| 1179 | struct xfs_btree_block *block = ifp->if_broot; |
| 1180 | xfs_fsblock_t bno; |
| 1181 | struct xfs_buf *bp; |
| 1182 | xfs_extnum_t i, j; |
| 1183 | int level; |
| 1184 | __be64 *pp; |
| 1185 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1186 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1187 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1188 | |
| 1189 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 1190 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 1191 | return -EFSCORRUPTED; |
| 1192 | } |
| 1193 | |
| 1194 | ifp->if_bytes = 0; |
| 1195 | ifp->if_real_bytes = 0; |
| 1196 | xfs_iext_add(ifp, 0, nextents); |
| 1197 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1198 | /* |
| 1199 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1200 | */ |
| 1201 | level = be16_to_cpu(block->bb_level); |
| 1202 | ASSERT(level > 0); |
| 1203 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1204 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1205 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1206 | /* |
| 1207 | * Go down the tree until leaf level is reached, following the first |
| 1208 | * pointer (leftmost) at each level. |
| 1209 | */ |
| 1210 | while (level-- > 0) { |
| 1211 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1212 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1213 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1214 | goto out; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1215 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1216 | if (level == 0) |
| 1217 | break; |
| 1218 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1219 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1220 | XFS_WANT_CORRUPTED_GOTO(mp, |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1221 | XFS_FSB_SANITY_CHECK(mp, bno), out_brelse); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1222 | xfs_trans_brelse(tp, bp); |
| 1223 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1224 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1225 | /* |
| 1226 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1227 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1228 | i = 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1229 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1230 | /* |
| 1231 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1232 | */ |
| 1233 | for (;;) { |
| 1234 | xfs_bmbt_rec_t *frp; |
| 1235 | xfs_fsblock_t nextbno; |
| 1236 | xfs_extnum_t num_recs; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1237 | |
| 1238 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1239 | if (unlikely(i + num_recs > nextents)) { |
| 1240 | ASSERT(i + num_recs <= nextents); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1241 | xfs_warn(ip->i_mount, |
| 1242 | "corrupt dinode %Lu, (btree extents).", |
| 1243 | (unsigned long long) ip->i_ino); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1244 | XFS_CORRUPTION_ERROR(__func__, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1245 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1246 | error = -EFSCORRUPTED; |
| 1247 | goto out_brelse; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1248 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1249 | /* |
| 1250 | * Read-ahead the next leaf block, if any. |
| 1251 | */ |
| 1252 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1253 | if (nextbno != NULLFSBLOCK) |
| 1254 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1255 | &xfs_bmbt_buf_ops); |
| 1256 | /* |
| 1257 | * Copy records into the extent records. |
| 1258 | */ |
| 1259 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1260 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 1261 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
| 1262 | trp->l0 = be64_to_cpu(frp->l0); |
| 1263 | trp->l1 = be64_to_cpu(frp->l1); |
Christoph Hellwig | 0c1d9e4 | 2017-04-20 09:42:48 -0700 | [diff] [blame] | 1264 | if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1265 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
Christoph Hellwig | 0c1d9e4 | 2017-04-20 09:42:48 -0700 | [diff] [blame] | 1266 | XFS_ERRLEVEL_LOW, mp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1267 | error = -EFSCORRUPTED; |
| 1268 | goto out_brelse; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1269 | } |
Christoph Hellwig | e8e0e17 | 2017-10-19 11:06:29 -0700 | [diff] [blame] | 1270 | trace_xfs_read_extent(ip, i, state, _THIS_IP_); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1271 | } |
| 1272 | xfs_trans_brelse(tp, bp); |
| 1273 | bno = nextbno; |
| 1274 | /* |
| 1275 | * If we've reached the end, stop. |
| 1276 | */ |
| 1277 | if (bno == NULLFSBLOCK) |
| 1278 | break; |
| 1279 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1280 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1281 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1282 | goto out; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1283 | block = XFS_BUF_TO_BLOCK(bp); |
| 1284 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1285 | |
| 1286 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) { |
| 1287 | error = -EFSCORRUPTED; |
| 1288 | goto out; |
| 1289 | } |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1290 | ASSERT(i == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1291 | |
| 1292 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1293 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1294 | |
| 1295 | out_brelse: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1296 | xfs_trans_brelse(tp, bp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1297 | out: |
| 1298 | xfs_iext_destroy(ifp); |
| 1299 | return error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1300 | } |
| 1301 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1302 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1303 | * Returns the relative block number of the first unused block(s) in the given |
| 1304 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1305 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1306 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1307 | */ |
| 1308 | int /* error */ |
| 1309 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1310 | struct xfs_trans *tp, /* transaction pointer */ |
| 1311 | struct xfs_inode *ip, /* incore inode */ |
| 1312 | xfs_extlen_t len, /* size of hole to find */ |
| 1313 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1314 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1315 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1316 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1317 | struct xfs_bmbt_irec got; |
| 1318 | xfs_extnum_t idx = 0; |
| 1319 | xfs_fileoff_t lastaddr = 0; |
| 1320 | xfs_fileoff_t lowest, max; |
| 1321 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1322 | |
| 1323 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1324 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1325 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1326 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1327 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1328 | *first_unused = 0; |
| 1329 | return 0; |
| 1330 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1331 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1332 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1333 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1334 | if (error) |
| 1335 | return error; |
| 1336 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1337 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1338 | lowest = max = *first_unused; |
| 1339 | while (xfs_iext_get_extent(ifp, idx++, &got)) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1340 | /* |
| 1341 | * See if the hole before this extent will work. |
| 1342 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1343 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1344 | got.br_startoff - max >= len) |
| 1345 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1346 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1347 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1348 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1349 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1350 | *first_unused = max; |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1355 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1356 | * last_block (input value) in the file. |
| 1357 | * This is not based on i_size, it is based on the extent records. |
| 1358 | * Returns 0 for local files, as they do not have extent records. |
| 1359 | */ |
| 1360 | int /* error */ |
| 1361 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1362 | struct xfs_trans *tp, /* transaction pointer */ |
| 1363 | struct xfs_inode *ip, /* incore inode */ |
| 1364 | xfs_fileoff_t *last_block, /* last block */ |
| 1365 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1366 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1367 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1368 | struct xfs_bmbt_irec got; |
| 1369 | xfs_extnum_t idx; |
| 1370 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1371 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1372 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1373 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1374 | *last_block = 0; |
| 1375 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1376 | case XFS_DINODE_FMT_BTREE: |
| 1377 | case XFS_DINODE_FMT_EXTENTS: |
| 1378 | break; |
| 1379 | default: |
| 1380 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1381 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1382 | |
| 1383 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1384 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1385 | if (error) |
| 1386 | return error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1387 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1388 | |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1389 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &idx, &got)) |
| 1390 | *last_block = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1391 | return 0; |
| 1392 | } |
| 1393 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1394 | int |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1395 | xfs_bmap_last_extent( |
| 1396 | struct xfs_trans *tp, |
| 1397 | struct xfs_inode *ip, |
| 1398 | int whichfork, |
| 1399 | struct xfs_bmbt_irec *rec, |
| 1400 | int *is_empty) |
| 1401 | { |
| 1402 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1403 | int error; |
| 1404 | int nextents; |
| 1405 | |
| 1406 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1407 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1408 | if (error) |
| 1409 | return error; |
| 1410 | } |
| 1411 | |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1412 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1413 | if (nextents == 0) { |
| 1414 | *is_empty = 1; |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 1418 | xfs_iext_get_extent(ifp, nextents - 1, rec); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1419 | *is_empty = 0; |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * Check the last inode extent to determine whether this allocation will result |
| 1425 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1426 | * blocks at the end of the file which do not start at the previous data block, |
| 1427 | * we will try to align the new blocks at stripe unit boundaries. |
| 1428 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1429 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1430 | * at, or past the EOF. |
| 1431 | */ |
| 1432 | STATIC int |
| 1433 | xfs_bmap_isaeof( |
| 1434 | struct xfs_bmalloca *bma, |
| 1435 | int whichfork) |
| 1436 | { |
| 1437 | struct xfs_bmbt_irec rec; |
| 1438 | int is_empty; |
| 1439 | int error; |
| 1440 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1441 | bma->aeof = false; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1442 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1443 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1444 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1445 | return error; |
| 1446 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1447 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1448 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1449 | return 0; |
| 1450 | } |
| 1451 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1452 | /* |
| 1453 | * Check if we are allocation or past the last extent, or at least into |
| 1454 | * the last delayed allocated extent. |
| 1455 | */ |
| 1456 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1457 | (bma->offset >= rec.br_startoff && |
| 1458 | isnullstartblock(rec.br_startblock)); |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1463 | * Returns the file-relative block number of the first block past eof in |
| 1464 | * the file. This is not based on i_size, it is based on the extent records. |
| 1465 | * Returns 0 for local files, as they do not have extent records. |
| 1466 | */ |
| 1467 | int |
| 1468 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1469 | struct xfs_inode *ip, |
| 1470 | xfs_fileoff_t *last_block, |
| 1471 | int whichfork) |
| 1472 | { |
| 1473 | struct xfs_bmbt_irec rec; |
| 1474 | int is_empty; |
| 1475 | int error; |
| 1476 | |
| 1477 | *last_block = 0; |
| 1478 | |
| 1479 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1480 | return 0; |
| 1481 | |
| 1482 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1483 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1484 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1485 | |
| 1486 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1487 | if (error || is_empty) |
| 1488 | return error; |
| 1489 | |
| 1490 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | /* |
| 1495 | * Returns whether the selected fork of the inode has exactly one |
| 1496 | * block or not. For the data fork we check this matches di_size, |
| 1497 | * implying the file's range is 0..bsize-1. |
| 1498 | */ |
| 1499 | int /* 1=>1 block, 0=>otherwise */ |
| 1500 | xfs_bmap_one_block( |
| 1501 | xfs_inode_t *ip, /* incore inode */ |
| 1502 | int whichfork) /* data or attr fork */ |
| 1503 | { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1504 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1505 | int rval; /* return value */ |
| 1506 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 1507 | |
| 1508 | #ifndef DEBUG |
| 1509 | if (whichfork == XFS_DATA_FORK) |
| 1510 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1511 | #endif /* !DEBUG */ |
| 1512 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1513 | return 0; |
| 1514 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1515 | return 0; |
| 1516 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1517 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 1518 | xfs_iext_get_extent(ifp, 0, &s); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1519 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1520 | if (rval && whichfork == XFS_DATA_FORK) |
| 1521 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1522 | return rval; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
| 1526 | * Extent tree manipulation functions used during allocation. |
| 1527 | */ |
| 1528 | |
| 1529 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1530 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | */ |
| 1532 | STATIC int /* error */ |
| 1533 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1534 | struct xfs_bmalloca *bma, |
| 1535 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1537 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1540 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1542 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1543 | /* left is 0, right is 1, prev is 2 */ |
| 1544 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1545 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1546 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1547 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1548 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1550 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1551 | xfs_extnum_t *nextents; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1552 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1554 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1555 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1556 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1557 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1558 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1559 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1560 | ASSERT(bma->idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1561 | ASSERT(bma->idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1562 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1563 | ASSERT(!bma->cur || |
| 1564 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1565 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1566 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | #define LEFT r[0] |
| 1569 | #define RIGHT r[1] |
| 1570 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | |
| 1572 | /* |
| 1573 | * Set up a bunch of variables to make the tests simpler. |
| 1574 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1575 | xfs_iext_get_extent(ifp, bma->idx, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1577 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1579 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1580 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1581 | da_old = startblockval(PREV.br_startblock); |
| 1582 | da_new = 0; |
| 1583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | /* |
| 1585 | * Set flags determining what part of the previous delayed allocation |
| 1586 | * extent is being replaced by a real allocation. |
| 1587 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1588 | if (PREV.br_startoff == new->br_startoff) |
| 1589 | state |= BMAP_LEFT_FILLING; |
| 1590 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1591 | state |= BMAP_RIGHT_FILLING; |
| 1592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | /* |
| 1594 | * Check and set flags if this segment has a left neighbor. |
| 1595 | * Don't set contiguous if the combined extent would be too large. |
| 1596 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1597 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1598 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 1599 | xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1600 | |
| 1601 | if (isnullstartblock(LEFT.br_startblock)) |
| 1602 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1604 | |
| 1605 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1606 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1607 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1608 | LEFT.br_state == new->br_state && |
| 1609 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1610 | state |= BMAP_LEFT_CONTIG; |
| 1611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | /* |
| 1613 | * Check and set flags if this segment has a right neighbor. |
| 1614 | * Don't set contiguous if the combined extent would be too large. |
| 1615 | * Also check for all-three-contiguous being too large. |
| 1616 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1617 | if (bma->idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1618 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 1619 | xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1620 | |
| 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 | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1650 | bma->idx--; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1651 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1652 | xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1653 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1654 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1655 | (*nextents)--; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1656 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1658 | else { |
| 1659 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1660 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1661 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1663 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1664 | error = xfs_btree_delete(bma->cur, &i); |
| 1665 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1667 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1668 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1669 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1671 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1672 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1673 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | goto done; |
| 1675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | break; |
| 1677 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1678 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | /* |
| 1680 | * Filling in all of a previously delayed allocation extent. |
| 1681 | * The left neighbor is contiguous, the right is not. |
| 1682 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1683 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 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 | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1687 | xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1688 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1689 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1690 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | rval = XFS_ILOG_DEXT; |
| 1692 | else { |
| 1693 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1694 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1695 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1697 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1698 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1699 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | goto done; |
| 1701 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | break; |
| 1703 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1704 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | /* |
| 1706 | * Filling in all of a previously delayed allocation extent. |
| 1707 | * The right neighbor is contiguous, the left is not. |
| 1708 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1709 | PREV.br_startblock = new->br_startblock; |
| 1710 | PREV.br_blockcount += RIGHT.br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1711 | xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1712 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1713 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1714 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | rval = XFS_ILOG_DEXT; |
| 1716 | else { |
| 1717 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1718 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1719 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1721 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1722 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1723 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | goto done; |
| 1725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | break; |
| 1727 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1728 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | /* |
| 1730 | * Filling in all of a previously delayed allocation extent. |
| 1731 | * Neither the left nor right neighbors are contiguous with |
| 1732 | * the new one. |
| 1733 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1734 | PREV.br_startblock = new->br_startblock; |
| 1735 | PREV.br_state = new->br_state; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1736 | xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1737 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1738 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1739 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1741 | else { |
| 1742 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1743 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1744 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1746 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1747 | error = xfs_btree_insert(bma->cur, &i); |
| 1748 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1750 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | break; |
| 1753 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1754 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | /* |
| 1756 | * Filling in the first part of a previous delayed allocation. |
| 1757 | * The left neighbor is contiguous. |
| 1758 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1759 | old = LEFT; |
| 1760 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1761 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1762 | startblockval(PREV.br_startblock)); |
| 1763 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1764 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1765 | xfs_iext_update_extent(bma->ip, state, bma->idx - 1, &LEFT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1766 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1767 | PREV.br_blockcount = temp = PREV.br_blockcount - new->br_blockcount; |
| 1768 | PREV.br_startoff += new->br_blockcount; |
| 1769 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1770 | xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1771 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1772 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | rval = XFS_ILOG_DEXT; |
| 1774 | else { |
| 1775 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1776 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1777 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1779 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1780 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1781 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | goto done; |
| 1783 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1784 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1785 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | break; |
| 1787 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1788 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | /* |
| 1790 | * Filling in the first part of a previous delayed allocation. |
| 1791 | * The left neighbor is not contiguous. |
| 1792 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1793 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1794 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1795 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1797 | else { |
| 1798 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1799 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1800 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1802 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1803 | error = xfs_btree_insert(bma->cur, &i); |
| 1804 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1806 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1808 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1809 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1810 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1811 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1812 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | rval |= tmp_rval; |
| 1814 | if (error) |
| 1815 | goto done; |
| 1816 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1817 | |
| 1818 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1819 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1820 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1821 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1822 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1823 | PREV.br_startoff = new_endoff; |
| 1824 | PREV.br_blockcount = temp; |
| 1825 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1826 | xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | break; |
| 1828 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1829 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | /* |
| 1831 | * Filling in the last part of a previous delayed allocation. |
| 1832 | * The right neighbor is contiguous with the new allocation. |
| 1833 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1834 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1835 | RIGHT.br_startoff = new->br_startoff; |
| 1836 | RIGHT.br_startblock = new->br_startblock; |
| 1837 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1838 | xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &RIGHT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1839 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1840 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | rval = XFS_ILOG_DEXT; |
| 1842 | else { |
| 1843 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1844 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1845 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1847 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1848 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1849 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | goto done; |
| 1851 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1852 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1853 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1854 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1855 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1856 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1857 | PREV.br_blockcount = temp; |
| 1858 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1859 | xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1860 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1861 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | break; |
| 1863 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1864 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | /* |
| 1866 | * Filling in the last part of a previous delayed allocation. |
| 1867 | * The right neighbor is not contiguous. |
| 1868 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1869 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1870 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1871 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1873 | else { |
| 1874 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1875 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1876 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1878 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1879 | error = xfs_btree_insert(bma->cur, &i); |
| 1880 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1882 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1884 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1885 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1886 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1887 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1888 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | rval |= tmp_rval; |
| 1890 | if (error) |
| 1891 | goto done; |
| 1892 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1893 | |
| 1894 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1895 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1896 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1897 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1898 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1899 | PREV.br_startblock = nullstartblock(da_new); |
| 1900 | PREV.br_blockcount = temp; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1901 | xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1902 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1903 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | break; |
| 1905 | |
| 1906 | case 0: |
| 1907 | /* |
| 1908 | * Filling in the middle part of a previous delayed allocation. |
| 1909 | * Contiguity is impossible here. |
| 1910 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1911 | * |
| 1912 | * We start with a delayed allocation: |
| 1913 | * |
| 1914 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1915 | * PREV @ idx |
| 1916 | * |
| 1917 | * and we are allocating: |
| 1918 | * +rrrrrrrrrrrrrrrrr+ |
| 1919 | * new |
| 1920 | * |
| 1921 | * and we set it up for insertion as: |
| 1922 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1923 | * new |
| 1924 | * PREV @ idx LEFT RIGHT |
| 1925 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1927 | old = PREV; |
| 1928 | |
| 1929 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1930 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1931 | |
| 1932 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1933 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1934 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1935 | RIGHT.br_blockcount = |
| 1936 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1937 | RIGHT.br_startblock = |
| 1938 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1939 | RIGHT.br_blockcount)); |
| 1940 | |
| 1941 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1942 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1943 | PREV.br_startblock = |
| 1944 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1945 | PREV.br_blockcount)); |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 1946 | xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1947 | |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1948 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1949 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1950 | (*nextents)++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1951 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1952 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1954 | else { |
| 1955 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1956 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1957 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1959 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1960 | error = xfs_btree_insert(bma->cur, &i); |
| 1961 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1963 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1965 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1966 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1967 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1968 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1969 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | rval |= tmp_rval; |
| 1971 | if (error) |
| 1972 | goto done; |
| 1973 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1974 | |
| 1975 | da_new = startblockval(PREV.br_startblock) + |
| 1976 | startblockval(RIGHT.br_startblock); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1977 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | break; |
| 1979 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1980 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1981 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1982 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1983 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1984 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1985 | case BMAP_LEFT_CONTIG: |
| 1986 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | /* |
| 1988 | * These cases are all impossible. |
| 1989 | */ |
| 1990 | ASSERT(0); |
| 1991 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1992 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 1993 | /* add reverse mapping */ |
| 1994 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 1995 | if (error) |
| 1996 | goto done; |
| 1997 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1998 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1999 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2000 | int tmp_logflags; /* partial log flag return val */ |
| 2001 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2002 | ASSERT(bma->cur == NULL); |
| 2003 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2004 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2005 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2006 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2007 | if (error) |
| 2008 | goto done; |
| 2009 | } |
| 2010 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2011 | if (bma->cur) { |
| 2012 | da_new += bma->cur->bc_private.b.allocated; |
| 2013 | bma->cur->bc_private.b.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2016 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2017 | if (da_new != da_old) { |
| 2018 | ASSERT(state == 0 || da_new < da_old); |
| 2019 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2020 | false); |
| 2021 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2022 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2023 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2025 | if (whichfork != XFS_COW_FORK) |
| 2026 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | return error; |
| 2028 | #undef LEFT |
| 2029 | #undef RIGHT |
| 2030 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2034 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | */ |
| 2036 | STATIC int /* error */ |
| 2037 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2038 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2040 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2041 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2043 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2044 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2045 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2046 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2051 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2054 | /* left is 0, right is 1, prev is 2 */ |
| 2055 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2056 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2057 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2058 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2060 | *logflagsp = 0; |
| 2061 | |
| 2062 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2063 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2064 | |
| 2065 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2066 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2067 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2068 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2069 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | #define LEFT r[0] |
| 2072 | #define RIGHT r[1] |
| 2073 | #define PREV r[2] |
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 | /* |
| 2076 | * Set up a bunch of variables to make the tests simpler. |
| 2077 | */ |
| 2078 | error = 0; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2079 | xfs_iext_get_extent(ifp, *idx, &PREV); |
| 2080 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2082 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2083 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | /* |
| 2086 | * Set flags determining what part of the previous oldext allocation |
| 2087 | * extent is being replaced by a newext allocation. |
| 2088 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2089 | if (PREV.br_startoff == new->br_startoff) |
| 2090 | state |= BMAP_LEFT_FILLING; |
| 2091 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2092 | state |= BMAP_RIGHT_FILLING; |
| 2093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | /* |
| 2095 | * Check and set flags if this segment has a left neighbor. |
| 2096 | * Don't set contiguous if the combined extent would be too large. |
| 2097 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2098 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2099 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 2100 | xfs_iext_get_extent(ifp, *idx - 1, &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2101 | |
| 2102 | if (isnullstartblock(LEFT.br_startblock)) |
| 2103 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2105 | |
| 2106 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2107 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2108 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2109 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2110 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2111 | state |= BMAP_LEFT_CONTIG; |
| 2112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | /* |
| 2114 | * Check and set flags if this segment has a right neighbor. |
| 2115 | * Don't set contiguous if the combined extent would be too large. |
| 2116 | * Also check for all-three-contiguous being too large. |
| 2117 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2118 | if (*idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2119 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 2120 | xfs_iext_get_extent(ifp, *idx + 1, &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2121 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2122 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2124 | |
| 2125 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2126 | new_endoff == RIGHT.br_startoff && |
| 2127 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2128 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2129 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2130 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2131 | BMAP_RIGHT_FILLING)) != |
| 2132 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2133 | BMAP_RIGHT_FILLING) || |
| 2134 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2135 | <= MAXEXTLEN)) |
| 2136 | state |= BMAP_RIGHT_CONTIG; |
| 2137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | /* |
| 2139 | * Switch out based on the FILLING and CONTIG state bits. |
| 2140 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2141 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2142 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2143 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2144 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | /* |
| 2146 | * Setting all of a previous oldext extent to newext. |
| 2147 | * The left and right neighbors are both contiguous with new. |
| 2148 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2149 | --*idx; |
| 2150 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2151 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2152 | xfs_iext_update_extent(ip, state, *idx, &LEFT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2153 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2154 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2155 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2156 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | if (cur == NULL) |
| 2158 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2159 | else { |
| 2160 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2161 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2162 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2164 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2165 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2167 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2168 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2170 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2171 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2173 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2174 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2176 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2177 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2178 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | goto done; |
| 2180 | } |
| 2181 | break; |
| 2182 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2183 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | /* |
| 2185 | * Setting all of a previous oldext extent to newext. |
| 2186 | * The left neighbor is contiguous, the right is not. |
| 2187 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2188 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2189 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2190 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2191 | xfs_iext_update_extent(ip, state, *idx, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2192 | |
| 2193 | xfs_iext_remove(ip, *idx + 1, 1, state); |
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 | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2223 | xfs_iext_update_extent(ip, state, *idx, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2224 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2225 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2226 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2227 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | if (cur == NULL) |
| 2229 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2230 | else { |
| 2231 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2232 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2233 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2235 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2236 | if ((error = xfs_btree_delete(cur, &i))) |
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 | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2239 | if ((error = xfs_btree_decrement(cur, 0, &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 | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2242 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2243 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | goto done; |
| 2245 | } |
| 2246 | break; |
| 2247 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2248 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | /* |
| 2250 | * Setting all of a previous oldext extent to newext. |
| 2251 | * Neither the left nor right neighbors are contiguous with |
| 2252 | * the new one. |
| 2253 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2254 | PREV.br_state = new->br_state; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2255 | xfs_iext_update_extent(ip, state, *idx, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | if (cur == NULL) |
| 2258 | rval = XFS_ILOG_DEXT; |
| 2259 | else { |
| 2260 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2261 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2262 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2264 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2265 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2266 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | goto done; |
| 2268 | } |
| 2269 | break; |
| 2270 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2271 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | /* |
| 2273 | * Setting the first part of a previous oldext extent to newext. |
| 2274 | * The left neighbor is contiguous. |
| 2275 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2276 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2277 | xfs_iext_update_extent(ip, state, *idx - 1, &LEFT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2278 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2279 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2280 | PREV.br_startoff += new->br_blockcount; |
| 2281 | PREV.br_startblock += new->br_blockcount; |
| 2282 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2283 | xfs_iext_update_extent(ip, state, *idx, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2284 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2285 | --*idx; |
| 2286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | if (cur == NULL) |
| 2288 | rval = XFS_ILOG_DEXT; |
| 2289 | else { |
| 2290 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2291 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2292 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2294 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2295 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2296 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2298 | error = xfs_btree_decrement(cur, 0, &i); |
| 2299 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2301 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2302 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | goto done; |
| 2304 | } |
| 2305 | break; |
| 2306 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2307 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | /* |
| 2309 | * Setting the first part of a previous oldext extent to newext. |
| 2310 | * The left neighbor is not contiguous. |
| 2311 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2312 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2313 | PREV.br_startoff += new->br_blockcount; |
| 2314 | PREV.br_startblock += new->br_blockcount; |
| 2315 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2316 | xfs_iext_update_extent(ip, state, *idx, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2317 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2318 | xfs_iext_insert(ip, *idx, 1, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2319 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2320 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | if (cur == NULL) |
| 2322 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2323 | else { |
| 2324 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2325 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2326 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2328 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2329 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2330 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | goto done; |
| 2332 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2333 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2335 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | } |
| 2337 | break; |
| 2338 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2339 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | /* |
| 2341 | * Setting the last part of a previous oldext extent to newext. |
| 2342 | * The right neighbor is contiguous with the new allocation. |
| 2343 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2344 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2345 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2346 | xfs_iext_update_extent(ip, state, *idx, &PREV); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2347 | |
| 2348 | ++*idx; |
| 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 | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2353 | xfs_iext_update_extent(ip, state, *idx, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | if (cur == NULL) |
| 2356 | rval = XFS_ILOG_DEXT; |
| 2357 | else { |
| 2358 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2359 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2360 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2362 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2363 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2364 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2366 | error = xfs_btree_increment(cur, 0, &i); |
| 2367 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2369 | error = xfs_bmbt_update(cur, &RIGHT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2370 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | goto done; |
| 2372 | } |
| 2373 | break; |
| 2374 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2375 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | /* |
| 2377 | * Setting the last part of a previous oldext extent to newext. |
| 2378 | * The right neighbor is not contiguous. |
| 2379 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2380 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2381 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2382 | xfs_iext_update_extent(ip, state, *idx, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2383 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2384 | ++*idx; |
| 2385 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2386 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2387 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2388 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | if (cur == NULL) |
| 2390 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2391 | else { |
| 2392 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2393 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2394 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2396 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2397 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2398 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2400 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2401 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2403 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2404 | if ((error = xfs_btree_insert(cur, &i))) |
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 == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | } |
| 2408 | break; |
| 2409 | |
| 2410 | case 0: |
| 2411 | /* |
| 2412 | * Setting the middle part of a previous oldext extent to |
| 2413 | * newext. Contiguity is impossible here. |
| 2414 | * One extent becomes three extents. |
| 2415 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2416 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2417 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2418 | xfs_iext_update_extent(ip, state, *idx, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | r[0] = *new; |
| 2421 | r[1].br_startoff = new_endoff; |
| 2422 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2423 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2425 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2426 | |
| 2427 | ++*idx; |
| 2428 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 2429 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2430 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2431 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | if (cur == NULL) |
| 2433 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2434 | else { |
| 2435 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2436 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2437 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2439 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2441 | error = xfs_bmbt_update(cur, &r[1]); |
| 2442 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | goto done; |
| 2444 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2446 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2448 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2449 | /* |
| 2450 | * Reset the cursor to the position of the new extent |
| 2451 | * we are about to insert as we can't trust it after |
| 2452 | * the previous insert. |
| 2453 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2454 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2455 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2457 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2459 | if ((error = xfs_btree_insert(cur, &i))) |
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 == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | } |
| 2463 | break; |
| 2464 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2465 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2466 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2467 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2468 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2469 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2470 | case BMAP_LEFT_CONTIG: |
| 2471 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | /* |
| 2473 | * These cases are all impossible. |
| 2474 | */ |
| 2475 | ASSERT(0); |
| 2476 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2477 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2478 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2479 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2480 | if (error) |
| 2481 | goto done; |
| 2482 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2483 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2484 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2485 | int tmp_logflags; /* partial log flag return val */ |
| 2486 | |
| 2487 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2488 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2489 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2490 | *logflagsp |= tmp_logflags; |
| 2491 | if (error) |
| 2492 | goto done; |
| 2493 | } |
| 2494 | |
| 2495 | /* clear out the allocated field, done with it now in any case. */ |
| 2496 | if (cur) { |
| 2497 | cur->bc_private.b.allocated = 0; |
| 2498 | *curp = cur; |
| 2499 | } |
| 2500 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2501 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2503 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | return error; |
| 2505 | #undef LEFT |
| 2506 | #undef RIGHT |
| 2507 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2511 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2513 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | xfs_bmap_add_extent_hole_delay( |
| 2515 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2516 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2517 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2518 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2520 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2522 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2523 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2524 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2525 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2526 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2528 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2529 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | /* |
| 2532 | * Check and set flags if this segment has a left neighbor |
| 2533 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2534 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2535 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 2536 | xfs_iext_get_extent(ifp, *idx - 1, &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2537 | |
| 2538 | if (isnullstartblock(left.br_startblock)) |
| 2539 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | /* |
| 2543 | * Check and set flags if the current (right) segment exists. |
| 2544 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2545 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2546 | if (*idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2547 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 2548 | xfs_iext_get_extent(ifp, *idx, &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2549 | |
| 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 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2581 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | temp = left.br_blockcount + new->br_blockcount + |
| 2583 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2584 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2585 | oldlen = startblockval(left.br_startblock) + |
| 2586 | startblockval(new->br_startblock) + |
| 2587 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2588 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2589 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2590 | left.br_startblock = nullstartblock(newlen); |
| 2591 | left.br_blockcount = temp; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2592 | xfs_iext_update_extent(ip, state, *idx, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2593 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2594 | xfs_iext_remove(ip, *idx + 1, 1, state); |
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 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2603 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2605 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2606 | oldlen = startblockval(left.br_startblock) + |
| 2607 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2608 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2609 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2610 | left.br_blockcount = temp; |
| 2611 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2612 | xfs_iext_update_extent(ip, state, *idx, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | break; |
| 2614 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2615 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | /* |
| 2617 | * New allocation is contiguous with a delayed allocation |
| 2618 | * on the right. |
| 2619 | * Merge the new allocation with the right neighbor. |
| 2620 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2622 | oldlen = startblockval(new->br_startblock) + |
| 2623 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2624 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2625 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2626 | right.br_startoff = new->br_startoff; |
| 2627 | right.br_startblock = nullstartblock(newlen); |
| 2628 | right.br_blockcount = temp; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2629 | xfs_iext_update_extent(ip, state, *idx, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | break; |
| 2631 | |
| 2632 | case 0: |
| 2633 | /* |
| 2634 | * New allocation is not contiguous with another |
| 2635 | * delayed allocation. |
| 2636 | * Insert a new entry. |
| 2637 | */ |
| 2638 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2639 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | break; |
| 2641 | } |
| 2642 | if (oldlen != newlen) { |
| 2643 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2644 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2645 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | /* |
| 2647 | * Nothing to do for disk quota accounting here. |
| 2648 | */ |
| 2649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2653 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | */ |
| 2655 | STATIC int /* error */ |
| 2656 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2657 | struct xfs_trans *tp, |
| 2658 | struct xfs_inode *ip, |
| 2659 | int whichfork, |
| 2660 | xfs_extnum_t *idx, |
| 2661 | struct xfs_btree_cur **curp, |
| 2662 | struct xfs_bmbt_irec *new, |
| 2663 | xfs_fsblock_t *first, |
| 2664 | struct xfs_defer_ops *dfops, |
| 2665 | int *logflagsp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2667 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2668 | struct xfs_mount *mp = ip->i_mount; |
| 2669 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2673 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2674 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2675 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2676 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2678 | ASSERT(*idx >= 0); |
| 2679 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2680 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2681 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2682 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2683 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | /* |
| 2686 | * Check and set flags if this segment has a left neighbor. |
| 2687 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2688 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2689 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 2690 | xfs_iext_get_extent(ifp, *idx - 1, &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2691 | if (isnullstartblock(left.br_startblock)) |
| 2692 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | /* |
| 2696 | * Check and set flags if this segment has a current value. |
| 2697 | * Not true if we're inserting into the "hole" at eof. |
| 2698 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2699 | if (*idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2700 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 2701 | xfs_iext_get_extent(ifp, *idx, &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2702 | if (isnullstartblock(right.br_startblock)) |
| 2703 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | /* |
| 2707 | * We're inserting a real allocation between "left" and "right". |
| 2708 | * Set the contiguity flags. Don't let extents get too large. |
| 2709 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2710 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2711 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2712 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2713 | left.br_state == new->br_state && |
| 2714 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2715 | state |= BMAP_LEFT_CONTIG; |
| 2716 | |
| 2717 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2718 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2719 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2720 | new->br_state == right.br_state && |
| 2721 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2722 | (!(state & BMAP_LEFT_CONTIG) || |
| 2723 | left.br_blockcount + new->br_blockcount + |
| 2724 | right.br_blockcount <= MAXEXTLEN)) |
| 2725 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2727 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | /* |
| 2729 | * Select which case we're in here, and implement it. |
| 2730 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2731 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2732 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | /* |
| 2734 | * New allocation is contiguous with real allocations on the |
| 2735 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2736 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2738 | --*idx; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2739 | left.br_blockcount += new->br_blockcount + right.br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2740 | xfs_iext_update_extent(ip, state, *idx, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2741 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2742 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2743 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2744 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2745 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2746 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2747 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2748 | } else { |
| 2749 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2750 | error = xfs_bmbt_lookup_eq(cur, &right, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2751 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2752 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2753 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2754 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2755 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2756 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2757 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2758 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2759 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2760 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2761 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2762 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2763 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2764 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2766 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2768 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | /* |
| 2770 | * New allocation is contiguous with a real allocation |
| 2771 | * on the left. |
| 2772 | * Merge the new allocation with the left neighbor. |
| 2773 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2774 | --*idx; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2775 | old = left; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2776 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2777 | left.br_blockcount += new->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2778 | xfs_iext_update_extent(ip, state, *idx, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2779 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2780 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2781 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2782 | } else { |
| 2783 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2784 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2785 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2786 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2787 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2788 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2789 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2790 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2792 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2794 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | /* |
| 2796 | * New allocation is contiguous with a real allocation |
| 2797 | * on the right. |
| 2798 | * Merge the new allocation with the right neighbor. |
| 2799 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2800 | old = right; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2801 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2802 | right.br_startoff = new->br_startoff; |
| 2803 | right.br_startblock = new->br_startblock; |
| 2804 | right.br_blockcount += new->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2805 | xfs_iext_update_extent(ip, state, *idx, &right); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2806 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2807 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2808 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2809 | } else { |
| 2810 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2811 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2812 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2813 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2814 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2815 | error = xfs_bmbt_update(cur, &right); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2816 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2817 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2819 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | |
| 2821 | case 0: |
| 2822 | /* |
| 2823 | * New allocation is not contiguous with another |
| 2824 | * real allocation. |
| 2825 | * Insert a new entry. |
| 2826 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2827 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2828 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2829 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2830 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2831 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2832 | } else { |
| 2833 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2834 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2835 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2836 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2837 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2838 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2839 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2840 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2841 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2843 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2845 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2846 | /* add reverse mapping */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2847 | error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2848 | if (error) |
| 2849 | goto done; |
| 2850 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2851 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2852 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2853 | int tmp_logflags; /* partial log flag return val */ |
| 2854 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2855 | ASSERT(cur == NULL); |
| 2856 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2857 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2858 | *logflagsp |= tmp_logflags; |
| 2859 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2860 | if (error) |
| 2861 | goto done; |
| 2862 | } |
| 2863 | |
| 2864 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2865 | if (cur) |
| 2866 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2867 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2868 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2869 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2870 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2871 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2874 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2875 | * Functions used in the extent read, allocate and remove paths |
| 2876 | */ |
| 2877 | |
| 2878 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2879 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2880 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2881 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2882 | xfs_bmap_extsize_align( |
| 2883 | xfs_mount_t *mp, |
| 2884 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2885 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2886 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2887 | int rt, /* is this a realtime inode? */ |
| 2888 | int eof, /* is extent at end-of-file? */ |
| 2889 | int delay, /* creating delalloc extent? */ |
| 2890 | int convert, /* overwriting unwritten extent? */ |
| 2891 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2892 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2893 | { |
| 2894 | xfs_fileoff_t orig_off; /* original offset */ |
| 2895 | xfs_extlen_t orig_alen; /* original length */ |
| 2896 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2897 | xfs_fileoff_t nexto; /* next file offset */ |
| 2898 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2899 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2900 | xfs_extlen_t align_alen; /* temp for length */ |
| 2901 | xfs_extlen_t temp; /* temp for calculations */ |
| 2902 | |
| 2903 | if (convert) |
| 2904 | return 0; |
| 2905 | |
| 2906 | orig_off = align_off = *offp; |
| 2907 | orig_alen = align_alen = *lenp; |
| 2908 | orig_end = orig_off + orig_alen; |
| 2909 | |
| 2910 | /* |
| 2911 | * If this request overlaps an existing extent, then don't |
| 2912 | * attempt to perform any additional alignment. |
| 2913 | */ |
| 2914 | if (!delay && !eof && |
| 2915 | (orig_off >= gotp->br_startoff) && |
| 2916 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2917 | return 0; |
| 2918 | } |
| 2919 | |
| 2920 | /* |
| 2921 | * If the file offset is unaligned vs. the extent size |
| 2922 | * we need to align it. This will be possible unless |
| 2923 | * the file was previously written with a kernel that didn't |
| 2924 | * perform this alignment, or if a truncate shot us in the |
| 2925 | * foot. |
| 2926 | */ |
| 2927 | temp = do_mod(orig_off, extsz); |
| 2928 | if (temp) { |
| 2929 | align_alen += temp; |
| 2930 | align_off -= temp; |
| 2931 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2932 | |
| 2933 | /* Same adjustment for the end of the requested area. */ |
| 2934 | temp = (align_alen % extsz); |
| 2935 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2936 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2937 | |
| 2938 | /* |
| 2939 | * For large extent hint sizes, the aligned extent might be larger than |
| 2940 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 2941 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 2942 | * short allocation just fine, so it is safe to do this. We only want to |
| 2943 | * do it when we are forced to, though, because it means more allocation |
| 2944 | * operations are required. |
| 2945 | */ |
| 2946 | while (align_alen > MAXEXTLEN) |
| 2947 | align_alen -= extsz; |
| 2948 | ASSERT(align_alen <= MAXEXTLEN); |
| 2949 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2950 | /* |
| 2951 | * If the previous block overlaps with this proposed allocation |
| 2952 | * then move the start forward without adjusting the length. |
| 2953 | */ |
| 2954 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2955 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2956 | prevo = prevp->br_startoff; |
| 2957 | else |
| 2958 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2959 | } else |
| 2960 | prevo = 0; |
| 2961 | if (align_off != orig_off && align_off < prevo) |
| 2962 | align_off = prevo; |
| 2963 | /* |
| 2964 | * If the next block overlaps with this proposed allocation |
| 2965 | * then move the start back without adjusting the length, |
| 2966 | * but not before offset 0. |
| 2967 | * This may of course make the start overlap previous block, |
| 2968 | * and if we hit the offset 0 limit then the next block |
| 2969 | * can still overlap too. |
| 2970 | */ |
| 2971 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2972 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2973 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2974 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2975 | else |
| 2976 | nexto = gotp->br_startoff; |
| 2977 | } else |
| 2978 | nexto = NULLFILEOFF; |
| 2979 | if (!eof && |
| 2980 | align_off + align_alen != orig_end && |
| 2981 | align_off + align_alen > nexto) |
| 2982 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2983 | /* |
| 2984 | * If we're now overlapping the next or previous extent that |
| 2985 | * means we can't fit an extsz piece in this hole. Just move |
| 2986 | * the start forward to the first valid spot and set |
| 2987 | * the length so we hit the end. |
| 2988 | */ |
| 2989 | if (align_off != orig_off && align_off < prevo) |
| 2990 | align_off = prevo; |
| 2991 | if (align_off + align_alen != orig_end && |
| 2992 | align_off + align_alen > nexto && |
| 2993 | nexto != NULLFILEOFF) { |
| 2994 | ASSERT(nexto > prevo); |
| 2995 | align_alen = nexto - align_off; |
| 2996 | } |
| 2997 | |
| 2998 | /* |
| 2999 | * If realtime, and the result isn't a multiple of the realtime |
| 3000 | * extent size we need to remove blocks until it is. |
| 3001 | */ |
| 3002 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3003 | /* |
| 3004 | * We're not covering the original request, or |
| 3005 | * we won't be able to once we fix the length. |
| 3006 | */ |
| 3007 | if (orig_off < align_off || |
| 3008 | orig_end > align_off + align_alen || |
| 3009 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3010 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3011 | /* |
| 3012 | * Try to fix it by moving the start up. |
| 3013 | */ |
| 3014 | if (align_off + temp <= orig_off) { |
| 3015 | align_alen -= temp; |
| 3016 | align_off += temp; |
| 3017 | } |
| 3018 | /* |
| 3019 | * Try to fix it by moving the end in. |
| 3020 | */ |
| 3021 | else if (align_off + align_alen - temp >= orig_end) |
| 3022 | align_alen -= temp; |
| 3023 | /* |
| 3024 | * Set the start to the minimum then trim the length. |
| 3025 | */ |
| 3026 | else { |
| 3027 | align_alen -= orig_off - align_off; |
| 3028 | align_off = orig_off; |
| 3029 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3030 | } |
| 3031 | /* |
| 3032 | * Result doesn't cover the request, fail it. |
| 3033 | */ |
| 3034 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3035 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3036 | } else { |
| 3037 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3038 | /* see MAXEXTLEN handling above */ |
| 3039 | ASSERT(orig_end <= align_off + align_alen || |
| 3040 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | #ifdef DEBUG |
| 3044 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3045 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3046 | if (prevp->br_startoff != NULLFILEOFF) |
| 3047 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3048 | #endif |
| 3049 | |
| 3050 | *lenp = align_alen; |
| 3051 | *offp = align_off; |
| 3052 | return 0; |
| 3053 | } |
| 3054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3056 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3057 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3058 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3059 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | { |
| 3061 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3063 | xfs_mount_t *mp; /* mount point structure */ |
| 3064 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3065 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | |
| 3067 | #define ISVALID(x,y) \ |
| 3068 | (rt ? \ |
| 3069 | (x) < mp->m_sb.sb_rblocks : \ |
| 3070 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3071 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3072 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3075 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3076 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3077 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3078 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | /* |
| 3080 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3081 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3083 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3084 | !isnullstartblock(ap->prev.br_startblock) && |
| 3085 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3086 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3087 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | /* |
| 3089 | * Adjust for the gap between prevp and us. |
| 3090 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3091 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3092 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3094 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3095 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | } |
| 3097 | /* |
| 3098 | * If not at eof, then compare the two neighbor blocks. |
| 3099 | * Figure out whether either one gives us a good starting point, |
| 3100 | * and pick the better one. |
| 3101 | */ |
| 3102 | else if (!ap->eof) { |
| 3103 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3104 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3105 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3106 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3107 | |
| 3108 | /* |
| 3109 | * If there's a previous (left) block, select a requested |
| 3110 | * start block based on it. |
| 3111 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3112 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3113 | !isnullstartblock(ap->prev.br_startblock) && |
| 3114 | (prevbno = ap->prev.br_startblock + |
| 3115 | ap->prev.br_blockcount) && |
| 3116 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | /* |
| 3118 | * Calculate gap to end of previous block. |
| 3119 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3120 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3121 | (ap->prev.br_startoff + |
| 3122 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | /* |
| 3124 | * Figure the startblock based on the previous block's |
| 3125 | * end and the gap size. |
| 3126 | * Heuristic! |
| 3127 | * If the gap is large relative to the piece we're |
| 3128 | * allocating, or using it gives us an invalid block |
| 3129 | * number, then just use the end of the previous block. |
| 3130 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3131 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3133 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | prevbno += adjust; |
| 3135 | else |
| 3136 | prevdiff += adjust; |
| 3137 | /* |
| 3138 | * If the firstblock forbids it, can't use it, |
| 3139 | * must use default. |
| 3140 | */ |
| 3141 | if (!rt && !nullfb && |
| 3142 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3143 | prevbno = NULLFSBLOCK; |
| 3144 | } |
| 3145 | /* |
| 3146 | * No previous block or can't follow it, just default. |
| 3147 | */ |
| 3148 | else |
| 3149 | prevbno = NULLFSBLOCK; |
| 3150 | /* |
| 3151 | * If there's a following (right) block, select a requested |
| 3152 | * start block based on it. |
| 3153 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3154 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | /* |
| 3156 | * Calculate gap to start of next block. |
| 3157 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3158 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | /* |
| 3160 | * Figure the startblock based on the next block's |
| 3161 | * start and the gap size. |
| 3162 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3163 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | /* |
| 3165 | * Heuristic! |
| 3166 | * If the gap is large relative to the piece we're |
| 3167 | * allocating, or using it gives us an invalid block |
| 3168 | * number, then just use the start of the next block |
| 3169 | * offset by our length. |
| 3170 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3171 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3173 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3174 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3175 | gotbno -= ap->length; |
| 3176 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3177 | } else |
| 3178 | gotdiff += adjust; |
| 3179 | /* |
| 3180 | * If the firstblock forbids it, can't use it, |
| 3181 | * must use default. |
| 3182 | */ |
| 3183 | if (!rt && !nullfb && |
| 3184 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3185 | gotbno = NULLFSBLOCK; |
| 3186 | } |
| 3187 | /* |
| 3188 | * No next block, just default. |
| 3189 | */ |
| 3190 | else |
| 3191 | gotbno = NULLFSBLOCK; |
| 3192 | /* |
| 3193 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3194 | * 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] | 3195 | */ |
| 3196 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3197 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3199 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3201 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3203 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3204 | } |
| 3205 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3206 | static int |
| 3207 | xfs_bmap_longest_free_extent( |
| 3208 | struct xfs_trans *tp, |
| 3209 | xfs_agnumber_t ag, |
| 3210 | xfs_extlen_t *blen, |
| 3211 | int *notinit) |
| 3212 | { |
| 3213 | struct xfs_mount *mp = tp->t_mountp; |
| 3214 | struct xfs_perag *pag; |
| 3215 | xfs_extlen_t longest; |
| 3216 | int error = 0; |
| 3217 | |
| 3218 | pag = xfs_perag_get(mp, ag); |
| 3219 | if (!pag->pagf_init) { |
| 3220 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3221 | if (error) |
| 3222 | goto out; |
| 3223 | |
| 3224 | if (!pag->pagf_init) { |
| 3225 | *notinit = 1; |
| 3226 | goto out; |
| 3227 | } |
| 3228 | } |
| 3229 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 3230 | longest = xfs_alloc_longest_free_extent(mp, pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3231 | xfs_alloc_min_freelist(mp, pag), |
| 3232 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3233 | if (*blen < longest) |
| 3234 | *blen = longest; |
| 3235 | |
| 3236 | out: |
| 3237 | xfs_perag_put(pag); |
| 3238 | return error; |
| 3239 | } |
| 3240 | |
| 3241 | static void |
| 3242 | xfs_bmap_select_minlen( |
| 3243 | struct xfs_bmalloca *ap, |
| 3244 | struct xfs_alloc_arg *args, |
| 3245 | xfs_extlen_t *blen, |
| 3246 | int notinit) |
| 3247 | { |
| 3248 | if (notinit || *blen < ap->minlen) { |
| 3249 | /* |
| 3250 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3251 | * there is space for this request. |
| 3252 | */ |
| 3253 | args->minlen = ap->minlen; |
| 3254 | } else if (*blen < args->maxlen) { |
| 3255 | /* |
| 3256 | * If the best seen length is less than the request length, |
| 3257 | * use the best as the minimum. |
| 3258 | */ |
| 3259 | args->minlen = *blen; |
| 3260 | } else { |
| 3261 | /* |
| 3262 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3263 | * as the minimum. |
| 3264 | */ |
| 3265 | args->minlen = args->maxlen; |
| 3266 | } |
| 3267 | } |
| 3268 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3269 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3270 | xfs_bmap_btalloc_nullfb( |
| 3271 | struct xfs_bmalloca *ap, |
| 3272 | struct xfs_alloc_arg *args, |
| 3273 | xfs_extlen_t *blen) |
| 3274 | { |
| 3275 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3276 | xfs_agnumber_t ag, startag; |
| 3277 | int notinit = 0; |
| 3278 | int error; |
| 3279 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3280 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3281 | args->total = ap->total; |
| 3282 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3283 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3284 | if (startag == NULLAGNUMBER) |
| 3285 | startag = ag = 0; |
| 3286 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3287 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3288 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3289 | ¬init); |
| 3290 | if (error) |
| 3291 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3292 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3293 | if (++ag == mp->m_sb.sb_agcount) |
| 3294 | ag = 0; |
| 3295 | if (ag == startag) |
| 3296 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3297 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3298 | |
| 3299 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3300 | return 0; |
| 3301 | } |
| 3302 | |
| 3303 | STATIC int |
| 3304 | xfs_bmap_btalloc_filestreams( |
| 3305 | struct xfs_bmalloca *ap, |
| 3306 | struct xfs_alloc_arg *args, |
| 3307 | xfs_extlen_t *blen) |
| 3308 | { |
| 3309 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3310 | xfs_agnumber_t ag; |
| 3311 | int notinit = 0; |
| 3312 | int error; |
| 3313 | |
| 3314 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3315 | args->total = ap->total; |
| 3316 | |
| 3317 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3318 | if (ag == NULLAGNUMBER) |
| 3319 | ag = 0; |
| 3320 | |
| 3321 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3322 | if (error) |
| 3323 | return error; |
| 3324 | |
| 3325 | if (*blen < args->maxlen) { |
| 3326 | error = xfs_filestream_new_ag(ap, &ag); |
| 3327 | if (error) |
| 3328 | return error; |
| 3329 | |
| 3330 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3331 | ¬init); |
| 3332 | if (error) |
| 3333 | return error; |
| 3334 | |
| 3335 | } |
| 3336 | |
| 3337 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3338 | |
| 3339 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3340 | * Set the failure fallback case to look in the selected AG as stream |
| 3341 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3342 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3343 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3344 | return 0; |
| 3345 | } |
| 3346 | |
| 3347 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3348 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3349 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3350 | { |
| 3351 | xfs_mount_t *mp; /* mount point structure */ |
| 3352 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3353 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3354 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3355 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3356 | xfs_alloc_arg_t args; |
| 3357 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3358 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3359 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3360 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3361 | int tryagain; |
| 3362 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3363 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3364 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3365 | ASSERT(ap->length); |
| 3366 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3367 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3368 | |
| 3369 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3370 | stripe_align = 0; |
| 3371 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3372 | stripe_align = mp->m_swidth; |
| 3373 | else if (mp->m_dalign) |
| 3374 | stripe_align = mp->m_dalign; |
| 3375 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3376 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3377 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3378 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3379 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3380 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3381 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3382 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3383 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3384 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3385 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3386 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3387 | |
| 3388 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3389 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3390 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3391 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3392 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3393 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3394 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3395 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3396 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3397 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3398 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3399 | } |
| 3400 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3401 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3402 | |
| 3403 | xfs_bmap_adjacent(ap); |
| 3404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3406 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | * it's in the right allocation group. |
| 3408 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3409 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | ; |
| 3411 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3412 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3414 | * Normal allocation, done through xfs_alloc_vextent. |
| 3415 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3416 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3417 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3418 | args.tp = ap->tp; |
| 3419 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3420 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3421 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3422 | |
| 3423 | /* Trim the allocation back to the maximum an AG can fit. */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 3424 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3425 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3426 | blen = 0; |
| 3427 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3428 | /* |
| 3429 | * Search for an allocation group with a single extent large |
| 3430 | * enough for the request. If one isn't found, then adjust |
| 3431 | * the minimum allocation size to the largest space found. |
| 3432 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3433 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3434 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3435 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3436 | else |
| 3437 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3438 | if (error) |
| 3439 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3440 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3441 | if (xfs_inode_is_filestream(ap->ip)) |
| 3442 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3443 | else |
| 3444 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3445 | args.total = args.minlen = ap->minlen; |
| 3446 | } else { |
| 3447 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3448 | args.total = ap->total; |
| 3449 | args.minlen = ap->minlen; |
| 3450 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3451 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3452 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3453 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3454 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3455 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3456 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3457 | args.prod = 1; |
| 3458 | args.mod = 0; |
| 3459 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3460 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3461 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3462 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3463 | } |
| 3464 | /* |
| 3465 | * If we are not low on available data blocks, and the |
| 3466 | * underlying logical volume manager is a stripe, and |
| 3467 | * the file offset is zero then try to allocate data |
| 3468 | * blocks on stripe unit boundary. |
| 3469 | * NOTE: ap->aeof is only set if the allocation length |
| 3470 | * is >= the stripe unit and the allocation offset is |
| 3471 | * at the end of file. |
| 3472 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3473 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3474 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3475 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3476 | atype = args.type; |
| 3477 | isaligned = 1; |
| 3478 | /* |
| 3479 | * Adjust for alignment |
| 3480 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3481 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3482 | args.minlen = blen - args.alignment; |
| 3483 | args.minalignslop = 0; |
| 3484 | } else { |
| 3485 | /* |
| 3486 | * First try an exact bno allocation. |
| 3487 | * If it fails then do a near or start bno |
| 3488 | * allocation with alignment turned on. |
| 3489 | */ |
| 3490 | atype = args.type; |
| 3491 | tryagain = 1; |
| 3492 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3493 | args.alignment = 1; |
| 3494 | /* |
| 3495 | * Compute the minlen+alignment for the |
| 3496 | * next case. Set slop so that the value |
| 3497 | * of minlen+alignment+slop doesn't go up |
| 3498 | * between the calls. |
| 3499 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3500 | if (blen > stripe_align && blen <= args.maxlen) |
| 3501 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3502 | else |
| 3503 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3504 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3505 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3506 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3507 | args.minlen - 1; |
| 3508 | else |
| 3509 | args.minalignslop = 0; |
| 3510 | } |
| 3511 | } else { |
| 3512 | args.alignment = 1; |
| 3513 | args.minalignslop = 0; |
| 3514 | } |
| 3515 | args.minleft = ap->minleft; |
| 3516 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3517 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3518 | args.datatype = ap->datatype; |
| 3519 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3520 | args.ip = ap->ip; |
| 3521 | |
| 3522 | error = xfs_alloc_vextent(&args); |
| 3523 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3524 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3525 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3526 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3527 | /* |
| 3528 | * Exact allocation failed. Now try with alignment |
| 3529 | * turned on. |
| 3530 | */ |
| 3531 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3532 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3533 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3534 | args.minlen = nextminlen; |
| 3535 | args.minalignslop = 0; |
| 3536 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | if ((error = xfs_alloc_vextent(&args))) |
| 3538 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3539 | } |
| 3540 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3541 | /* |
| 3542 | * allocation failed, so turn off alignment and |
| 3543 | * try again. |
| 3544 | */ |
| 3545 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3546 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3547 | args.alignment = 0; |
| 3548 | if ((error = xfs_alloc_vextent(&args))) |
| 3549 | return error; |
| 3550 | } |
| 3551 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3552 | args.minlen > ap->minlen) { |
| 3553 | args.minlen = ap->minlen; |
| 3554 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3555 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3556 | if ((error = xfs_alloc_vextent(&args))) |
| 3557 | return error; |
| 3558 | } |
| 3559 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3560 | args.fsbno = 0; |
| 3561 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3562 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3563 | if ((error = xfs_alloc_vextent(&args))) |
| 3564 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3565 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3566 | } |
| 3567 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3568 | /* |
| 3569 | * check the allocation happened at the same or higher AG than |
| 3570 | * the first block that was allocated. |
| 3571 | */ |
| 3572 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3573 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) <= |
| 3574 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3575 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3576 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3577 | if (*ap->firstblock == NULLFSBLOCK) |
| 3578 | *ap->firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3579 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3580 | ap->length = args.len; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 3581 | if (!(ap->flags & XFS_BMAPI_COWFORK)) |
| 3582 | ap->ip->i_d.di_nblocks += args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3583 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3584 | if (ap->wasdel) |
| 3585 | ap->ip->i_delayed_blks -= args.len; |
| 3586 | /* |
| 3587 | * Adjust the disk quota also. This was reserved |
| 3588 | * earlier. |
| 3589 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3590 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3591 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3592 | XFS_TRANS_DQ_BCOUNT, |
| 3593 | (long) args.len); |
| 3594 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3595 | ap->blkno = NULLFSBLOCK; |
| 3596 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3597 | } |
| 3598 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | /* |
| 3602 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3603 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3604 | */ |
| 3605 | STATIC int |
| 3606 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3607 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3608 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3609 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3610 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3611 | return xfs_bmap_rtalloc(ap); |
| 3612 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | } |
| 3614 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3615 | /* Trim extent to fit a logical block range. */ |
| 3616 | void |
| 3617 | xfs_trim_extent( |
| 3618 | struct xfs_bmbt_irec *irec, |
| 3619 | xfs_fileoff_t bno, |
| 3620 | xfs_filblks_t len) |
| 3621 | { |
| 3622 | xfs_fileoff_t distance; |
| 3623 | xfs_fileoff_t end = bno + len; |
| 3624 | |
| 3625 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3626 | irec->br_startoff >= end) { |
| 3627 | irec->br_blockcount = 0; |
| 3628 | return; |
| 3629 | } |
| 3630 | |
| 3631 | if (irec->br_startoff < bno) { |
| 3632 | distance = bno - irec->br_startoff; |
| 3633 | if (isnullstartblock(irec->br_startblock)) |
| 3634 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3635 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3636 | irec->br_startblock != HOLESTARTBLOCK) |
| 3637 | irec->br_startblock += distance; |
| 3638 | irec->br_startoff += distance; |
| 3639 | irec->br_blockcount -= distance; |
| 3640 | } |
| 3641 | |
| 3642 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3643 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3644 | irec->br_blockcount -= distance; |
| 3645 | } |
| 3646 | } |
| 3647 | |
Brian Foster | 40214d1 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 3648 | /* trim extent to within eof */ |
| 3649 | void |
| 3650 | xfs_trim_extent_eof( |
| 3651 | struct xfs_bmbt_irec *irec, |
| 3652 | struct xfs_inode *ip) |
| 3653 | |
| 3654 | { |
| 3655 | xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount, |
| 3656 | i_size_read(VFS_I(ip)))); |
| 3657 | } |
| 3658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3659 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3660 | * Trim the returned map to the required bounds |
| 3661 | */ |
| 3662 | STATIC void |
| 3663 | xfs_bmapi_trim_map( |
| 3664 | struct xfs_bmbt_irec *mval, |
| 3665 | struct xfs_bmbt_irec *got, |
| 3666 | xfs_fileoff_t *bno, |
| 3667 | xfs_filblks_t len, |
| 3668 | xfs_fileoff_t obno, |
| 3669 | xfs_fileoff_t end, |
| 3670 | int n, |
| 3671 | int flags) |
| 3672 | { |
| 3673 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3674 | got->br_startoff + got->br_blockcount <= obno) { |
| 3675 | *mval = *got; |
| 3676 | if (isnullstartblock(got->br_startblock)) |
| 3677 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3678 | return; |
| 3679 | } |
| 3680 | |
| 3681 | if (obno > *bno) |
| 3682 | *bno = obno; |
| 3683 | ASSERT((*bno >= obno) || (n == 0)); |
| 3684 | ASSERT(*bno < end); |
| 3685 | mval->br_startoff = *bno; |
| 3686 | if (isnullstartblock(got->br_startblock)) |
| 3687 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3688 | else |
| 3689 | mval->br_startblock = got->br_startblock + |
| 3690 | (*bno - got->br_startoff); |
| 3691 | /* |
| 3692 | * Return the minimum of what we got and what we asked for for |
| 3693 | * the length. We can use the len variable here because it is |
| 3694 | * modified below and we could have been there before coming |
| 3695 | * here if the first part of the allocation didn't overlap what |
| 3696 | * was asked for. |
| 3697 | */ |
| 3698 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3699 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3700 | mval->br_state = got->br_state; |
| 3701 | ASSERT(mval->br_blockcount <= len); |
| 3702 | return; |
| 3703 | } |
| 3704 | |
| 3705 | /* |
| 3706 | * Update and validate the extent map to return |
| 3707 | */ |
| 3708 | STATIC void |
| 3709 | xfs_bmapi_update_map( |
| 3710 | struct xfs_bmbt_irec **map, |
| 3711 | xfs_fileoff_t *bno, |
| 3712 | xfs_filblks_t *len, |
| 3713 | xfs_fileoff_t obno, |
| 3714 | xfs_fileoff_t end, |
| 3715 | int *n, |
| 3716 | int flags) |
| 3717 | { |
| 3718 | xfs_bmbt_irec_t *mval = *map; |
| 3719 | |
| 3720 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3721 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3722 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3723 | (mval->br_startoff < obno)); |
| 3724 | |
| 3725 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3726 | *len = end - *bno; |
| 3727 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3728 | /* update previous map with new information */ |
| 3729 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3730 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3731 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3732 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3733 | mval[-1].br_state = mval->br_state; |
| 3734 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3735 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3736 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3737 | mval->br_startblock == mval[-1].br_startblock + |
| 3738 | mval[-1].br_blockcount && |
| 3739 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3740 | mval[-1].br_state == mval->br_state)) { |
| 3741 | ASSERT(mval->br_startoff == |
| 3742 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3743 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3744 | } else if (*n > 0 && |
| 3745 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3746 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3747 | mval->br_startoff == |
| 3748 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3749 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3750 | mval[-1].br_state = mval->br_state; |
| 3751 | } else if (!((*n == 0) && |
| 3752 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3753 | obno))) { |
| 3754 | mval++; |
| 3755 | (*n)++; |
| 3756 | } |
| 3757 | *map = mval; |
| 3758 | } |
| 3759 | |
| 3760 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3761 | * Map file blocks to filesystem blocks without allocation. |
| 3762 | */ |
| 3763 | int |
| 3764 | xfs_bmapi_read( |
| 3765 | struct xfs_inode *ip, |
| 3766 | xfs_fileoff_t bno, |
| 3767 | xfs_filblks_t len, |
| 3768 | struct xfs_bmbt_irec *mval, |
| 3769 | int *nmap, |
| 3770 | int flags) |
| 3771 | { |
| 3772 | struct xfs_mount *mp = ip->i_mount; |
| 3773 | struct xfs_ifork *ifp; |
| 3774 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3775 | xfs_fileoff_t obno; |
| 3776 | xfs_fileoff_t end; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3777 | xfs_extnum_t idx; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3778 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3779 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3780 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3781 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3782 | |
| 3783 | ASSERT(*nmap >= 1); |
| 3784 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3785 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3786 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3787 | |
| 3788 | if (unlikely(XFS_TEST_ERROR( |
| 3789 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3790 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 3791 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3792 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3793 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3797 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3798 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3799 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3800 | |
| 3801 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3802 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3803 | /* No CoW fork? Return a hole. */ |
| 3804 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 3805 | mval->br_startoff = bno; |
| 3806 | mval->br_startblock = HOLESTARTBLOCK; |
| 3807 | mval->br_blockcount = len; |
| 3808 | mval->br_state = XFS_EXT_NORM; |
| 3809 | *nmap = 1; |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3813 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3814 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3815 | if (error) |
| 3816 | return error; |
| 3817 | } |
| 3818 | |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3819 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) |
| 3820 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3821 | end = bno + len; |
| 3822 | obno = bno; |
| 3823 | |
| 3824 | while (bno < end && n < *nmap) { |
| 3825 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3826 | if (eof) |
| 3827 | got.br_startoff = end; |
| 3828 | if (got.br_startoff > bno) { |
| 3829 | /* Reading in a hole. */ |
| 3830 | mval->br_startoff = bno; |
| 3831 | mval->br_startblock = HOLESTARTBLOCK; |
| 3832 | mval->br_blockcount = |
| 3833 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3834 | mval->br_state = XFS_EXT_NORM; |
| 3835 | bno += mval->br_blockcount; |
| 3836 | len -= mval->br_blockcount; |
| 3837 | mval++; |
| 3838 | n++; |
| 3839 | continue; |
| 3840 | } |
| 3841 | |
| 3842 | /* set up the extent map to return. */ |
| 3843 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3844 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3845 | |
| 3846 | /* If we're done, stop now. */ |
| 3847 | if (bno >= end || n >= *nmap) |
| 3848 | break; |
| 3849 | |
| 3850 | /* Else go on to the next record. */ |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3851 | if (!xfs_iext_get_extent(ifp, ++idx, &got)) |
| 3852 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3853 | } |
| 3854 | *nmap = n; |
| 3855 | return 0; |
| 3856 | } |
| 3857 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3858 | /* |
| 3859 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3860 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3861 | * |
| 3862 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3863 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3864 | * to the extent record that was inserted to the inode fork. |
| 3865 | * |
| 3866 | * Note that the allocated extent may have been merged with contiguous extents |
| 3867 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3868 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3869 | * look up the updated record in the inode fork. |
| 3870 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3871 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3872 | xfs_bmapi_reserve_delalloc( |
| 3873 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3874 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3875 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3876 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3877 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3878 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3879 | xfs_extnum_t *lastx, |
| 3880 | int eof) |
| 3881 | { |
| 3882 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3883 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3884 | xfs_extlen_t alen; |
| 3885 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3886 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 3887 | xfs_extlen_t extsz; |
| 3888 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3889 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3890 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3891 | /* |
| 3892 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3893 | * tag the inode before we return. |
| 3894 | */ |
| 3895 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3896 | if (!eof) |
| 3897 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3898 | if (prealloc && alen >= len) |
| 3899 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3900 | |
| 3901 | /* Figure out the extent size, adjust alen */ |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3902 | if (whichfork == XFS_COW_FORK) |
| 3903 | extsz = xfs_get_cowextsz_hint(ip); |
| 3904 | else |
| 3905 | extsz = xfs_get_extsz_hint(ip); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3906 | if (extsz) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3907 | struct xfs_bmbt_irec prev; |
| 3908 | |
| 3909 | if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev)) |
| 3910 | prev.br_startoff = NULLFILEOFF; |
| 3911 | |
| 3912 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3913 | 1, 0, &aoff, &alen); |
| 3914 | ASSERT(!error); |
| 3915 | } |
| 3916 | |
| 3917 | if (rt) |
| 3918 | extsz = alen / mp->m_sb.sb_rextsize; |
| 3919 | |
| 3920 | /* |
| 3921 | * Make a transaction-less quota reservation for delayed allocation |
| 3922 | * blocks. This number gets adjusted later. We return if we haven't |
| 3923 | * allocated blocks already inside this loop. |
| 3924 | */ |
| 3925 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 3926 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 3927 | if (error) |
| 3928 | return error; |
| 3929 | |
| 3930 | /* |
| 3931 | * Split changing sb for alen and indlen since they could be coming |
| 3932 | * from different places. |
| 3933 | */ |
| 3934 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 3935 | ASSERT(indlen > 0); |
| 3936 | |
| 3937 | if (rt) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 3938 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3939 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3940 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | if (error) |
| 3944 | goto out_unreserve_quota; |
| 3945 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3946 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3947 | if (error) |
| 3948 | goto out_unreserve_blocks; |
| 3949 | |
| 3950 | |
| 3951 | ip->i_delayed_blks += alen; |
| 3952 | |
| 3953 | got->br_startoff = aoff; |
| 3954 | got->br_startblock = nullstartblock(indlen); |
| 3955 | got->br_blockcount = alen; |
| 3956 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3957 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3958 | xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3959 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3960 | /* |
| 3961 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 3962 | * preallocation can occur at the start or end of the extent, even when |
| 3963 | * prealloc == 0, so we must also check the aligned offset and length. |
| 3964 | */ |
| 3965 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 3966 | xfs_inode_set_eofblocks_tag(ip); |
| 3967 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 3968 | xfs_inode_set_cowblocks_tag(ip); |
| 3969 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3970 | return 0; |
| 3971 | |
| 3972 | out_unreserve_blocks: |
| 3973 | if (rt) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 3974 | xfs_mod_frextents(mp, extsz); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3975 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3976 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3977 | out_unreserve_quota: |
| 3978 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 3979 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3980 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 3981 | return error; |
| 3982 | } |
| 3983 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 3984 | static int |
| 3985 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 3986 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3987 | { |
| 3988 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 3989 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3990 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 3991 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3992 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3993 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3994 | ASSERT(bma->length > 0); |
| 3995 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3996 | /* |
| 3997 | * For the wasdelay case, we could also just allocate the stuff asked |
| 3998 | * for in this bmap call but that wouldn't be as good. |
| 3999 | */ |
| 4000 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4001 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4002 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 4003 | if (bma->idx) |
| 4004 | xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4005 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4006 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4007 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4008 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4009 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4013 | * Set the data type being allocated. For the data fork, the first data |
| 4014 | * in the file is treated differently to all other allocations. For the |
| 4015 | * attribute fork, we only need to ensure the allocated range is not on |
| 4016 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4017 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4018 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4019 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4020 | if (whichfork == XFS_DATA_FORK) { |
| 4021 | if (bma->offset == 0) |
| 4022 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4023 | else |
| 4024 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4025 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4026 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4027 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4030 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4031 | |
| 4032 | /* |
| 4033 | * Only want to do the alignment at the eof if it is userdata and |
| 4034 | * allocation length is larger than a stripe unit. |
| 4035 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4036 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4037 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4038 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4039 | if (error) |
| 4040 | return error; |
| 4041 | } |
| 4042 | |
| 4043 | error = xfs_bmap_alloc(bma); |
| 4044 | if (error) |
| 4045 | return error; |
| 4046 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4047 | if (bma->cur) |
| 4048 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4049 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4050 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4051 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4052 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4053 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4054 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4055 | } |
| 4056 | /* |
| 4057 | * Bump the number of extents we've allocated |
| 4058 | * in this call. |
| 4059 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4060 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4061 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4062 | if (bma->cur) |
| 4063 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4064 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4065 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4066 | bma->got.br_startoff = bma->offset; |
| 4067 | bma->got.br_startblock = bma->blkno; |
| 4068 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4069 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4070 | |
| 4071 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4072 | * In the data fork, a wasdelay extent has been initialized, so |
| 4073 | * shouldn't be flagged as unwritten. |
| 4074 | * |
| 4075 | * For the cow fork, however, we convert delalloc reservations |
| 4076 | * (extents allocated for speculative preallocation) to |
| 4077 | * allocated unwritten extents, and only convert the unwritten |
| 4078 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4079 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4080 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4081 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4082 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4083 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4084 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4085 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4086 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4087 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4088 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
| 4089 | whichfork, &bma->idx, &bma->cur, &bma->got, |
| 4090 | bma->firstblock, bma->dfops, &bma->logflags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4091 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4092 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4093 | if (error) |
| 4094 | return error; |
| 4095 | |
| 4096 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4097 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4098 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4099 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4100 | */ |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 4101 | xfs_iext_get_extent(ifp, bma->idx, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4102 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4103 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4104 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4105 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4106 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4107 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4108 | return 0; |
| 4109 | } |
| 4110 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4111 | STATIC int |
| 4112 | xfs_bmapi_convert_unwritten( |
| 4113 | struct xfs_bmalloca *bma, |
| 4114 | struct xfs_bmbt_irec *mval, |
| 4115 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4116 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4117 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4118 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4119 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4120 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4121 | int error; |
| 4122 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4123 | /* check if we need to do unwritten->real conversion */ |
| 4124 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4125 | (flags & XFS_BMAPI_PREALLOC)) |
| 4126 | return 0; |
| 4127 | |
| 4128 | /* check if we need to do real->unwritten conversion */ |
| 4129 | if (mval->br_state == XFS_EXT_NORM && |
| 4130 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4131 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4132 | return 0; |
| 4133 | |
| 4134 | /* |
| 4135 | * Modify (by adding) the state flag, if writing. |
| 4136 | */ |
| 4137 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4138 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4139 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4140 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4141 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4142 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4143 | } |
| 4144 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4145 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4146 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4147 | /* |
| 4148 | * Before insertion into the bmbt, zero the range being converted |
| 4149 | * if required. |
| 4150 | */ |
| 4151 | if (flags & XFS_BMAPI_ZERO) { |
| 4152 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4153 | mval->br_blockcount); |
| 4154 | if (error) |
| 4155 | return error; |
| 4156 | } |
| 4157 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4158 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
| 4159 | &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4160 | &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4161 | /* |
| 4162 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4163 | * path because the conversion might not have done so (e.g., if the |
| 4164 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4165 | * in the transaction for the sake of fsync(), even if nothing has |
| 4166 | * changed, because fsync() will not force the log for this transaction |
| 4167 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4168 | * |
| 4169 | * Note: If we're only converting cow fork extents, there aren't |
| 4170 | * 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] | 4171 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4172 | if (whichfork != XFS_COW_FORK) |
| 4173 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4174 | if (error) |
| 4175 | return error; |
| 4176 | |
| 4177 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4178 | * Update our extent pointer, given that |
| 4179 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4180 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4181 | */ |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 4182 | xfs_iext_get_extent(ifp, bma->idx, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4183 | |
| 4184 | /* |
| 4185 | * We may have combined previously unwritten space with written space, |
| 4186 | * so generate another request. |
| 4187 | */ |
| 4188 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4189 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4190 | return 0; |
| 4191 | } |
| 4192 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4193 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4194 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4195 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4196 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4197 | * locking problems. |
| 4198 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | * The returned value in "firstblock" from the first call in a transaction |
| 4200 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4201 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4202 | * the first call in "total"; if no allocation group has that many free |
| 4203 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4204 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4205 | int |
| 4206 | xfs_bmapi_write( |
| 4207 | struct xfs_trans *tp, /* transaction pointer */ |
| 4208 | struct xfs_inode *ip, /* incore inode */ |
| 4209 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4210 | xfs_filblks_t len, /* length to map in file */ |
| 4211 | int flags, /* XFS_BMAPI_... */ |
| 4212 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4213 | controls a.g. for allocs */ |
| 4214 | xfs_extlen_t total, /* total blocks needed */ |
| 4215 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4216 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4217 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4218 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4219 | struct xfs_mount *mp = ip->i_mount; |
| 4220 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4221 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4222 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4223 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4224 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4225 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4226 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4227 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4230 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4231 | int orig_flags; /* original flags arg value */ |
| 4232 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4233 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4234 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | |
| 4236 | orig_bno = bno; |
| 4237 | orig_len = len; |
| 4238 | orig_flags = flags; |
| 4239 | orig_mval = mval; |
| 4240 | orig_nmap = *nmap; |
| 4241 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4242 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4245 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4246 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4247 | ASSERT(tp != NULL || |
| 4248 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4249 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4250 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4251 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4252 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4253 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4254 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4255 | /* zeroing is for currently only for data extents, not metadata */ |
| 4256 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4257 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4258 | /* |
| 4259 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4260 | * but it makes no sense to do both at once. This would result in |
| 4261 | * zeroing the unwritten extent twice, but it still being an |
| 4262 | * unwritten extent.... |
| 4263 | */ |
| 4264 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4265 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | if (unlikely(XFS_TEST_ERROR( |
| 4268 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4269 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4270 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4271 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4272 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | } |
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 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4276 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4279 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4280 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4281 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4282 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4284 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4286 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4287 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4288 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4292 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4293 | if (error) |
| 4294 | goto error0; |
| 4295 | } |
| 4296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | n = 0; |
| 4298 | end = bno + len; |
| 4299 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4300 | |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4301 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got)) |
| 4302 | eof = true; |
| 4303 | if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev)) |
| 4304 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4305 | bma.tp = tp; |
| 4306 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4307 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4308 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4309 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4310 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4313 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4314 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4315 | /* in hole or beyoned EOF? */ |
| 4316 | if (eof || bma.got.br_startoff > bno) { |
| 4317 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4318 | /* |
| 4319 | * For the COW fork we can reasonably get a |
| 4320 | * request for converting an extent that races |
| 4321 | * with other threads already having converted |
| 4322 | * part of it, as there converting COW to |
| 4323 | * regular blocks is not protected using the |
| 4324 | * IOLOCK. |
| 4325 | */ |
| 4326 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4327 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4328 | error = -EIO; |
| 4329 | goto error0; |
| 4330 | } |
| 4331 | |
| 4332 | if (eof || bno >= end) |
| 4333 | break; |
| 4334 | } else { |
| 4335 | need_alloc = true; |
| 4336 | } |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4337 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4338 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4339 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4340 | |
| 4341 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | * First, deal with the hole before the allocated space |
| 4343 | * that we found, if any. |
| 4344 | */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4345 | if (need_alloc || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4346 | bma.eof = eof; |
| 4347 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4348 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4349 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4350 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4351 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4352 | /* |
| 4353 | * There's a 32/64 bit type mismatch between the |
| 4354 | * allocation length request (which can be 64 bits in |
| 4355 | * length) and the bma length request, which is |
| 4356 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4357 | * check for 32-bit overflows and handle them here. |
| 4358 | */ |
| 4359 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4360 | bma.length = MAXEXTLEN; |
| 4361 | else |
| 4362 | bma.length = len; |
| 4363 | |
| 4364 | ASSERT(len > 0); |
| 4365 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4366 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | if (error) |
| 4368 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4369 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4370 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4371 | |
| 4372 | /* |
| 4373 | * If this is a CoW allocation, record the data in |
| 4374 | * the refcount btree for orphan recovery. |
| 4375 | */ |
| 4376 | if (whichfork == XFS_COW_FORK) { |
| 4377 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4378 | bma.blkno, bma.length); |
| 4379 | if (error) |
| 4380 | goto error0; |
| 4381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4382 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4383 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4384 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4385 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4386 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4388 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4389 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4390 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4391 | continue; |
| 4392 | if (error) |
| 4393 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4395 | /* update the extent map to return */ |
| 4396 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | /* |
| 4399 | * If we're done, stop now. Stop when we've allocated |
| 4400 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4401 | * the transaction may get too big. |
| 4402 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4403 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4405 | |
| 4406 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4407 | bma.prev = bma.got; |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4408 | if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got)) |
| 4409 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | /* |
| 4414 | * Transform from btree to extents, give it cur. |
| 4415 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4416 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4417 | int tmp_logflags = 0; |
| 4418 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4419 | ASSERT(bma.cur); |
| 4420 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4422 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | if (error) |
| 4424 | goto error0; |
| 4425 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4428 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4429 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4431 | error0: |
| 4432 | /* |
| 4433 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4434 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4436 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4438 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4439 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4441 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | /* |
| 4443 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4444 | * detecting a case where the data is changed, there's an error, |
| 4445 | * and it's not logged so we don't shutdown when we should. |
| 4446 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4447 | if (bma.logflags) |
| 4448 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4449 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4450 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | if (!error) { |
| 4452 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4453 | XFS_FSB_TO_AGNO(mp, *firstblock) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | XFS_FSB_TO_AGNO(mp, |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4455 | bma.cur->bc_private.b.firstblock)); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4456 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4458 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4459 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4460 | } |
| 4461 | if (!error) |
| 4462 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4463 | orig_nmap, *nmap); |
| 4464 | return error; |
| 4465 | } |
| 4466 | |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4467 | static int |
| 4468 | xfs_bmapi_remap( |
| 4469 | struct xfs_trans *tp, |
| 4470 | struct xfs_inode *ip, |
| 4471 | xfs_fileoff_t bno, |
| 4472 | xfs_filblks_t len, |
| 4473 | xfs_fsblock_t startblock, |
| 4474 | struct xfs_defer_ops *dfops) |
| 4475 | { |
| 4476 | struct xfs_mount *mp = ip->i_mount; |
| 4477 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4478 | struct xfs_btree_cur *cur = NULL; |
| 4479 | xfs_fsblock_t firstblock = NULLFSBLOCK; |
| 4480 | struct xfs_bmbt_irec got; |
| 4481 | xfs_extnum_t idx; |
| 4482 | int logflags = 0, error; |
| 4483 | |
| 4484 | ASSERT(len > 0); |
| 4485 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4486 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 4487 | |
| 4488 | if (unlikely(XFS_TEST_ERROR( |
| 4489 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4490 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4491 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4492 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4493 | return -EFSCORRUPTED; |
| 4494 | } |
| 4495 | |
| 4496 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4497 | return -EIO; |
| 4498 | |
| 4499 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4500 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4501 | if (error) |
| 4502 | return error; |
| 4503 | } |
| 4504 | |
| 4505 | if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) { |
| 4506 | /* make sure we only reflink into a hole. */ |
| 4507 | ASSERT(got.br_startoff > bno); |
| 4508 | ASSERT(got.br_startoff - bno >= len); |
| 4509 | } |
| 4510 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4511 | ip->i_d.di_nblocks += len; |
| 4512 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4513 | |
| 4514 | if (ifp->if_flags & XFS_IFBROOT) { |
| 4515 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
| 4516 | cur->bc_private.b.firstblock = firstblock; |
| 4517 | cur->bc_private.b.dfops = dfops; |
| 4518 | cur->bc_private.b.flags = 0; |
| 4519 | } |
| 4520 | |
| 4521 | got.br_startoff = bno; |
| 4522 | got.br_startblock = startblock; |
| 4523 | got.br_blockcount = len; |
| 4524 | got.br_state = XFS_EXT_NORM; |
| 4525 | |
| 4526 | error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur, |
| 4527 | &got, &firstblock, dfops, &logflags); |
| 4528 | if (error) |
| 4529 | goto error0; |
| 4530 | |
| 4531 | if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) { |
| 4532 | int tmp_logflags = 0; |
| 4533 | |
| 4534 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4535 | &tmp_logflags, XFS_DATA_FORK); |
| 4536 | logflags |= tmp_logflags; |
| 4537 | } |
| 4538 | |
| 4539 | error0: |
| 4540 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4541 | logflags &= ~XFS_ILOG_DEXT; |
| 4542 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4543 | logflags &= ~XFS_ILOG_DBROOT; |
| 4544 | |
| 4545 | if (logflags) |
| 4546 | xfs_trans_log_inode(tp, ip, logflags); |
| 4547 | if (cur) { |
| 4548 | xfs_btree_del_cursor(cur, |
| 4549 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4550 | } |
| 4551 | return error; |
| 4552 | } |
| 4553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4555 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4556 | * indlen reservation must be shared across the two new extents that are left |
| 4557 | * behind. |
| 4558 | * |
| 4559 | * Given the original reservation and the worst case indlen for the two new |
| 4560 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4561 | * reservation fairly across the two new extents. If necessary, steal available |
| 4562 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4563 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4564 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4565 | */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4566 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4567 | xfs_bmap_split_indlen( |
| 4568 | xfs_filblks_t ores, /* original res. */ |
| 4569 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4570 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4571 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4572 | { |
| 4573 | xfs_filblks_t len1 = *indlen1; |
| 4574 | xfs_filblks_t len2 = *indlen2; |
| 4575 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4576 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4577 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4578 | |
| 4579 | /* |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4580 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4581 | * indlen for both new extents. |
| 4582 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4583 | if (ores < nres && avail) |
| 4584 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4585 | ores += stolen; |
| 4586 | |
| 4587 | /* nothing else to do if we've satisfied the new reservation */ |
| 4588 | if (ores >= nres) |
| 4589 | return stolen; |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4590 | |
| 4591 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4592 | * We can't meet the total required reservation for the two extents. |
| 4593 | * Calculate the percent of the overall shortage between both extents |
| 4594 | * and apply this percentage to each of the requested indlen values. |
| 4595 | * This distributes the shortage fairly and reduces the chances that one |
| 4596 | * of the two extents is left with nothing when extents are repeatedly |
| 4597 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4598 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4599 | resfactor = (ores * 100); |
| 4600 | do_div(resfactor, nres); |
| 4601 | len1 *= resfactor; |
| 4602 | do_div(len1, 100); |
| 4603 | len2 *= resfactor; |
| 4604 | do_div(len2, 100); |
| 4605 | ASSERT(len1 + len2 <= ores); |
| 4606 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4607 | |
| 4608 | /* |
| 4609 | * Hand out the remainder to each extent. If one of the two reservations |
| 4610 | * is zero, we want to make sure that one gets a block first. The loop |
| 4611 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4612 | * is zero. |
| 4613 | */ |
| 4614 | ores -= (len1 + len2); |
| 4615 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4616 | if (ores && !len2 && *indlen2) { |
| 4617 | len2++; |
| 4618 | ores--; |
| 4619 | } |
| 4620 | while (ores) { |
| 4621 | if (len1 < *indlen1) { |
| 4622 | len1++; |
| 4623 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4624 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4625 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4626 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4627 | if (len2 < *indlen2) { |
| 4628 | len2++; |
| 4629 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4630 | } |
| 4631 | } |
| 4632 | |
| 4633 | *indlen1 = len1; |
| 4634 | *indlen2 = len2; |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4635 | |
| 4636 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4637 | } |
| 4638 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4639 | int |
| 4640 | xfs_bmap_del_extent_delay( |
| 4641 | struct xfs_inode *ip, |
| 4642 | int whichfork, |
| 4643 | xfs_extnum_t *idx, |
| 4644 | struct xfs_bmbt_irec *got, |
| 4645 | struct xfs_bmbt_irec *del) |
| 4646 | { |
| 4647 | struct xfs_mount *mp = ip->i_mount; |
| 4648 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4649 | struct xfs_bmbt_irec new; |
| 4650 | int64_t da_old, da_new, da_diff = 0; |
| 4651 | xfs_fileoff_t del_endoff, got_endoff; |
| 4652 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4653 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4654 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4655 | bool isrt; |
| 4656 | |
| 4657 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4658 | |
| 4659 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4660 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4661 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4662 | da_old = startblockval(got->br_startblock); |
| 4663 | da_new = 0; |
| 4664 | |
| 4665 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 4666 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4667 | ASSERT(del->br_blockcount > 0); |
| 4668 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4669 | ASSERT(got_endoff >= del_endoff); |
| 4670 | |
| 4671 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4672 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4673 | |
| 4674 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4675 | xfs_mod_frextents(mp, rtexts); |
| 4676 | } |
| 4677 | |
| 4678 | /* |
| 4679 | * Update the inode delalloc counter now and wait to update the |
| 4680 | * sb counters as we might have to borrow some blocks for the |
| 4681 | * indirect block accounting. |
| 4682 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4683 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4684 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4685 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4686 | if (error) |
| 4687 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4688 | ip->i_delayed_blks -= del->br_blockcount; |
| 4689 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4690 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4691 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4692 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4693 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4694 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4695 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4696 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4697 | /* |
| 4698 | * Matches the whole extent. Delete the entry. |
| 4699 | */ |
| 4700 | xfs_iext_remove(ip, *idx, 1, state); |
| 4701 | --*idx; |
| 4702 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4703 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4704 | /* |
| 4705 | * Deleting the first part of the extent. |
| 4706 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4707 | got->br_startoff = del_endoff; |
| 4708 | got->br_blockcount -= del->br_blockcount; |
| 4709 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4710 | got->br_blockcount), da_old); |
| 4711 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4712 | xfs_iext_update_extent(ip, state, *idx, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4713 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4714 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4715 | /* |
| 4716 | * Deleting the last part of the extent. |
| 4717 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4718 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4719 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4720 | got->br_blockcount), da_old); |
| 4721 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4722 | xfs_iext_update_extent(ip, state, *idx, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4723 | break; |
| 4724 | case 0: |
| 4725 | /* |
| 4726 | * Deleting the middle of the extent. |
| 4727 | * |
| 4728 | * Distribute the original indlen reservation across the two new |
| 4729 | * extents. Steal blocks from the deleted extent if necessary. |
| 4730 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4731 | * Warn if either of the new indlen reservations is zero as this |
| 4732 | * can lead to delalloc problems. |
| 4733 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4734 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4735 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4736 | |
| 4737 | new.br_blockcount = got_endoff - del_endoff; |
| 4738 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4739 | |
| 4740 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4741 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4742 | del->br_blockcount); |
| 4743 | |
| 4744 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4745 | xfs_iext_update_extent(ip, state, *idx, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4746 | |
| 4747 | new.br_startoff = del_endoff; |
| 4748 | new.br_state = got->br_state; |
| 4749 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4750 | |
| 4751 | ++*idx; |
| 4752 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 4753 | |
| 4754 | da_new = got_indlen + new_indlen - stolen; |
| 4755 | del->br_blockcount -= stolen; |
| 4756 | break; |
| 4757 | } |
| 4758 | |
| 4759 | ASSERT(da_old >= da_new); |
| 4760 | da_diff = da_old - da_new; |
| 4761 | if (!isrt) |
| 4762 | da_diff += del->br_blockcount; |
| 4763 | if (da_diff) |
| 4764 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4765 | return error; |
| 4766 | } |
| 4767 | |
| 4768 | void |
| 4769 | xfs_bmap_del_extent_cow( |
| 4770 | struct xfs_inode *ip, |
| 4771 | xfs_extnum_t *idx, |
| 4772 | struct xfs_bmbt_irec *got, |
| 4773 | struct xfs_bmbt_irec *del) |
| 4774 | { |
| 4775 | struct xfs_mount *mp = ip->i_mount; |
| 4776 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4777 | struct xfs_bmbt_irec new; |
| 4778 | xfs_fileoff_t del_endoff, got_endoff; |
| 4779 | int state = BMAP_COWFORK; |
| 4780 | |
| 4781 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4782 | |
| 4783 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4784 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4785 | |
| 4786 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 4787 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4788 | ASSERT(del->br_blockcount > 0); |
| 4789 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4790 | ASSERT(got_endoff >= del_endoff); |
| 4791 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4792 | |
| 4793 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4794 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4795 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4796 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4797 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4798 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4799 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4800 | /* |
| 4801 | * Matches the whole extent. Delete the entry. |
| 4802 | */ |
| 4803 | xfs_iext_remove(ip, *idx, 1, state); |
| 4804 | --*idx; |
| 4805 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4806 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4807 | /* |
| 4808 | * Deleting the first part of the extent. |
| 4809 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4810 | got->br_startoff = del_endoff; |
| 4811 | got->br_blockcount -= del->br_blockcount; |
| 4812 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4813 | xfs_iext_update_extent(ip, state, *idx, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4814 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4815 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4816 | /* |
| 4817 | * Deleting the last part of the extent. |
| 4818 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4819 | got->br_blockcount -= del->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4820 | xfs_iext_update_extent(ip, state, *idx, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4821 | break; |
| 4822 | case 0: |
| 4823 | /* |
| 4824 | * Deleting the middle of the extent. |
| 4825 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4826 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4827 | xfs_iext_update_extent(ip, state, *idx, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4828 | |
| 4829 | new.br_startoff = del_endoff; |
| 4830 | new.br_blockcount = got_endoff - del_endoff; |
| 4831 | new.br_state = got->br_state; |
| 4832 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4833 | |
| 4834 | ++*idx; |
| 4835 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 4836 | break; |
| 4837 | } |
| 4838 | } |
| 4839 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4840 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4841 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4842 | * after removing space. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4843 | */ |
| 4844 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4845 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4846 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4847 | xfs_trans_t *tp, /* current transaction pointer */ |
| 4848 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4849 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4850 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4851 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4852 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4853 | int whichfork, /* data or attr fork */ |
| 4854 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4855 | { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4856 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4857 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4858 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4859 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4860 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4861 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4862 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4863 | int i; /* temp state */ |
| 4864 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4865 | xfs_mount_t *mp; /* mount structure */ |
| 4866 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4867 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4868 | /* REFERENCED */ |
| 4869 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4870 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4871 | struct xfs_bmbt_irec old; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4872 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4873 | mp = ip->i_mount; |
| 4874 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4875 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4876 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 4877 | ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp))); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4878 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4879 | xfs_iext_get_extent(ifp, *idx, &got); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4880 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4881 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4882 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4883 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4884 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4885 | qfield = 0; |
| 4886 | error = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4887 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4888 | /* |
| 4889 | * If it's the case where the directory code is running with no block |
| 4890 | * reservation, and the deleted block is in the middle of its extent, |
| 4891 | * and the resulting insert of an extent would cause transformation to |
| 4892 | * btree format, then reject it. The calling code will then swap blocks |
| 4893 | * around instead. We have to do this now, rather than waiting for the |
| 4894 | * conversion to btree format, since the transaction will be dirty then. |
| 4895 | */ |
| 4896 | if (tp->t_blk_res == 0 && |
| 4897 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 4898 | XFS_IFORK_NEXTENTS(ip, whichfork) >= |
| 4899 | XFS_IFORK_MAXEXT(ip, whichfork) && |
| 4900 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 4901 | return -ENOSPC; |
| 4902 | |
| 4903 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4904 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4905 | xfs_fsblock_t bno; |
| 4906 | xfs_filblks_t len; |
| 4907 | |
| 4908 | ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0); |
| 4909 | ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0); |
| 4910 | bno = del->br_startblock; |
| 4911 | len = del->br_blockcount; |
| 4912 | do_div(bno, mp->m_sb.sb_rextsize); |
| 4913 | do_div(len, mp->m_sb.sb_rextsize); |
| 4914 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4915 | if (error) |
| 4916 | goto done; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4917 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4918 | nblks = len * mp->m_sb.sb_rextsize; |
| 4919 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4920 | } else { |
| 4921 | do_fx = 1; |
| 4922 | nblks = del->br_blockcount; |
| 4923 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4924 | } |
| 4925 | |
| 4926 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4927 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4928 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4929 | if (error) |
| 4930 | goto done; |
| 4931 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4932 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 4933 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4934 | if (got.br_startoff == del->br_startoff) |
| 4935 | state |= BMAP_LEFT_FILLING; |
| 4936 | if (got_endoff == del_endoff) |
| 4937 | state |= BMAP_RIGHT_FILLING; |
| 4938 | |
| 4939 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4940 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4941 | /* |
| 4942 | * Matches the whole extent. Delete the entry. |
| 4943 | */ |
Christoph Hellwig | 35e62da | 2017-10-19 11:04:43 -0700 | [diff] [blame] | 4944 | xfs_iext_remove(ip, *idx, 1, state); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4945 | --*idx; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4946 | |
| 4947 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4948 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 4949 | flags |= XFS_ILOG_CORE; |
| 4950 | if (!cur) { |
| 4951 | flags |= xfs_ilog_fext(whichfork); |
| 4952 | break; |
| 4953 | } |
| 4954 | if ((error = xfs_btree_delete(cur, &i))) |
| 4955 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4956 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4957 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4958 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4959 | /* |
| 4960 | * Deleting the first part of the extent. |
| 4961 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4962 | got.br_startoff = del_endoff; |
| 4963 | got.br_startblock = del_endblock; |
| 4964 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4965 | xfs_iext_update_extent(ip, state, *idx, &got); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4966 | if (!cur) { |
| 4967 | flags |= xfs_ilog_fext(whichfork); |
| 4968 | break; |
| 4969 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4970 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4971 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4972 | goto done; |
| 4973 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4974 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4975 | /* |
| 4976 | * Deleting the last part of the extent. |
| 4977 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4978 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4979 | xfs_iext_update_extent(ip, state, *idx, &got); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4980 | if (!cur) { |
| 4981 | flags |= xfs_ilog_fext(whichfork); |
| 4982 | break; |
| 4983 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4984 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4985 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4986 | goto done; |
| 4987 | break; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4988 | case 0: |
| 4989 | /* |
| 4990 | * Deleting the middle of the extent. |
| 4991 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4992 | old = got; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4993 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4994 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4995 | xfs_iext_update_extent(ip, state, *idx, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4996 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4997 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4998 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4999 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5000 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5001 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5002 | flags |= XFS_ILOG_CORE; |
| 5003 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5004 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5005 | if (error) |
| 5006 | goto done; |
| 5007 | error = xfs_btree_increment(cur, 0, &i); |
| 5008 | if (error) |
| 5009 | goto done; |
| 5010 | cur->bc_rec.b = new; |
| 5011 | error = xfs_btree_insert(cur, &i); |
| 5012 | if (error && error != -ENOSPC) |
| 5013 | goto done; |
| 5014 | /* |
| 5015 | * If get no-space back from btree insert, it tried a |
| 5016 | * split, and we have a zero block reservation. Fix up |
| 5017 | * our state and return the error. |
| 5018 | */ |
| 5019 | if (error == -ENOSPC) { |
| 5020 | /* |
| 5021 | * Reset the cursor, don't trust it after any |
| 5022 | * insert operation. |
| 5023 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5024 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5025 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5026 | goto done; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5027 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5028 | /* |
| 5029 | * Update the btree record back |
| 5030 | * to the original value. |
| 5031 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5032 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5033 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5034 | goto done; |
| 5035 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5036 | * Reset the extent record back |
| 5037 | * to the original value. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5038 | */ |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5039 | xfs_iext_update_extent(ip, state, *idx, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5040 | flags = 0; |
| 5041 | error = -ENOSPC; |
| 5042 | goto done; |
| 5043 | } |
| 5044 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5045 | } else |
| 5046 | flags |= xfs_ilog_fext(whichfork); |
| 5047 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5048 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5049 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 5050 | ++*idx; |
| 5051 | break; |
| 5052 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5053 | |
| 5054 | /* remove reverse mapping */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5055 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5056 | if (error) |
| 5057 | goto done; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5058 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5059 | /* |
| 5060 | * If we need to, add to list of extents to delete. |
| 5061 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5062 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5063 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5064 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5065 | if (error) |
| 5066 | goto done; |
| 5067 | } else |
| 5068 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5069 | del->br_blockcount, NULL); |
| 5070 | } |
| 5071 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5072 | /* |
| 5073 | * Adjust inode # blocks in the file. |
| 5074 | */ |
| 5075 | if (nblks) |
| 5076 | ip->i_d.di_nblocks -= nblks; |
| 5077 | /* |
| 5078 | * Adjust quota data. |
| 5079 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5080 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5081 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5082 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5083 | done: |
| 5084 | *logflagsp = flags; |
| 5085 | return error; |
| 5086 | } |
| 5087 | |
| 5088 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | * Unmap (remove) blocks from a file. |
| 5090 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5091 | * that value. If not all extents in the block range can be removed then |
| 5092 | * *done is set. |
| 5093 | */ |
| 5094 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5095 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | xfs_trans_t *tp, /* transaction pointer */ |
| 5097 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5098 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5099 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | int flags, /* misc flags */ |
| 5101 | xfs_extnum_t nexts, /* number of extents max */ |
| 5102 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5103 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5104 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | { |
| 5106 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5107 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | int error; /* error return value */ |
| 5109 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5110 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5112 | int isrt; /* freeing in rt area */ |
| 5113 | xfs_extnum_t lastx; /* last extent index used */ |
| 5114 | int logflags; /* transaction logging flags */ |
| 5115 | xfs_extlen_t mod; /* rt extent offset */ |
| 5116 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | int tmp_logflags; /* partial logging flags */ |
| 5118 | int wasdel; /* was a delayed alloc extent */ |
| 5119 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5121 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5122 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5123 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5124 | xfs_fileoff_t end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5126 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5127 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5128 | whichfork = xfs_bmapi_whichfork(flags); |
| 5129 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5131 | if (unlikely( |
| 5132 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5133 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5134 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5135 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5136 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | } |
| 5138 | mp = ip->i_mount; |
| 5139 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5140 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5141 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5142 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | ASSERT(len > 0); |
| 5144 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5145 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5146 | /* |
| 5147 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5148 | * blowing out the transaction with a mix of EFIs and reflink |
| 5149 | * adjustments. |
| 5150 | */ |
| 5151 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
| 5152 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5153 | else |
| 5154 | max_len = len; |
| 5155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5157 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5158 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5159 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5160 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | return 0; |
| 5162 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5163 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5164 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5165 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5166 | |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5167 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &lastx, &got)) { |
| 5168 | *rlen = 0; |
| 5169 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5171 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | logflags = 0; |
| 5174 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5175 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5176 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5178 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | cur->bc_private.b.flags = 0; |
| 5180 | } else |
| 5181 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5182 | |
| 5183 | if (isrt) { |
| 5184 | /* |
| 5185 | * Synchronize by locking the bitmap inode. |
| 5186 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5187 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5188 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5189 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5190 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5191 | } |
| 5192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | extno = 0; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5194 | while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5195 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5197 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | * Just back up to the previous extent, if so. |
| 5199 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5200 | if (got.br_startoff > end) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | if (--lastx < 0) |
| 5202 | break; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5203 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | } |
| 5205 | /* |
| 5206 | * Is the last block of this extent before the range |
| 5207 | * we're supposed to delete? If so, we're done. |
| 5208 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5209 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5210 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5211 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | break; |
| 5213 | /* |
| 5214 | * Then deal with the (possibly delayed) allocated space |
| 5215 | * we found. |
| 5216 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5218 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5219 | |
| 5220 | /* |
| 5221 | * Make sure we don't touch multiple AGF headers out of order |
| 5222 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5223 | */ |
| 5224 | if (!wasdel) { |
| 5225 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5226 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5227 | break; |
| 5228 | prev_agno = agno; |
| 5229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | if (got.br_startoff < start) { |
| 5231 | del.br_startoff = start; |
| 5232 | del.br_blockcount -= start - got.br_startoff; |
| 5233 | if (!wasdel) |
| 5234 | del.br_startblock += start - got.br_startoff; |
| 5235 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5236 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5237 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5238 | |
| 5239 | /* How much can we safely unmap? */ |
| 5240 | if (max_len < del.br_blockcount) { |
| 5241 | del.br_startoff += del.br_blockcount - max_len; |
| 5242 | if (!wasdel) |
| 5243 | del.br_startblock += del.br_blockcount - max_len; |
| 5244 | del.br_blockcount = max_len; |
| 5245 | } |
| 5246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | sum = del.br_startblock + del.br_blockcount; |
| 5248 | if (isrt && |
| 5249 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5250 | /* |
| 5251 | * Realtime extent not lined up at the end. |
| 5252 | * The extent could have been split into written |
| 5253 | * and unwritten pieces, or we could just be |
| 5254 | * unmapping part of it. But we can't really |
| 5255 | * get rid of part of a realtime extent. |
| 5256 | */ |
| 5257 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5258 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | /* |
| 5260 | * This piece is unwritten, or we're not |
| 5261 | * using unwritten extents. Skip over it. |
| 5262 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5263 | ASSERT(end >= mod); |
| 5264 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | del.br_blockcount : mod; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5266 | if (end < got.br_startoff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | if (--lastx >= 0) |
Christoph Hellwig | e3f0f75 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5268 | xfs_iext_get_extent(ifp, lastx, |
| 5269 | &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | } |
| 5271 | continue; |
| 5272 | } |
| 5273 | /* |
| 5274 | * It's written, turn it unwritten. |
| 5275 | * This is better than zeroing it. |
| 5276 | */ |
| 5277 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5278 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | /* |
| 5280 | * If this spans a realtime extent boundary, |
| 5281 | * chop it back to the start of the one we end at. |
| 5282 | */ |
| 5283 | if (del.br_blockcount > mod) { |
| 5284 | del.br_startoff += del.br_blockcount - mod; |
| 5285 | del.br_startblock += del.br_blockcount - mod; |
| 5286 | del.br_blockcount = mod; |
| 5287 | } |
| 5288 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5289 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5290 | whichfork, &lastx, &cur, &del, |
| 5291 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | if (error) |
| 5293 | goto error0; |
| 5294 | goto nodelete; |
| 5295 | } |
| 5296 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5297 | /* |
| 5298 | * Realtime extent is lined up at the end but not |
| 5299 | * at the front. We'll get rid of full extents if |
| 5300 | * we can. |
| 5301 | */ |
| 5302 | mod = mp->m_sb.sb_rextsize - mod; |
| 5303 | if (del.br_blockcount > mod) { |
| 5304 | del.br_blockcount -= mod; |
| 5305 | del.br_startoff += mod; |
| 5306 | del.br_startblock += mod; |
| 5307 | } else if ((del.br_startoff == start && |
| 5308 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5309 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5310 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | /* |
| 5312 | * Can't make it unwritten. There isn't |
| 5313 | * a full extent here so just skip it. |
| 5314 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5315 | ASSERT(end >= del.br_blockcount); |
| 5316 | end -= del.br_blockcount; |
| 5317 | if (got.br_startoff > end && --lastx >= 0) |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5318 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | continue; |
| 5320 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5321 | struct xfs_bmbt_irec prev; |
| 5322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | /* |
| 5324 | * This one is already unwritten. |
| 5325 | * It must have a written left neighbor. |
| 5326 | * Unwrite the killed part of that one and |
| 5327 | * try again. |
| 5328 | */ |
| 5329 | ASSERT(lastx > 0); |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5330 | xfs_iext_get_extent(ifp, lastx - 1, &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5332 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | ASSERT(del.br_startblock == |
| 5334 | prev.br_startblock + prev.br_blockcount); |
| 5335 | if (prev.br_startoff < start) { |
| 5336 | mod = start - prev.br_startoff; |
| 5337 | prev.br_blockcount -= mod; |
| 5338 | prev.br_startblock += mod; |
| 5339 | prev.br_startoff = start; |
| 5340 | } |
| 5341 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5342 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5343 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5344 | ip, whichfork, &lastx, &cur, |
| 5345 | &prev, firstblock, dfops, |
| 5346 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | if (error) |
| 5348 | goto error0; |
| 5349 | goto nodelete; |
| 5350 | } else { |
| 5351 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5352 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5353 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5354 | ip, whichfork, &lastx, &cur, |
| 5355 | &del, firstblock, dfops, |
| 5356 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | if (error) |
| 5358 | goto error0; |
| 5359 | goto nodelete; |
| 5360 | } |
| 5361 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5362 | |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5363 | if (wasdel) { |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5364 | error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx, |
| 5365 | &got, &del); |
| 5366 | } else { |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5367 | error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops, |
| 5368 | cur, &del, &tmp_logflags, whichfork, |
| 5369 | flags); |
| 5370 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5371 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | if (error) |
| 5374 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5375 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5376 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5377 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5378 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | /* |
| 5380 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5382 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5383 | if (lastx >= 0) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5384 | xfs_iext_get_extent(ifp, lastx, &got); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5385 | if (got.br_startoff > end && --lastx >= 0) |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5386 | xfs_iext_get_extent(ifp, lastx, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | extno++; |
| 5389 | } |
| 5390 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5391 | if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5392 | *rlen = 0; |
| 5393 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5394 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | /* |
| 5397 | * Convert to a btree if necessary. |
| 5398 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5399 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5401 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | &cur, 0, &tmp_logflags, whichfork); |
| 5403 | logflags |= tmp_logflags; |
| 5404 | if (error) |
| 5405 | goto error0; |
| 5406 | } |
| 5407 | /* |
| 5408 | * transform from btree to extents, give it cur |
| 5409 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5410 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | ASSERT(cur != NULL); |
| 5412 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5413 | whichfork); |
| 5414 | logflags |= tmp_logflags; |
| 5415 | if (error) |
| 5416 | goto error0; |
| 5417 | } |
| 5418 | /* |
| 5419 | * transform from extents to local? |
| 5420 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | error = 0; |
| 5422 | error0: |
| 5423 | /* |
| 5424 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5425 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5427 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5428 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5429 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5430 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5432 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5433 | /* |
| 5434 | * Log inode even in the error case, if the transaction |
| 5435 | * is dirty we'll need to shut down the filesystem. |
| 5436 | */ |
| 5437 | if (logflags) |
| 5438 | xfs_trans_log_inode(tp, ip, logflags); |
| 5439 | if (cur) { |
| 5440 | if (!error) { |
| 5441 | *firstblock = cur->bc_private.b.firstblock; |
| 5442 | cur->bc_private.b.allocated = 0; |
| 5443 | } |
| 5444 | xfs_btree_del_cursor(cur, |
| 5445 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5446 | } |
| 5447 | return error; |
| 5448 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5449 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5450 | /* Unmap a range of a file. */ |
| 5451 | int |
| 5452 | xfs_bunmapi( |
| 5453 | xfs_trans_t *tp, |
| 5454 | struct xfs_inode *ip, |
| 5455 | xfs_fileoff_t bno, |
| 5456 | xfs_filblks_t len, |
| 5457 | int flags, |
| 5458 | xfs_extnum_t nexts, |
| 5459 | xfs_fsblock_t *firstblock, |
| 5460 | struct xfs_defer_ops *dfops, |
| 5461 | int *done) |
| 5462 | { |
| 5463 | int error; |
| 5464 | |
| 5465 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5466 | dfops); |
| 5467 | *done = (len == 0); |
| 5468 | return error; |
| 5469 | } |
| 5470 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5471 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5472 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5473 | * extent that precedes the target hole of the shift. |
| 5474 | */ |
| 5475 | STATIC bool |
| 5476 | xfs_bmse_can_merge( |
| 5477 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5478 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5479 | xfs_fileoff_t shift) /* shift fsb */ |
| 5480 | { |
| 5481 | xfs_fileoff_t startoff; |
| 5482 | |
| 5483 | startoff = got->br_startoff - shift; |
| 5484 | |
| 5485 | /* |
| 5486 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5487 | * the preceding extent. |
| 5488 | */ |
| 5489 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5490 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5491 | (left->br_state != got->br_state) || |
| 5492 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5493 | return false; |
| 5494 | |
| 5495 | return true; |
| 5496 | } |
| 5497 | |
| 5498 | /* |
| 5499 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5500 | * hole in the file. If an extent shift would result in the extent being fully |
| 5501 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5502 | * the preceding extent rather than do the shift. |
| 5503 | * |
| 5504 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5505 | * with the provided extents via xfs_bmse_can_merge(). |
| 5506 | */ |
| 5507 | STATIC int |
| 5508 | xfs_bmse_merge( |
| 5509 | struct xfs_inode *ip, |
| 5510 | int whichfork, |
| 5511 | xfs_fileoff_t shift, /* shift fsb */ |
| 5512 | int current_ext, /* idx of gotp */ |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5513 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5514 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5515 | struct xfs_btree_cur *cur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5516 | int *logflags, /* output */ |
| 5517 | struct xfs_defer_ops *dfops) |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5518 | { |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5519 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5520 | xfs_filblks_t blockcount; |
| 5521 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5522 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5523 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5524 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5525 | |
| 5526 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5527 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5528 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5529 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5530 | new = *left; |
| 5531 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5532 | |
| 5533 | /* |
| 5534 | * Update the on-disk extent count, the btree if necessary and log the |
| 5535 | * inode. |
| 5536 | */ |
| 5537 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5538 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5539 | *logflags |= XFS_ILOG_CORE; |
| 5540 | if (!cur) { |
| 5541 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5542 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5543 | } |
| 5544 | |
| 5545 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5546 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5547 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5548 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5549 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5550 | |
| 5551 | error = xfs_btree_delete(cur, &i); |
| 5552 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5553 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5554 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5555 | |
| 5556 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5557 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5558 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5559 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5560 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5561 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5562 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5563 | if (error) |
| 5564 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5565 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5566 | done: |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5567 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), |
| 5568 | current_ext - 1, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5569 | xfs_iext_remove(ip, current_ext, 1, 0); |
| 5570 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5571 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5572 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got); |
| 5573 | if (error) |
| 5574 | return error; |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5575 | memcpy(&new, got, sizeof(new)); |
| 5576 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5577 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5578 | } |
| 5579 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5580 | static int |
| 5581 | xfs_bmap_shift_update_extent( |
| 5582 | struct xfs_inode *ip, |
| 5583 | int whichfork, |
| 5584 | xfs_extnum_t idx, |
| 5585 | struct xfs_bmbt_irec *got, |
| 5586 | struct xfs_btree_cur *cur, |
| 5587 | int *logflags, |
| 5588 | struct xfs_defer_ops *dfops, |
| 5589 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5590 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5591 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5592 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5593 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5594 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5595 | *logflags |= XFS_ILOG_CORE; |
| 5596 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5597 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5598 | |
| 5599 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5600 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5601 | if (error) |
| 5602 | return error; |
| 5603 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
| 5604 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5605 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5606 | if (error) |
| 5607 | return error; |
| 5608 | } else { |
| 5609 | *logflags |= XFS_ILOG_DEXT; |
| 5610 | } |
| 5611 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5612 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), idx, got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5613 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5614 | /* update reverse mapping */ |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5615 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5616 | if (error) |
| 5617 | return error; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5618 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5619 | } |
| 5620 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5621 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5622 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5623 | struct xfs_trans *tp, |
| 5624 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5625 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5626 | xfs_fileoff_t offset_shift_fsb, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5627 | bool *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5628 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5629 | xfs_fsblock_t *firstblock, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5630 | struct xfs_defer_ops *dfops) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5631 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5632 | int whichfork = XFS_DATA_FORK; |
| 5633 | struct xfs_mount *mp = ip->i_mount; |
| 5634 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5635 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5636 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5637 | xfs_extnum_t current_ext; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5638 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5639 | int error = 0; |
| 5640 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5641 | |
| 5642 | if (unlikely(XFS_TEST_ERROR( |
| 5643 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5644 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5645 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5646 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5647 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5648 | } |
| 5649 | |
| 5650 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5651 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5652 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5653 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5654 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5655 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5656 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5657 | if (error) |
| 5658 | return error; |
| 5659 | } |
| 5660 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5661 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5662 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5663 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5664 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5665 | cur->bc_private.b.flags = 0; |
| 5666 | } |
| 5667 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5668 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, ¤t_ext, &got)) { |
| 5669 | *done = true; |
| 5670 | goto del_cursor; |
| 5671 | } |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5672 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5673 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5674 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5675 | if (xfs_iext_get_extent(ifp, current_ext - 1, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5676 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5677 | error = -EINVAL; |
| 5678 | goto del_cursor; |
| 5679 | } |
| 5680 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5681 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
| 5682 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
| 5683 | current_ext, &got, &prev, cur, |
| 5684 | &logflags, dfops); |
| 5685 | if (error) |
| 5686 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5687 | |
| 5688 | /* update got after merge */ |
| 5689 | if (!xfs_iext_get_extent(ifp, current_ext, &got)) { |
| 5690 | *done = true; |
| 5691 | goto del_cursor; |
| 5692 | } |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5693 | goto done; |
| 5694 | } |
| 5695 | } else { |
| 5696 | if (got.br_startoff < offset_shift_fsb) { |
| 5697 | error = -EINVAL; |
| 5698 | goto del_cursor; |
| 5699 | } |
| 5700 | } |
| 5701 | |
| 5702 | error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got, |
| 5703 | cur, &logflags, dfops, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5704 | if (error) |
| 5705 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5706 | |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5707 | if (!xfs_iext_get_extent(ifp, ++current_ext, &got)) { |
| 5708 | *done = true; |
| 5709 | goto del_cursor; |
| 5710 | } |
| 5711 | |
| 5712 | done: |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5713 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5714 | del_cursor: |
| 5715 | if (cur) |
| 5716 | xfs_btree_del_cursor(cur, |
| 5717 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5718 | if (logflags) |
| 5719 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5720 | return error; |
| 5721 | } |
| 5722 | |
| 5723 | int |
| 5724 | xfs_bmap_insert_extents( |
| 5725 | struct xfs_trans *tp, |
| 5726 | struct xfs_inode *ip, |
| 5727 | xfs_fileoff_t *next_fsb, |
| 5728 | xfs_fileoff_t offset_shift_fsb, |
| 5729 | bool *done, |
| 5730 | xfs_fileoff_t stop_fsb, |
| 5731 | xfs_fsblock_t *firstblock, |
| 5732 | struct xfs_defer_ops *dfops) |
| 5733 | { |
| 5734 | int whichfork = XFS_DATA_FORK; |
| 5735 | struct xfs_mount *mp = ip->i_mount; |
| 5736 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5737 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5738 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5739 | xfs_extnum_t current_ext; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5740 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5741 | int error = 0; |
| 5742 | int logflags = 0; |
| 5743 | |
| 5744 | if (unlikely(XFS_TEST_ERROR( |
| 5745 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5746 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5747 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
| 5748 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 5749 | return -EFSCORRUPTED; |
| 5750 | } |
| 5751 | |
| 5752 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5753 | return -EIO; |
| 5754 | |
| 5755 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5756 | |
| 5757 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5758 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5759 | if (error) |
| 5760 | return error; |
| 5761 | } |
| 5762 | |
| 5763 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5764 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5765 | cur->bc_private.b.firstblock = *firstblock; |
| 5766 | cur->bc_private.b.dfops = dfops; |
| 5767 | cur->bc_private.b.flags = 0; |
| 5768 | } |
| 5769 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5770 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5771 | current_ext = xfs_iext_count(ifp) - 1; |
| 5772 | if (!xfs_iext_get_extent(ifp, current_ext, &got) || |
| 5773 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5774 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5775 | goto del_cursor; |
| 5776 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5777 | } else { |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5778 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, ¤t_ext, |
| 5779 | &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5780 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5781 | goto del_cursor; |
| 5782 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5783 | } |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5784 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5785 | |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5786 | if (stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5787 | error = -EIO; |
| 5788 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5789 | } |
| 5790 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5791 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5792 | if (xfs_iext_get_extent(ifp, current_ext + 1, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5793 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5794 | error = -EINVAL; |
| 5795 | goto del_cursor; |
| 5796 | } |
| 5797 | |
| 5798 | /* |
| 5799 | * Unlike a left shift (which involves a hole punch), a right |
| 5800 | * shift does not modify extent neighbors in any way. We should |
| 5801 | * never find mergeable extents in this scenario. Check anyways |
| 5802 | * and warn if we encounter two extents that could be one. |
| 5803 | */ |
| 5804 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5805 | WARN_ON_ONCE(1); |
| 5806 | } |
| 5807 | |
| 5808 | error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got, |
| 5809 | cur, &logflags, dfops, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5810 | if (error) |
| 5811 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5812 | |
| 5813 | if (!xfs_iext_get_extent(ifp, --current_ext, &got) || |
| 5814 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5815 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5816 | goto del_cursor; |
| 5817 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5818 | |
| 5819 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5820 | del_cursor: |
| 5821 | if (cur) |
| 5822 | xfs_btree_del_cursor(cur, |
| 5823 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5824 | if (logflags) |
| 5825 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5826 | return error; |
| 5827 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5828 | |
| 5829 | /* |
| 5830 | * Splits an extent into two extents at split_fsb block such that it is |
| 5831 | * the first block of the current_ext. @current_ext is a target extent |
| 5832 | * to be split. @split_fsb is a block where the extents is split. |
| 5833 | * If split_fsb lies in a hole or the first block of extents, just return 0. |
| 5834 | */ |
| 5835 | STATIC int |
| 5836 | xfs_bmap_split_extent_at( |
| 5837 | struct xfs_trans *tp, |
| 5838 | struct xfs_inode *ip, |
| 5839 | xfs_fileoff_t split_fsb, |
| 5840 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5841 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5842 | { |
| 5843 | int whichfork = XFS_DATA_FORK; |
| 5844 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5845 | struct xfs_bmbt_irec got; |
| 5846 | struct xfs_bmbt_irec new; /* split extent */ |
| 5847 | struct xfs_mount *mp = ip->i_mount; |
| 5848 | struct xfs_ifork *ifp; |
| 5849 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
| 5850 | xfs_extnum_t current_ext; |
| 5851 | int error = 0; |
| 5852 | int logflags = 0; |
| 5853 | int i = 0; |
| 5854 | |
| 5855 | if (unlikely(XFS_TEST_ERROR( |
| 5856 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5857 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5858 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5859 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5860 | XFS_ERRLEVEL_LOW, mp); |
| 5861 | return -EFSCORRUPTED; |
| 5862 | } |
| 5863 | |
| 5864 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5865 | return -EIO; |
| 5866 | |
| 5867 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5868 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5869 | /* Read in all the extents */ |
| 5870 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5871 | if (error) |
| 5872 | return error; |
| 5873 | } |
| 5874 | |
| 5875 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5876 | * 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] | 5877 | */ |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5878 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, ¤t_ext, &got) || |
| 5879 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5880 | return 0; |
| 5881 | |
| 5882 | gotblkcnt = split_fsb - got.br_startoff; |
| 5883 | new.br_startoff = split_fsb; |
| 5884 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5885 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5886 | new.br_state = got.br_state; |
| 5887 | |
| 5888 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5889 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5890 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5891 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5892 | cur->bc_private.b.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5893 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5894 | if (error) |
| 5895 | goto del_cursor; |
| 5896 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5897 | } |
| 5898 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5899 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5900 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), |
| 5901 | current_ext, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5902 | |
| 5903 | logflags = XFS_ILOG_CORE; |
| 5904 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5905 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5906 | if (error) |
| 5907 | goto del_cursor; |
| 5908 | } else |
| 5909 | logflags |= XFS_ILOG_DEXT; |
| 5910 | |
| 5911 | /* Add new extent */ |
| 5912 | current_ext++; |
| 5913 | xfs_iext_insert(ip, current_ext, 1, &new, 0); |
| 5914 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5915 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5916 | |
| 5917 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5918 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5919 | if (error) |
| 5920 | goto del_cursor; |
| 5921 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5922 | error = xfs_btree_insert(cur, &i); |
| 5923 | if (error) |
| 5924 | goto del_cursor; |
| 5925 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5926 | } |
| 5927 | |
| 5928 | /* |
| 5929 | * Convert to a btree if necessary. |
| 5930 | */ |
| 5931 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 5932 | int tmp_logflags; /* partial log flag return val */ |
| 5933 | |
| 5934 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5935 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5936 | &cur, 0, &tmp_logflags, whichfork); |
| 5937 | logflags |= tmp_logflags; |
| 5938 | } |
| 5939 | |
| 5940 | del_cursor: |
| 5941 | if (cur) { |
| 5942 | cur->bc_private.b.allocated = 0; |
| 5943 | xfs_btree_del_cursor(cur, |
| 5944 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5945 | } |
| 5946 | |
| 5947 | if (logflags) |
| 5948 | xfs_trans_log_inode(tp, ip, logflags); |
| 5949 | return error; |
| 5950 | } |
| 5951 | |
| 5952 | int |
| 5953 | xfs_bmap_split_extent( |
| 5954 | struct xfs_inode *ip, |
| 5955 | xfs_fileoff_t split_fsb) |
| 5956 | { |
| 5957 | struct xfs_mount *mp = ip->i_mount; |
| 5958 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5959 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5960 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5961 | int error; |
| 5962 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 5963 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 5964 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 5965 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5966 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5967 | |
| 5968 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 5969 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 5970 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5971 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5972 | |
| 5973 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5974 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5975 | if (error) |
| 5976 | goto out; |
| 5977 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 5978 | error = xfs_defer_finish(&tp, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5979 | if (error) |
| 5980 | goto out; |
| 5981 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 5982 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5983 | |
| 5984 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5985 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 5986 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5987 | return error; |
| 5988 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 5989 | |
| 5990 | /* Deferred mapping is only for real extents in the data fork. */ |
| 5991 | static bool |
| 5992 | xfs_bmap_is_update_needed( |
| 5993 | struct xfs_bmbt_irec *bmap) |
| 5994 | { |
| 5995 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 5996 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 5997 | } |
| 5998 | |
| 5999 | /* Record a bmap intent. */ |
| 6000 | static int |
| 6001 | __xfs_bmap_add( |
| 6002 | struct xfs_mount *mp, |
| 6003 | struct xfs_defer_ops *dfops, |
| 6004 | enum xfs_bmap_intent_type type, |
| 6005 | struct xfs_inode *ip, |
| 6006 | int whichfork, |
| 6007 | struct xfs_bmbt_irec *bmap) |
| 6008 | { |
| 6009 | int error; |
| 6010 | struct xfs_bmap_intent *bi; |
| 6011 | |
| 6012 | trace_xfs_bmap_defer(mp, |
| 6013 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6014 | type, |
| 6015 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6016 | ip->i_ino, whichfork, |
| 6017 | bmap->br_startoff, |
| 6018 | bmap->br_blockcount, |
| 6019 | bmap->br_state); |
| 6020 | |
| 6021 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6022 | INIT_LIST_HEAD(&bi->bi_list); |
| 6023 | bi->bi_type = type; |
| 6024 | bi->bi_owner = ip; |
| 6025 | bi->bi_whichfork = whichfork; |
| 6026 | bi->bi_bmap = *bmap; |
| 6027 | |
Christoph Hellwig | 882d878 | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 6028 | error = xfs_defer_ijoin(dfops, bi->bi_owner); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6029 | if (error) { |
| 6030 | kmem_free(bi); |
| 6031 | return error; |
| 6032 | } |
| 6033 | |
| 6034 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6035 | return 0; |
| 6036 | } |
| 6037 | |
| 6038 | /* Map an extent into a file. */ |
| 6039 | int |
| 6040 | xfs_bmap_map_extent( |
| 6041 | struct xfs_mount *mp, |
| 6042 | struct xfs_defer_ops *dfops, |
| 6043 | struct xfs_inode *ip, |
| 6044 | struct xfs_bmbt_irec *PREV) |
| 6045 | { |
| 6046 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6047 | return 0; |
| 6048 | |
| 6049 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6050 | XFS_DATA_FORK, PREV); |
| 6051 | } |
| 6052 | |
| 6053 | /* Unmap an extent out of a file. */ |
| 6054 | int |
| 6055 | xfs_bmap_unmap_extent( |
| 6056 | struct xfs_mount *mp, |
| 6057 | struct xfs_defer_ops *dfops, |
| 6058 | struct xfs_inode *ip, |
| 6059 | struct xfs_bmbt_irec *PREV) |
| 6060 | { |
| 6061 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6062 | return 0; |
| 6063 | |
| 6064 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6065 | XFS_DATA_FORK, PREV); |
| 6066 | } |
| 6067 | |
| 6068 | /* |
| 6069 | * Process one of the deferred bmap operations. We pass back the |
| 6070 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6071 | */ |
| 6072 | int |
| 6073 | xfs_bmap_finish_one( |
| 6074 | struct xfs_trans *tp, |
| 6075 | struct xfs_defer_ops *dfops, |
| 6076 | struct xfs_inode *ip, |
| 6077 | enum xfs_bmap_intent_type type, |
| 6078 | int whichfork, |
| 6079 | xfs_fileoff_t startoff, |
| 6080 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6081 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6082 | xfs_exntst_t state) |
| 6083 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6084 | xfs_fsblock_t firstfsb; |
| 6085 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6086 | |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6087 | /* |
| 6088 | * firstfsb is tied to the transaction lifetime and is used to |
| 6089 | * ensure correct AG locking order and schedule work item |
| 6090 | * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us |
| 6091 | * to only making one bmap call per transaction, so it should |
| 6092 | * be safe to have it as a local variable here. |
| 6093 | */ |
| 6094 | firstfsb = NULLFSBLOCK; |
| 6095 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6096 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6097 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6098 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6099 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6100 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6101 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6102 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6103 | |
| 6104 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6105 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6106 | return -EIO; |
| 6107 | |
| 6108 | switch (type) { |
| 6109 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6110 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6111 | startblock, dfops); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6112 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6113 | break; |
| 6114 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6115 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
| 6116 | XFS_BMAPI_REMAP, 1, &firstfsb, dfops); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6117 | break; |
| 6118 | default: |
| 6119 | ASSERT(0); |
| 6120 | error = -EFSCORRUPTED; |
| 6121 | } |
| 6122 | |
| 6123 | return error; |
| 6124 | } |