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 | 70a9883c | 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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 53 | |
| 54 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 55 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | */ |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 59 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 60 | * in this filesystem. Done once, during mount. |
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 | void |
| 63 | xfs_bmap_compute_maxlevels( |
| 64 | xfs_mount_t *mp, /* file system mount structure */ |
| 65 | int whichfork) /* data or attr fork */ |
| 66 | { |
| 67 | int level; /* btree level */ |
| 68 | uint maxblocks; /* max blocks at this level */ |
| 69 | uint maxleafents; /* max leaf entries possible */ |
| 70 | int maxrootrecs; /* max records in root block */ |
| 71 | int minleafrecs; /* min records in leaf block */ |
| 72 | int minnoderecs; /* min records in node block */ |
| 73 | int sz; /* root block size */ |
| 74 | |
| 75 | /* |
| 76 | * The maximum number of extents in a file, hence the maximum |
| 77 | * number of leaf entries, is controlled by the type of di_nextents |
| 78 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 79 | * (a signed 16-bit number, xfs_aextnum_t). |
| 80 | * |
| 81 | * Note that we can no longer assume that if we are in ATTR1 that |
| 82 | * the fork offset of all the inodes will be |
| 83 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 84 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 85 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 86 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 87 | * of a minimum size available. |
| 88 | */ |
| 89 | if (whichfork == XFS_DATA_FORK) { |
| 90 | maxleafents = MAXEXTNUM; |
| 91 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 92 | } else { |
| 93 | maxleafents = MAXAEXTNUM; |
| 94 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 95 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 96 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 97 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 98 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 99 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 100 | for (level = 1; maxblocks > 1; level++) { |
| 101 | if (maxblocks <= maxrootrecs) |
| 102 | maxblocks = 1; |
| 103 | else |
| 104 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 105 | } |
| 106 | mp->m_bm_maxlevels[whichfork] = level; |
| 107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 109 | STATIC int /* error */ |
| 110 | xfs_bmbt_lookup_eq( |
| 111 | struct xfs_btree_cur *cur, |
| 112 | xfs_fileoff_t off, |
| 113 | xfs_fsblock_t bno, |
| 114 | xfs_filblks_t len, |
| 115 | int *stat) /* success/failure */ |
| 116 | { |
| 117 | cur->bc_rec.b.br_startoff = off; |
| 118 | cur->bc_rec.b.br_startblock = bno; |
| 119 | cur->bc_rec.b.br_blockcount = len; |
| 120 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 121 | } |
| 122 | |
| 123 | STATIC int /* error */ |
| 124 | xfs_bmbt_lookup_ge( |
| 125 | struct xfs_btree_cur *cur, |
| 126 | xfs_fileoff_t off, |
| 127 | xfs_fsblock_t bno, |
| 128 | xfs_filblks_t len, |
| 129 | int *stat) /* success/failure */ |
| 130 | { |
| 131 | cur->bc_rec.b.br_startoff = off; |
| 132 | cur->bc_rec.b.br_startblock = bno; |
| 133 | cur->bc_rec.b.br_blockcount = len; |
| 134 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 135 | } |
| 136 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 137 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 138 | * Check if the inode needs to be converted to btree format. |
| 139 | */ |
| 140 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 141 | { |
| 142 | return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 143 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 144 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Check if the inode should be converted to extent format. |
| 149 | */ |
| 150 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 151 | { |
| 152 | return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 153 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 154 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Update the record referred to by cur to the value given |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 159 | * by [off, bno, len, state]. |
| 160 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 161 | */ |
| 162 | STATIC int |
| 163 | xfs_bmbt_update( |
| 164 | struct xfs_btree_cur *cur, |
| 165 | xfs_fileoff_t off, |
| 166 | xfs_fsblock_t bno, |
| 167 | xfs_filblks_t len, |
| 168 | xfs_exntst_t state) |
| 169 | { |
| 170 | union xfs_btree_rec rec; |
| 171 | |
| 172 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 173 | return xfs_btree_update(cur, &rec); |
| 174 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 177 | * Compute the worst-case number of indirect blocks that will be used |
| 178 | * for ip's delayed extent of length "len". |
| 179 | */ |
| 180 | STATIC xfs_filblks_t |
| 181 | xfs_bmap_worst_indlen( |
| 182 | xfs_inode_t *ip, /* incore inode pointer */ |
| 183 | xfs_filblks_t len) /* delayed extent length */ |
| 184 | { |
| 185 | int level; /* btree level number */ |
| 186 | int maxrecs; /* maximum record count at this level */ |
| 187 | xfs_mount_t *mp; /* mount structure */ |
| 188 | xfs_filblks_t rval; /* return value */ |
| 189 | |
| 190 | mp = ip->i_mount; |
| 191 | maxrecs = mp->m_bmap_dmxr[0]; |
| 192 | for (level = 0, rval = 0; |
| 193 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 194 | level++) { |
| 195 | len += maxrecs - 1; |
| 196 | do_div(len, maxrecs); |
| 197 | rval += len; |
| 198 | if (len == 1) |
| 199 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 200 | level - 1; |
| 201 | if (level == 0) |
| 202 | maxrecs = mp->m_bmap_dmxr[1]; |
| 203 | } |
| 204 | return rval; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Calculate the default attribute fork offset for newly created inodes. |
| 209 | */ |
| 210 | uint |
| 211 | xfs_default_attroffset( |
| 212 | struct xfs_inode *ip) |
| 213 | { |
| 214 | struct xfs_mount *mp = ip->i_mount; |
| 215 | uint offset; |
| 216 | |
| 217 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 218 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 219 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 220 | } else { |
| 221 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 222 | } |
| 223 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 224 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 225 | return offset; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Helper routine to reset inode di_forkoff field when switching |
| 230 | * attribute fork from local to extent format - we reset it where |
| 231 | * possible to make space available for inline data fork extents. |
| 232 | */ |
| 233 | STATIC void |
| 234 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 235 | xfs_inode_t *ip, |
| 236 | int whichfork) |
| 237 | { |
| 238 | if (whichfork == XFS_ATTR_FORK && |
| 239 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 240 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 241 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 242 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 243 | |
| 244 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 245 | ip->i_d.di_forkoff = dfl_forkoff; |
| 246 | } |
| 247 | } |
| 248 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 249 | #ifdef DEBUG |
| 250 | STATIC struct xfs_buf * |
| 251 | xfs_bmap_get_bp( |
| 252 | struct xfs_btree_cur *cur, |
| 253 | xfs_fsblock_t bno) |
| 254 | { |
| 255 | struct xfs_log_item_desc *lidp; |
| 256 | int i; |
| 257 | |
| 258 | if (!cur) |
| 259 | return NULL; |
| 260 | |
| 261 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 262 | if (!cur->bc_bufs[i]) |
| 263 | break; |
| 264 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 265 | return cur->bc_bufs[i]; |
| 266 | } |
| 267 | |
| 268 | /* Chase down all the log items to see if the bp is there */ |
| 269 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 270 | struct xfs_buf_log_item *bip; |
| 271 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 272 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 273 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 274 | return bip->bli_buf; |
| 275 | } |
| 276 | |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | STATIC void |
| 281 | xfs_check_block( |
| 282 | struct xfs_btree_block *block, |
| 283 | xfs_mount_t *mp, |
| 284 | int root, |
| 285 | short sz) |
| 286 | { |
| 287 | int i, j, dmxr; |
| 288 | __be64 *pp, *thispa; /* pointer to block address */ |
| 289 | xfs_bmbt_key_t *prevp, *keyp; |
| 290 | |
| 291 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 292 | |
| 293 | prevp = NULL; |
| 294 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 295 | dmxr = mp->m_bmap_dmxr[0]; |
| 296 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 297 | |
| 298 | if (prevp) { |
| 299 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 300 | be64_to_cpu(keyp->br_startoff)); |
| 301 | } |
| 302 | prevp = keyp; |
| 303 | |
| 304 | /* |
| 305 | * Compare the block numbers to see if there are dups. |
| 306 | */ |
| 307 | if (root) |
| 308 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 309 | else |
| 310 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 311 | |
| 312 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 313 | if (root) |
| 314 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 315 | else |
| 316 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 317 | if (*thispa == *pp) { |
| 318 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 319 | __func__, j, i, |
| 320 | (unsigned long long)be64_to_cpu(*thispa)); |
| 321 | panic("%s: ptrs are equal in node\n", |
| 322 | __func__); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * 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] | 330 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 331 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 332 | * them. The btree record ordering checks will still be done, so for such large |
| 333 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 334 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 335 | STATIC void |
| 336 | xfs_bmap_check_leaf_extents( |
| 337 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 338 | xfs_inode_t *ip, /* incore inode pointer */ |
| 339 | int whichfork) /* data or attr fork */ |
| 340 | { |
| 341 | struct xfs_btree_block *block; /* current btree block */ |
| 342 | xfs_fsblock_t bno; /* block # of "block" */ |
| 343 | xfs_buf_t *bp; /* buffer for "block" */ |
| 344 | int error; /* error return value */ |
| 345 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 346 | xfs_ifork_t *ifp; /* fork structure */ |
| 347 | int level; /* btree level, for checking */ |
| 348 | xfs_mount_t *mp; /* file system mount structure */ |
| 349 | __be64 *pp; /* pointer to block address */ |
| 350 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 351 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 352 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 353 | int bp_release = 0; |
| 354 | |
| 355 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 356 | return; |
| 357 | } |
| 358 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 359 | /* skip large extent count inodes */ |
| 360 | if (ip->i_d.di_nextents > 10000) |
| 361 | return; |
| 362 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 363 | bno = NULLFSBLOCK; |
| 364 | mp = ip->i_mount; |
| 365 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 366 | block = ifp->if_broot; |
| 367 | /* |
| 368 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 369 | */ |
| 370 | level = be16_to_cpu(block->bb_level); |
| 371 | ASSERT(level > 0); |
| 372 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 373 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 374 | bno = be64_to_cpu(*pp); |
| 375 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 376 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 377 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 378 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 379 | |
| 380 | /* |
| 381 | * Go down the tree until leaf level is reached, following the first |
| 382 | * pointer (leftmost) at each level. |
| 383 | */ |
| 384 | while (level-- > 0) { |
| 385 | /* See if buf is in cur first */ |
| 386 | bp_release = 0; |
| 387 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 388 | if (!bp) { |
| 389 | bp_release = 1; |
| 390 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 391 | XFS_BMAP_BTREE_REF, |
| 392 | &xfs_bmbt_buf_ops); |
| 393 | if (error) |
| 394 | goto error_norelse; |
| 395 | } |
| 396 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 397 | if (level == 0) |
| 398 | break; |
| 399 | |
| 400 | /* |
| 401 | * Check this block for basic sanity (increasing keys and |
| 402 | * no duplicate blocks). |
| 403 | */ |
| 404 | |
| 405 | xfs_check_block(block, mp, 0, 0); |
| 406 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 407 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 408 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 409 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 410 | if (bp_release) { |
| 411 | bp_release = 0; |
| 412 | xfs_trans_brelse(NULL, bp); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 418 | */ |
| 419 | i = 0; |
| 420 | |
| 421 | /* |
| 422 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 423 | */ |
| 424 | for (;;) { |
| 425 | xfs_fsblock_t nextbno; |
| 426 | xfs_extnum_t num_recs; |
| 427 | |
| 428 | |
| 429 | num_recs = xfs_btree_get_numrecs(block); |
| 430 | |
| 431 | /* |
| 432 | * Read-ahead the next leaf block, if any. |
| 433 | */ |
| 434 | |
| 435 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 436 | |
| 437 | /* |
| 438 | * Check all the extents to make sure they are OK. |
| 439 | * If we had a previous block, the last entry should |
| 440 | * conform with the first entry in this one. |
| 441 | */ |
| 442 | |
| 443 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 444 | if (i) { |
| 445 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 446 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 447 | xfs_bmbt_disk_get_startoff(ep)); |
| 448 | } |
| 449 | for (j = 1; j < num_recs; j++) { |
| 450 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 451 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 452 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 453 | xfs_bmbt_disk_get_startoff(nextp)); |
| 454 | ep = nextp; |
| 455 | } |
| 456 | |
| 457 | last = *ep; |
| 458 | i += num_recs; |
| 459 | if (bp_release) { |
| 460 | bp_release = 0; |
| 461 | xfs_trans_brelse(NULL, bp); |
| 462 | } |
| 463 | bno = nextbno; |
| 464 | /* |
| 465 | * If we've reached the end, stop. |
| 466 | */ |
| 467 | if (bno == NULLFSBLOCK) |
| 468 | break; |
| 469 | |
| 470 | bp_release = 0; |
| 471 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 472 | if (!bp) { |
| 473 | bp_release = 1; |
| 474 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 475 | XFS_BMAP_BTREE_REF, |
| 476 | &xfs_bmbt_buf_ops); |
| 477 | if (error) |
| 478 | goto error_norelse; |
| 479 | } |
| 480 | block = XFS_BUF_TO_BLOCK(bp); |
| 481 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 482 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 483 | return; |
| 484 | |
| 485 | error0: |
| 486 | xfs_warn(mp, "%s: at error0", __func__); |
| 487 | if (bp_release) |
| 488 | xfs_trans_brelse(NULL, bp); |
| 489 | error_norelse: |
| 490 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 491 | __func__, i); |
| 492 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Add bmap trace insert entries for all the contents of the extent records. |
| 498 | */ |
| 499 | void |
| 500 | xfs_bmap_trace_exlist( |
| 501 | xfs_inode_t *ip, /* incore inode pointer */ |
| 502 | xfs_extnum_t cnt, /* count of entries in the list */ |
| 503 | int whichfork, /* data or attr fork */ |
| 504 | unsigned long caller_ip) |
| 505 | { |
| 506 | xfs_extnum_t idx; /* extent record index */ |
| 507 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 508 | int state = 0; |
| 509 | |
| 510 | if (whichfork == XFS_ATTR_FORK) |
| 511 | state |= BMAP_ATTRFORK; |
| 512 | |
| 513 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 514 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
| 515 | for (idx = 0; idx < cnt; idx++) |
| 516 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 521 | * given the caller's original parameters. Specifically check the |
| 522 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 523 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 524 | */ |
| 525 | STATIC void |
| 526 | xfs_bmap_validate_ret( |
| 527 | xfs_fileoff_t bno, |
| 528 | xfs_filblks_t len, |
| 529 | int flags, |
| 530 | xfs_bmbt_irec_t *mval, |
| 531 | int nmap, |
| 532 | int ret_nmap) |
| 533 | { |
| 534 | int i; /* index to map values */ |
| 535 | |
| 536 | ASSERT(ret_nmap <= nmap); |
| 537 | |
| 538 | for (i = 0; i < ret_nmap; i++) { |
| 539 | ASSERT(mval[i].br_blockcount > 0); |
| 540 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 541 | ASSERT(mval[i].br_startoff >= bno); |
| 542 | ASSERT(mval[i].br_blockcount <= len); |
| 543 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 544 | bno + len); |
| 545 | } else { |
| 546 | ASSERT(mval[i].br_startoff < bno + len); |
| 547 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 548 | bno); |
| 549 | } |
| 550 | ASSERT(i == 0 || |
| 551 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 552 | mval[i].br_startoff); |
| 553 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 554 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 555 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 556 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | #else |
| 561 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
| 562 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 563 | #endif /* DEBUG */ |
| 564 | |
| 565 | /* |
| 566 | * bmap free list manipulation functions |
| 567 | */ |
| 568 | |
| 569 | /* |
| 570 | * Add the extent to the list of extents to be free at transaction end. |
| 571 | * The list is maintained sorted (by block number). |
| 572 | */ |
| 573 | void |
| 574 | xfs_bmap_add_free( |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame^] | 575 | struct xfs_mount *mp, |
| 576 | struct xfs_defer_ops *dfops, |
| 577 | xfs_fsblock_t bno, |
| 578 | xfs_filblks_t len, |
| 579 | struct xfs_owner_info *oinfo) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 580 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 581 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 582 | #ifdef DEBUG |
| 583 | xfs_agnumber_t agno; |
| 584 | xfs_agblock_t agbno; |
| 585 | |
| 586 | ASSERT(bno != NULLFSBLOCK); |
| 587 | ASSERT(len > 0); |
| 588 | ASSERT(len <= MAXEXTLEN); |
| 589 | ASSERT(!isnullstartblock(bno)); |
| 590 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 591 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 592 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 593 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 594 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 595 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 596 | #endif |
| 597 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame^] | 598 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 599 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 600 | new->xefi_startblock = bno; |
| 601 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame^] | 602 | if (oinfo) |
| 603 | new->xefi_oinfo = *oinfo; |
| 604 | else |
| 605 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 606 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
| 607 | XFS_FSB_TO_AGBNO(mp, bno), len); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 608 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Inode fork format manipulation functions |
| 613 | */ |
| 614 | |
| 615 | /* |
| 616 | * Transform a btree format file with only one leaf node, where the |
| 617 | * extents list will fit in the inode, into an extents format file. |
| 618 | * Since the file extents are already in-core, all we have to do is |
| 619 | * give up the space for the btree root and pitch the leaf block. |
| 620 | */ |
| 621 | STATIC int /* error */ |
| 622 | xfs_bmap_btree_to_extents( |
| 623 | xfs_trans_t *tp, /* transaction pointer */ |
| 624 | xfs_inode_t *ip, /* incore inode pointer */ |
| 625 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 626 | int *logflagsp, /* inode logging flags */ |
| 627 | int whichfork) /* data or attr fork */ |
| 628 | { |
| 629 | /* REFERENCED */ |
| 630 | struct xfs_btree_block *cblock;/* child btree block */ |
| 631 | xfs_fsblock_t cbno; /* child block number */ |
| 632 | xfs_buf_t *cbp; /* child block's buffer */ |
| 633 | int error; /* error return value */ |
| 634 | xfs_ifork_t *ifp; /* inode fork data */ |
| 635 | xfs_mount_t *mp; /* mount point structure */ |
| 636 | __be64 *pp; /* ptr to block address */ |
| 637 | struct xfs_btree_block *rblock;/* root btree block */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame^] | 638 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 639 | |
| 640 | mp = ip->i_mount; |
| 641 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 642 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 643 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 644 | rblock = ifp->if_broot; |
| 645 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 646 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 647 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 648 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 649 | cbno = be64_to_cpu(*pp); |
| 650 | *logflagsp = 0; |
| 651 | #ifdef DEBUG |
| 652 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
| 653 | return error; |
| 654 | #endif |
| 655 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 656 | &xfs_bmbt_buf_ops); |
| 657 | if (error) |
| 658 | return error; |
| 659 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 660 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 661 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame^] | 662 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
| 663 | 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] | 664 | ip->i_d.di_nblocks--; |
| 665 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 666 | xfs_trans_binval(tp, cbp); |
| 667 | if (cur->bc_bufs[0] == cbp) |
| 668 | cur->bc_bufs[0] = NULL; |
| 669 | xfs_iroot_realloc(ip, -1, whichfork); |
| 670 | ASSERT(ifp->if_broot == NULL); |
| 671 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 672 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 673 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Convert an extents-format file into a btree-format file. |
| 679 | * The new file will have a root block (in the inode) and a single child block. |
| 680 | */ |
| 681 | STATIC int /* error */ |
| 682 | xfs_bmap_extents_to_btree( |
| 683 | xfs_trans_t *tp, /* transaction pointer */ |
| 684 | xfs_inode_t *ip, /* incore inode pointer */ |
| 685 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 686 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 687 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 688 | int wasdel, /* converting a delayed alloc */ |
| 689 | int *logflagsp, /* inode logging flags */ |
| 690 | int whichfork) /* data or attr fork */ |
| 691 | { |
| 692 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 693 | xfs_buf_t *abp; /* buffer for ablock */ |
| 694 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 695 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 696 | struct xfs_btree_block *block; /* btree root block */ |
| 697 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 698 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 699 | int error; /* error return value */ |
| 700 | xfs_extnum_t i, cnt; /* extent record index */ |
| 701 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 702 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 703 | xfs_mount_t *mp; /* mount structure */ |
| 704 | xfs_extnum_t nextents; /* number of file extents */ |
| 705 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 706 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 707 | mp = ip->i_mount; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 708 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 709 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 710 | |
| 711 | /* |
| 712 | * Make space in the inode incore. |
| 713 | */ |
| 714 | xfs_iroot_realloc(ip, 1, whichfork); |
| 715 | ifp->if_flags |= XFS_IFBROOT; |
| 716 | |
| 717 | /* |
| 718 | * Fill in the root. |
| 719 | */ |
| 720 | block = ifp->if_broot; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 721 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 722 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 723 | XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino, |
| 724 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); |
| 725 | else |
| 726 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 727 | XFS_BMAP_MAGIC, 1, 1, ip->i_ino, |
| 728 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 729 | |
| 730 | /* |
| 731 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 732 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 733 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 734 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 735 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 736 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 737 | /* |
| 738 | * Convert to a btree with two levels, one record in root. |
| 739 | */ |
| 740 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 741 | memset(&args, 0, sizeof(args)); |
| 742 | args.tp = tp; |
| 743 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame^] | 744 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 745 | args.firstblock = *firstblock; |
| 746 | if (*firstblock == NULLFSBLOCK) { |
| 747 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 748 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 749 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 750 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 751 | args.fsbno = *firstblock; |
| 752 | } else { |
| 753 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 754 | args.fsbno = *firstblock; |
| 755 | } |
| 756 | args.minlen = args.maxlen = args.prod = 1; |
| 757 | args.wasdel = wasdel; |
| 758 | *logflagsp = 0; |
| 759 | if ((error = xfs_alloc_vextent(&args))) { |
| 760 | xfs_iroot_realloc(ip, -1, whichfork); |
| 761 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 762 | return error; |
| 763 | } |
| 764 | /* |
| 765 | * Allocation can't fail, the space was reserved. |
| 766 | */ |
| 767 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 768 | ASSERT(*firstblock == NULLFSBLOCK || |
| 769 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 770 | (dfops->dop_low && |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 771 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 772 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 773 | cur->bc_private.b.allocated++; |
| 774 | ip->i_d.di_nblocks++; |
| 775 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 776 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 777 | /* |
| 778 | * Fill in the child block. |
| 779 | */ |
| 780 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 781 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 782 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 783 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 784 | XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino, |
| 785 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); |
| 786 | else |
| 787 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 788 | XFS_BMAP_MAGIC, 0, 0, ip->i_ino, |
| 789 | XFS_BTREE_LONG_PTRS); |
| 790 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 791 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 792 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 793 | for (cnt = i = 0; i < nextents; i++) { |
| 794 | ep = xfs_iext_get_ext(ifp, i); |
| 795 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
| 796 | arp->l0 = cpu_to_be64(ep->l0); |
| 797 | arp->l1 = cpu_to_be64(ep->l1); |
| 798 | arp++; cnt++; |
| 799 | } |
| 800 | } |
| 801 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 802 | xfs_btree_set_numrecs(ablock, cnt); |
| 803 | |
| 804 | /* |
| 805 | * Fill in the root key and pointer. |
| 806 | */ |
| 807 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 808 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 809 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 810 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 811 | be16_to_cpu(block->bb_level))); |
| 812 | *pp = cpu_to_be64(args.fsbno); |
| 813 | |
| 814 | /* |
| 815 | * Do all this logging at the end so that |
| 816 | * the root is at the right level. |
| 817 | */ |
| 818 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 819 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 820 | ASSERT(*curp == NULL); |
| 821 | *curp = cur; |
| 822 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Convert a local file to an extents file. |
| 828 | * This code is out of bounds for data forks of regular files, |
| 829 | * since the file data needs to get logged so things will stay consistent. |
| 830 | * (The bmap-level manipulations are ok, though). |
| 831 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 832 | void |
| 833 | xfs_bmap_local_to_extents_empty( |
| 834 | struct xfs_inode *ip, |
| 835 | int whichfork) |
| 836 | { |
| 837 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 838 | |
| 839 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 840 | ASSERT(ifp->if_bytes == 0); |
| 841 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 842 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 843 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 844 | ifp->if_flags &= ~XFS_IFINLINE; |
| 845 | ifp->if_flags |= XFS_IFEXTENTS; |
| 846 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 847 | } |
| 848 | |
| 849 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 850 | STATIC int /* error */ |
| 851 | xfs_bmap_local_to_extents( |
| 852 | xfs_trans_t *tp, /* transaction pointer */ |
| 853 | xfs_inode_t *ip, /* incore inode pointer */ |
| 854 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 855 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 856 | int *logflagsp, /* inode logging flags */ |
| 857 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 858 | void (*init_fn)(struct xfs_trans *tp, |
| 859 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 860 | struct xfs_inode *ip, |
| 861 | struct xfs_ifork *ifp)) |
| 862 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 863 | int error = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 864 | int flags; /* logging flags returned */ |
| 865 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 866 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 867 | xfs_buf_t *bp; /* buffer for extent block */ |
| 868 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 869 | |
| 870 | /* |
| 871 | * We don't want to deal with the case of keeping inode data inline yet. |
| 872 | * So sending the data fork of a regular inode is invalid. |
| 873 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 874 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 875 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 876 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 877 | |
| 878 | if (!ifp->if_bytes) { |
| 879 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 880 | flags = XFS_ILOG_CORE; |
| 881 | goto done; |
| 882 | } |
| 883 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 884 | flags = 0; |
| 885 | error = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 886 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
| 887 | XFS_IFINLINE); |
| 888 | memset(&args, 0, sizeof(args)); |
| 889 | args.tp = tp; |
| 890 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame^] | 891 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 892 | args.firstblock = *firstblock; |
| 893 | /* |
| 894 | * Allocate a block. We know we need only one, since the |
| 895 | * file currently fits in an inode. |
| 896 | */ |
| 897 | if (*firstblock == NULLFSBLOCK) { |
| 898 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 899 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 900 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 901 | args.fsbno = *firstblock; |
| 902 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 903 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 904 | args.total = total; |
| 905 | args.minlen = args.maxlen = args.prod = 1; |
| 906 | error = xfs_alloc_vextent(&args); |
| 907 | if (error) |
| 908 | goto done; |
| 909 | |
| 910 | /* Can't fail, the space was reserved. */ |
| 911 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 912 | ASSERT(args.len == 1); |
| 913 | *firstblock = args.fsbno; |
| 914 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 915 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 916 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 917 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 918 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 919 | * The callout is responsible for logging because the remote format |
| 920 | * might differ from the local format and thus we don't know how much to |
| 921 | * log here. Note that init_fn must also set the buffer log item type |
| 922 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 923 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 924 | init_fn(tp, bp, ip, ifp); |
| 925 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 926 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 927 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 928 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 929 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 930 | |
| 931 | xfs_iext_add(ifp, 0, 1); |
| 932 | ep = xfs_iext_get_ext(ifp, 0); |
| 933 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
| 934 | trace_xfs_bmap_post_update(ip, 0, |
| 935 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 936 | _THIS_IP_); |
| 937 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 938 | ip->i_d.di_nblocks = 1; |
| 939 | xfs_trans_mod_dquot_byino(tp, ip, |
| 940 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 941 | flags |= xfs_ilog_fext(whichfork); |
| 942 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 943 | done: |
| 944 | *logflagsp = flags; |
| 945 | return error; |
| 946 | } |
| 947 | |
| 948 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 950 | */ |
| 951 | STATIC int /* error */ |
| 952 | xfs_bmap_add_attrfork_btree( |
| 953 | xfs_trans_t *tp, /* transaction pointer */ |
| 954 | xfs_inode_t *ip, /* incore inode pointer */ |
| 955 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 956 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | int *flags) /* inode logging flags */ |
| 958 | { |
| 959 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 960 | int error; /* error return value */ |
| 961 | xfs_mount_t *mp; /* file system mount struct */ |
| 962 | int stat; /* newroot status */ |
| 963 | |
| 964 | mp = ip->i_mount; |
| 965 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 966 | *flags |= XFS_ILOG_DBROOT; |
| 967 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 968 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 969 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | cur->bc_private.b.firstblock = *firstblock; |
| 971 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 972 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 973 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 974 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 975 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | goto error0; |
| 977 | if (stat == 0) { |
| 978 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 979 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
| 981 | *firstblock = cur->bc_private.b.firstblock; |
| 982 | cur->bc_private.b.allocated = 0; |
| 983 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 984 | } |
| 985 | return 0; |
| 986 | error0: |
| 987 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 988 | return error; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 993 | */ |
| 994 | STATIC int /* error */ |
| 995 | xfs_bmap_add_attrfork_extents( |
| 996 | xfs_trans_t *tp, /* transaction pointer */ |
| 997 | xfs_inode_t *ip, /* incore inode pointer */ |
| 998 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 999 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | int *flags) /* inode logging flags */ |
| 1001 | { |
| 1002 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 1003 | int error; /* error return value */ |
| 1004 | |
| 1005 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 1006 | return 0; |
| 1007 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1008 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | flags, XFS_DATA_FORK); |
| 1010 | if (cur) { |
| 1011 | cur->bc_private.b.allocated = 0; |
| 1012 | xfs_btree_del_cursor(cur, |
| 1013 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 1014 | } |
| 1015 | return error; |
| 1016 | } |
| 1017 | |
| 1018 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1019 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 1020 | * different data fork content type needs a different callout to do the |
| 1021 | * conversion. Some are basic and only require special block initialisation |
| 1022 | * callouts for the data formating, others (directories) are so specialised they |
| 1023 | * handle everything themselves. |
| 1024 | * |
| 1025 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1026 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1027 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | */ |
| 1029 | STATIC int /* error */ |
| 1030 | xfs_bmap_add_attrfork_local( |
| 1031 | xfs_trans_t *tp, /* transaction pointer */ |
| 1032 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1033 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1034 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | int *flags) /* inode logging flags */ |
| 1036 | { |
| 1037 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
| 1039 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1040 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1041 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1042 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1044 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | dargs.dp = ip; |
| 1046 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1047 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1048 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | dargs.whichfork = XFS_DATA_FORK; |
| 1050 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1051 | return xfs_dir2_sf_to_block(&dargs); |
| 1052 | } |
| 1053 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1054 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1055 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1056 | flags, XFS_DATA_FORK, |
| 1057 | xfs_symlink_local_to_remote); |
| 1058 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1059 | /* should only be called for types that support local format data */ |
| 1060 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1061 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1065 | * Convert inode from non-attributed to attributed. |
| 1066 | * Must not be in a transaction, ip must not be locked. |
| 1067 | */ |
| 1068 | int /* error code */ |
| 1069 | xfs_bmap_add_attrfork( |
| 1070 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1071 | int size, /* space new attribute needs */ |
| 1072 | int rsvd) /* xact may use reserved blks */ |
| 1073 | { |
| 1074 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1075 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1076 | xfs_mount_t *mp; /* mount structure */ |
| 1077 | xfs_trans_t *tp; /* transaction pointer */ |
| 1078 | int blks; /* space reservation */ |
| 1079 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1080 | int logflags; /* logging flags */ |
| 1081 | int error; /* error return value */ |
| 1082 | |
| 1083 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1084 | |
| 1085 | mp = ip->i_mount; |
| 1086 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1087 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1088 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1089 | |
| 1090 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1091 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1092 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1093 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1094 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1095 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1096 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1097 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1098 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1099 | if (error) |
| 1100 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1101 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1102 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1103 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1104 | /* |
| 1105 | * For inodes coming from pre-6.2 filesystems. |
| 1106 | */ |
| 1107 | ASSERT(ip->i_d.di_aformat == 0); |
| 1108 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1109 | } |
| 1110 | ASSERT(ip->i_d.di_anextents == 0); |
| 1111 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1112 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1113 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1114 | |
| 1115 | switch (ip->i_d.di_format) { |
| 1116 | case XFS_DINODE_FMT_DEV: |
| 1117 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1118 | break; |
| 1119 | case XFS_DINODE_FMT_UUID: |
| 1120 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 1121 | break; |
| 1122 | case XFS_DINODE_FMT_LOCAL: |
| 1123 | case XFS_DINODE_FMT_EXTENTS: |
| 1124 | case XFS_DINODE_FMT_BTREE: |
| 1125 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1126 | if (!ip->i_d.di_forkoff) |
| 1127 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1128 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1129 | version = 2; |
| 1130 | break; |
| 1131 | default: |
| 1132 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1133 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1134 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | ASSERT(ip->i_afp == NULL); |
| 1138 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1139 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1140 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1141 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1142 | switch (ip->i_d.di_format) { |
| 1143 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1144 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1145 | &logflags); |
| 1146 | break; |
| 1147 | case XFS_DINODE_FMT_EXTENTS: |
| 1148 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1149 | &dfops, &logflags); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1150 | break; |
| 1151 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1152 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1153 | &logflags); |
| 1154 | break; |
| 1155 | default: |
| 1156 | error = 0; |
| 1157 | break; |
| 1158 | } |
| 1159 | if (logflags) |
| 1160 | xfs_trans_log_inode(tp, ip, logflags); |
| 1161 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1162 | goto bmap_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1163 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1164 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1165 | bool log_sb = false; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1166 | |
| 1167 | spin_lock(&mp->m_sb_lock); |
| 1168 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1169 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1170 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1171 | } |
| 1172 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1173 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1174 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1175 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1176 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1177 | if (log_sb) |
| 1178 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1179 | } |
| 1180 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1181 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1182 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1183 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1184 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1185 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1186 | return error; |
| 1187 | |
| 1188 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1189 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1190 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1191 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1192 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1193 | return error; |
| 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * Internal and external extent tree search functions. |
| 1198 | */ |
| 1199 | |
| 1200 | /* |
| 1201 | * Read in the extents to if_extents. |
| 1202 | * All inode fields are set up by caller, we just traverse the btree |
| 1203 | * and copy the records in. If the file system cannot contain unwritten |
| 1204 | * extents, the records are checked for no "state" flags. |
| 1205 | */ |
| 1206 | int /* error */ |
| 1207 | xfs_bmap_read_extents( |
| 1208 | xfs_trans_t *tp, /* transaction pointer */ |
| 1209 | xfs_inode_t *ip, /* incore inode */ |
| 1210 | int whichfork) /* data or attr fork */ |
| 1211 | { |
| 1212 | struct xfs_btree_block *block; /* current btree block */ |
| 1213 | xfs_fsblock_t bno; /* block # of "block" */ |
| 1214 | xfs_buf_t *bp; /* buffer for "block" */ |
| 1215 | int error; /* error return value */ |
| 1216 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
| 1217 | xfs_extnum_t i, j; /* index into the extents list */ |
| 1218 | xfs_ifork_t *ifp; /* fork structure */ |
| 1219 | int level; /* btree level, for checking */ |
| 1220 | xfs_mount_t *mp; /* file system mount structure */ |
| 1221 | __be64 *pp; /* pointer to block address */ |
| 1222 | /* REFERENCED */ |
| 1223 | xfs_extnum_t room; /* number of entries there's room for */ |
| 1224 | |
| 1225 | bno = NULLFSBLOCK; |
| 1226 | mp = ip->i_mount; |
| 1227 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1228 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 1229 | XFS_EXTFMT_INODE(ip); |
| 1230 | block = ifp->if_broot; |
| 1231 | /* |
| 1232 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1233 | */ |
| 1234 | level = be16_to_cpu(block->bb_level); |
| 1235 | ASSERT(level > 0); |
| 1236 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1237 | bno = be64_to_cpu(*pp); |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 1238 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1239 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 1240 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 1241 | /* |
| 1242 | * Go down the tree until leaf level is reached, following the first |
| 1243 | * pointer (leftmost) at each level. |
| 1244 | */ |
| 1245 | while (level-- > 0) { |
| 1246 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1247 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1248 | if (error) |
| 1249 | return error; |
| 1250 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1251 | if (level == 0) |
| 1252 | break; |
| 1253 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1254 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1255 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 1256 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1257 | xfs_trans_brelse(tp, bp); |
| 1258 | } |
| 1259 | /* |
| 1260 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1261 | */ |
| 1262 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 1263 | i = 0; |
| 1264 | /* |
| 1265 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1266 | */ |
| 1267 | for (;;) { |
| 1268 | xfs_bmbt_rec_t *frp; |
| 1269 | xfs_fsblock_t nextbno; |
| 1270 | xfs_extnum_t num_recs; |
| 1271 | xfs_extnum_t start; |
| 1272 | |
| 1273 | num_recs = xfs_btree_get_numrecs(block); |
| 1274 | if (unlikely(i + num_recs > room)) { |
| 1275 | ASSERT(i + num_recs <= room); |
| 1276 | xfs_warn(ip->i_mount, |
| 1277 | "corrupt dinode %Lu, (btree extents).", |
| 1278 | (unsigned long long) ip->i_ino); |
| 1279 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 1280 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
| 1281 | goto error0; |
| 1282 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1283 | /* |
| 1284 | * Read-ahead the next leaf block, if any. |
| 1285 | */ |
| 1286 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1287 | if (nextbno != NULLFSBLOCK) |
| 1288 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1289 | &xfs_bmbt_buf_ops); |
| 1290 | /* |
| 1291 | * Copy records into the extent records. |
| 1292 | */ |
| 1293 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 1294 | start = i; |
| 1295 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 1296 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
| 1297 | trp->l0 = be64_to_cpu(frp->l0); |
| 1298 | trp->l1 = be64_to_cpu(frp->l1); |
| 1299 | } |
| 1300 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 1301 | /* |
| 1302 | * Check all attribute bmap btree records and |
| 1303 | * any "older" data bmap btree records for a |
| 1304 | * set bit in the "extent flag" position. |
| 1305 | */ |
| 1306 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 1307 | start, num_recs))) { |
| 1308 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 1309 | XFS_ERRLEVEL_LOW, |
| 1310 | ip->i_mount); |
| 1311 | goto error0; |
| 1312 | } |
| 1313 | } |
| 1314 | xfs_trans_brelse(tp, bp); |
| 1315 | bno = nextbno; |
| 1316 | /* |
| 1317 | * If we've reached the end, stop. |
| 1318 | */ |
| 1319 | if (bno == NULLFSBLOCK) |
| 1320 | break; |
| 1321 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1322 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1323 | if (error) |
| 1324 | return error; |
| 1325 | block = XFS_BUF_TO_BLOCK(bp); |
| 1326 | } |
| 1327 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
| 1328 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 1329 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
| 1330 | return 0; |
| 1331 | error0: |
| 1332 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1333 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | |
| 1337 | /* |
| 1338 | * Search the extent records for the entry containing block bno. |
| 1339 | * If bno lies in a hole, point to the next entry. If bno lies |
| 1340 | * past eof, *eofp will be set, and *prevp will contain the last |
| 1341 | * entry (null if none). Else, *lastxp will be set to the index |
| 1342 | * of the found entry; *gotp will contain the entry. |
| 1343 | */ |
| 1344 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
| 1345 | xfs_bmap_search_multi_extents( |
| 1346 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 1347 | xfs_fileoff_t bno, /* block number searched for */ |
| 1348 | int *eofp, /* out: end of file found */ |
| 1349 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 1350 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 1351 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 1352 | { |
| 1353 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 1354 | xfs_extnum_t lastx; /* last extent index */ |
| 1355 | |
| 1356 | /* |
| 1357 | * Initialize the extent entry structure to catch access to |
| 1358 | * uninitialized br_startblock field. |
| 1359 | */ |
| 1360 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 1361 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 1362 | gotp->br_state = XFS_EXT_INVALID; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1363 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1364 | prevp->br_startoff = NULLFILEOFF; |
| 1365 | |
| 1366 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 1367 | if (lastx > 0) { |
| 1368 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 1369 | } |
| 1370 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 1371 | xfs_bmbt_get_all(ep, gotp); |
| 1372 | *eofp = 0; |
| 1373 | } else { |
| 1374 | if (lastx > 0) { |
| 1375 | *gotp = *prevp; |
| 1376 | } |
| 1377 | *eofp = 1; |
| 1378 | ep = NULL; |
| 1379 | } |
| 1380 | *lastxp = lastx; |
| 1381 | return ep; |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * Search the extents list for the inode, for the extent containing bno. |
| 1386 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 1387 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 1388 | * Else, *lastxp will be set to the index of the found |
| 1389 | * entry; *gotp will contain the entry. |
| 1390 | */ |
| 1391 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
| 1392 | xfs_bmap_search_extents( |
| 1393 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1394 | xfs_fileoff_t bno, /* block number searched for */ |
| 1395 | int fork, /* data or attr fork */ |
| 1396 | int *eofp, /* out: end of file found */ |
| 1397 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 1398 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 1399 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 1400 | { |
| 1401 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1402 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 1403 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1404 | XFS_STATS_INC(ip->i_mount, xs_look_exlist); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1405 | ifp = XFS_IFORK_PTR(ip, fork); |
| 1406 | |
| 1407 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 1408 | |
| 1409 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 1410 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
| 1411 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
| 1412 | "Access to block zero in inode %llu " |
| 1413 | "start_block: %llx start_off: %llx " |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 1414 | "blkcnt: %llx extent-state: %x lastx: %x", |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1415 | (unsigned long long)ip->i_ino, |
| 1416 | (unsigned long long)gotp->br_startblock, |
| 1417 | (unsigned long long)gotp->br_startoff, |
| 1418 | (unsigned long long)gotp->br_blockcount, |
| 1419 | gotp->br_state, *lastxp); |
| 1420 | *lastxp = NULLEXTNUM; |
| 1421 | *eofp = 1; |
| 1422 | return NULL; |
| 1423 | } |
| 1424 | return ep; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * Returns the file-relative block number of the first unused block(s) |
| 1429 | * in the file with at least "len" logically contiguous blocks free. |
| 1430 | * This is the lowest-address hole if the file has holes, else the first block |
| 1431 | * past the end of file. |
| 1432 | * Return 0 if the file is currently local (in-inode). |
| 1433 | */ |
| 1434 | int /* error */ |
| 1435 | xfs_bmap_first_unused( |
| 1436 | xfs_trans_t *tp, /* transaction pointer */ |
| 1437 | xfs_inode_t *ip, /* incore inode */ |
| 1438 | xfs_extlen_t len, /* size of hole to find */ |
| 1439 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1440 | int whichfork) /* data or attr fork */ |
| 1441 | { |
| 1442 | int error; /* error return value */ |
| 1443 | int idx; /* extent record index */ |
| 1444 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1445 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 1446 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 1447 | xfs_fileoff_t max; /* starting useful block */ |
| 1448 | xfs_fileoff_t off; /* offset for this block */ |
| 1449 | xfs_extnum_t nextents; /* number of extent entries */ |
| 1450 | |
| 1451 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1452 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1453 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 1454 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1455 | *first_unused = 0; |
| 1456 | return 0; |
| 1457 | } |
| 1458 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1459 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1460 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1461 | return error; |
| 1462 | lowest = *first_unused; |
| 1463 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 1464 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
| 1465 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
| 1466 | off = xfs_bmbt_get_startoff(ep); |
| 1467 | /* |
| 1468 | * See if the hole before this extent will work. |
| 1469 | */ |
| 1470 | if (off >= lowest + len && off - max >= len) { |
| 1471 | *first_unused = max; |
| 1472 | return 0; |
| 1473 | } |
| 1474 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 1475 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1476 | } |
| 1477 | *first_unused = max; |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1482 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1483 | * last_block (input value) in the file. |
| 1484 | * This is not based on i_size, it is based on the extent records. |
| 1485 | * Returns 0 for local files, as they do not have extent records. |
| 1486 | */ |
| 1487 | int /* error */ |
| 1488 | xfs_bmap_last_before( |
| 1489 | xfs_trans_t *tp, /* transaction pointer */ |
| 1490 | xfs_inode_t *ip, /* incore inode */ |
| 1491 | xfs_fileoff_t *last_block, /* last block */ |
| 1492 | int whichfork) /* data or attr fork */ |
| 1493 | { |
| 1494 | xfs_fileoff_t bno; /* input file offset */ |
| 1495 | int eof; /* hit end of file */ |
| 1496 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
| 1497 | int error; /* error return value */ |
| 1498 | xfs_bmbt_irec_t got; /* current extent value */ |
| 1499 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1500 | xfs_extnum_t lastx; /* last extent used */ |
| 1501 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 1502 | |
| 1503 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1504 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 1505 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1506 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1507 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1508 | *last_block = 0; |
| 1509 | return 0; |
| 1510 | } |
| 1511 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1512 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1513 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1514 | return error; |
| 1515 | bno = *last_block - 1; |
| 1516 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 1517 | &prev); |
| 1518 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 1519 | if (prev.br_startoff == NULLFILEOFF) |
| 1520 | *last_block = 0; |
| 1521 | else |
| 1522 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 1523 | } |
| 1524 | /* |
| 1525 | * Otherwise *last_block is already the right answer. |
| 1526 | */ |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1530 | int |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1531 | xfs_bmap_last_extent( |
| 1532 | struct xfs_trans *tp, |
| 1533 | struct xfs_inode *ip, |
| 1534 | int whichfork, |
| 1535 | struct xfs_bmbt_irec *rec, |
| 1536 | int *is_empty) |
| 1537 | { |
| 1538 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1539 | int error; |
| 1540 | int nextents; |
| 1541 | |
| 1542 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1543 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1544 | if (error) |
| 1545 | return error; |
| 1546 | } |
| 1547 | |
| 1548 | nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
| 1549 | if (nextents == 0) { |
| 1550 | *is_empty = 1; |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
| 1554 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 1555 | *is_empty = 0; |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * Check the last inode extent to determine whether this allocation will result |
| 1561 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1562 | * blocks at the end of the file which do not start at the previous data block, |
| 1563 | * we will try to align the new blocks at stripe unit boundaries. |
| 1564 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1565 | * 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] | 1566 | * at, or past the EOF. |
| 1567 | */ |
| 1568 | STATIC int |
| 1569 | xfs_bmap_isaeof( |
| 1570 | struct xfs_bmalloca *bma, |
| 1571 | int whichfork) |
| 1572 | { |
| 1573 | struct xfs_bmbt_irec rec; |
| 1574 | int is_empty; |
| 1575 | int error; |
| 1576 | |
| 1577 | bma->aeof = 0; |
| 1578 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1579 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1580 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1581 | return error; |
| 1582 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1583 | if (is_empty) { |
| 1584 | bma->aeof = 1; |
| 1585 | return 0; |
| 1586 | } |
| 1587 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1588 | /* |
| 1589 | * Check if we are allocation or past the last extent, or at least into |
| 1590 | * the last delayed allocated extent. |
| 1591 | */ |
| 1592 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1593 | (bma->offset >= rec.br_startoff && |
| 1594 | isnullstartblock(rec.br_startblock)); |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1599 | * Returns the file-relative block number of the first block past eof in |
| 1600 | * the file. This is not based on i_size, it is based on the extent records. |
| 1601 | * Returns 0 for local files, as they do not have extent records. |
| 1602 | */ |
| 1603 | int |
| 1604 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1605 | struct xfs_inode *ip, |
| 1606 | xfs_fileoff_t *last_block, |
| 1607 | int whichfork) |
| 1608 | { |
| 1609 | struct xfs_bmbt_irec rec; |
| 1610 | int is_empty; |
| 1611 | int error; |
| 1612 | |
| 1613 | *last_block = 0; |
| 1614 | |
| 1615 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1616 | return 0; |
| 1617 | |
| 1618 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1619 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1620 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1621 | |
| 1622 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1623 | if (error || is_empty) |
| 1624 | return error; |
| 1625 | |
| 1626 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | /* |
| 1631 | * Returns whether the selected fork of the inode has exactly one |
| 1632 | * block or not. For the data fork we check this matches di_size, |
| 1633 | * implying the file's range is 0..bsize-1. |
| 1634 | */ |
| 1635 | int /* 1=>1 block, 0=>otherwise */ |
| 1636 | xfs_bmap_one_block( |
| 1637 | xfs_inode_t *ip, /* incore inode */ |
| 1638 | int whichfork) /* data or attr fork */ |
| 1639 | { |
| 1640 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
| 1641 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1642 | int rval; /* return value */ |
| 1643 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 1644 | |
| 1645 | #ifndef DEBUG |
| 1646 | if (whichfork == XFS_DATA_FORK) |
| 1647 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1648 | #endif /* !DEBUG */ |
| 1649 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1650 | return 0; |
| 1651 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1652 | return 0; |
| 1653 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1654 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 1655 | ep = xfs_iext_get_ext(ifp, 0); |
| 1656 | xfs_bmbt_get_all(ep, &s); |
| 1657 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1658 | if (rval && whichfork == XFS_DATA_FORK) |
| 1659 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1660 | return rval; |
| 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | * Extent tree manipulation functions used during allocation. |
| 1665 | */ |
| 1666 | |
| 1667 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1668 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | */ |
| 1670 | STATIC int /* error */ |
| 1671 | xfs_bmap_add_extent_delay_real( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1672 | struct xfs_bmalloca *bma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1674 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1676 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1679 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1681 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1682 | /* left is 0, right is 1, prev is 2 */ |
| 1683 | int rval=0; /* return value (logging flags) */ |
| 1684 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1685 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1686 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1687 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 1688 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | int tmp_rval; /* partial logging flags */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1690 | int whichfork = XFS_DATA_FORK; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1691 | struct xfs_mount *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1693 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1694 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1695 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1696 | ASSERT(bma->idx >= 0); |
| 1697 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1698 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1699 | ASSERT(!bma->cur || |
| 1700 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1701 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1702 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | #define LEFT r[0] |
| 1705 | #define RIGHT r[1] |
| 1706 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | |
| 1708 | /* |
| 1709 | * Set up a bunch of variables to make the tests simpler. |
| 1710 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1711 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | xfs_bmbt_get_all(ep, &PREV); |
| 1713 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1714 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1715 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1716 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1717 | da_old = startblockval(PREV.br_startblock); |
| 1718 | da_new = 0; |
| 1719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | /* |
| 1721 | * Set flags determining what part of the previous delayed allocation |
| 1722 | * extent is being replaced by a real allocation. |
| 1723 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1724 | if (PREV.br_startoff == new->br_startoff) |
| 1725 | state |= BMAP_LEFT_FILLING; |
| 1726 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1727 | state |= BMAP_RIGHT_FILLING; |
| 1728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | /* |
| 1730 | * Check and set flags if this segment has a left neighbor. |
| 1731 | * Don't set contiguous if the combined extent would be too large. |
| 1732 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1733 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1734 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1735 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1736 | |
| 1737 | if (isnullstartblock(LEFT.br_startblock)) |
| 1738 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1740 | |
| 1741 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1742 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1743 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1744 | LEFT.br_state == new->br_state && |
| 1745 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1746 | state |= BMAP_LEFT_CONTIG; |
| 1747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | /* |
| 1749 | * Check and set flags if this segment has a right neighbor. |
| 1750 | * Don't set contiguous if the combined extent would be too large. |
| 1751 | * Also check for all-three-contiguous being too large. |
| 1752 | */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1753 | if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1754 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1755 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1756 | |
| 1757 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1758 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1760 | |
| 1761 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1762 | new_endoff == RIGHT.br_startoff && |
| 1763 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1764 | new->br_state == RIGHT.br_state && |
| 1765 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1766 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1767 | BMAP_RIGHT_FILLING)) != |
| 1768 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1769 | BMAP_RIGHT_FILLING) || |
| 1770 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1771 | <= MAXEXTLEN)) |
| 1772 | state |= BMAP_RIGHT_CONTIG; |
| 1773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | error = 0; |
| 1775 | /* |
| 1776 | * Switch out based on the FILLING and CONTIG state bits. |
| 1777 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1778 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1779 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1780 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1781 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | /* |
| 1783 | * Filling in all of a previously delayed allocation extent. |
| 1784 | * The left and right neighbors are both contiguous with new. |
| 1785 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1786 | bma->idx--; |
| 1787 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1788 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1790 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1791 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1792 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1793 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
| 1794 | bma->ip->i_d.di_nextents--; |
| 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 | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1799 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1801 | RIGHT.br_blockcount, &i); |
| 1802 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1804 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1805 | error = xfs_btree_delete(bma->cur, &i); |
| 1806 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1808 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1809 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1810 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1812 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1813 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | LEFT.br_startblock, |
| 1815 | LEFT.br_blockcount + |
| 1816 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1817 | RIGHT.br_blockcount, LEFT.br_state); |
| 1818 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | goto done; |
| 1820 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | break; |
| 1822 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1823 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | /* |
| 1825 | * Filling in all of a previously delayed allocation extent. |
| 1826 | * The left neighbor is contiguous, the right is not. |
| 1827 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1828 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1829 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1830 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1831 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1832 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1833 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1834 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1835 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1836 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | rval = XFS_ILOG_DEXT; |
| 1838 | else { |
| 1839 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1840 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1842 | &i); |
| 1843 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1845 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1846 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | LEFT.br_startblock, |
| 1848 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1849 | PREV.br_blockcount, LEFT.br_state); |
| 1850 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | goto done; |
| 1852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | break; |
| 1854 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1855 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | /* |
| 1857 | * Filling in all of a previously delayed allocation extent. |
| 1858 | * The right neighbor is contiguous, the left is not. |
| 1859 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1860 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 1862 | xfs_bmbt_set_blockcount(ep, |
| 1863 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1864 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1865 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1866 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1867 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | rval = XFS_ILOG_DEXT; |
| 1869 | else { |
| 1870 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1871 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1873 | RIGHT.br_blockcount, &i); |
| 1874 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1876 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1877 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | new->br_startblock, |
| 1879 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1880 | RIGHT.br_blockcount, PREV.br_state); |
| 1881 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | goto done; |
| 1883 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | break; |
| 1885 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1886 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | /* |
| 1888 | * Filling in all of a previously delayed allocation extent. |
| 1889 | * Neither the left nor right neighbors are contiguous with |
| 1890 | * the new one. |
| 1891 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1892 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1894 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1895 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1896 | bma->ip->i_d.di_nextents++; |
| 1897 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1899 | else { |
| 1900 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1901 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1903 | &i); |
| 1904 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1906 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1907 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1908 | error = xfs_btree_insert(bma->cur, &i); |
| 1909 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1911 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | break; |
| 1914 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1915 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | /* |
| 1917 | * Filling in the first part of a previous delayed allocation. |
| 1918 | * The left neighbor is contiguous. |
| 1919 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1920 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 1921 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | LEFT.br_blockcount + new->br_blockcount); |
| 1923 | xfs_bmbt_set_startoff(ep, |
| 1924 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1925 | trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1928 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1930 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | rval = XFS_ILOG_DEXT; |
| 1932 | else { |
| 1933 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1934 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1936 | &i); |
| 1937 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1939 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1940 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | LEFT.br_startblock, |
| 1942 | LEFT.br_blockcount + |
| 1943 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1944 | LEFT.br_state); |
| 1945 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | goto done; |
| 1947 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1948 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1949 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1950 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1951 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1952 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1953 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | break; |
| 1955 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1956 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | /* |
| 1958 | * Filling in the first part of a previous delayed allocation. |
| 1959 | * The left neighbor is not contiguous. |
| 1960 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1961 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1963 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1964 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1965 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 1966 | bma->ip->i_d.di_nextents++; |
| 1967 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1969 | else { |
| 1970 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1971 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1973 | &i); |
| 1974 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1976 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1977 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1978 | error = xfs_btree_insert(bma->cur, &i); |
| 1979 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1981 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1983 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1984 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1985 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1986 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1987 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | rval |= tmp_rval; |
| 1989 | if (error) |
| 1990 | goto done; |
| 1991 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1992 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1993 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1994 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 1995 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1996 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1997 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | break; |
| 1999 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2000 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | /* |
| 2002 | * Filling in the last part of a previous delayed allocation. |
| 2003 | * The right neighbor is contiguous with the new allocation. |
| 2004 | */ |
| 2005 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2006 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2008 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2009 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | new->br_blockcount + RIGHT.br_blockcount, |
| 2011 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2012 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 2013 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | rval = XFS_ILOG_DEXT; |
| 2015 | else { |
| 2016 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2017 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2019 | RIGHT.br_blockcount, &i); |
| 2020 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2022 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2023 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | new->br_startblock, |
| 2025 | new->br_blockcount + |
| 2026 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2027 | RIGHT.br_state); |
| 2028 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | goto done; |
| 2030 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2031 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2032 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2033 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2034 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2035 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2036 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2037 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2038 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | break; |
| 2040 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2041 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | /* |
| 2043 | * Filling in the last part of a previous delayed allocation. |
| 2044 | * The right neighbor is not contiguous. |
| 2045 | */ |
| 2046 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2047 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2049 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
| 2050 | bma->ip->i_d.di_nextents++; |
| 2051 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2053 | else { |
| 2054 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2055 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2057 | &i); |
| 2058 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2060 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2061 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2062 | error = xfs_btree_insert(bma->cur, &i); |
| 2063 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2065 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2067 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2068 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2069 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2070 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2071 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | rval |= tmp_rval; |
| 2073 | if (error) |
| 2074 | goto done; |
| 2075 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2076 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2077 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2078 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 2079 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2080 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2081 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2082 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2083 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | break; |
| 2085 | |
| 2086 | case 0: |
| 2087 | /* |
| 2088 | * Filling in the middle part of a previous delayed allocation. |
| 2089 | * Contiguity is impossible here. |
| 2090 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2091 | * |
| 2092 | * We start with a delayed allocation: |
| 2093 | * |
| 2094 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 2095 | * PREV @ idx |
| 2096 | * |
| 2097 | * and we are allocating: |
| 2098 | * +rrrrrrrrrrrrrrrrr+ |
| 2099 | * new |
| 2100 | * |
| 2101 | * and we set it up for insertion as: |
| 2102 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 2103 | * new |
| 2104 | * PREV @ idx LEFT RIGHT |
| 2105 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | */ |
| 2107 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2109 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2110 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 2111 | LEFT = *new; |
| 2112 | RIGHT.br_state = PREV.br_state; |
| 2113 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2114 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2115 | RIGHT.br_startoff = new_endoff; |
| 2116 | RIGHT.br_blockcount = temp2; |
| 2117 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2118 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
| 2119 | bma->ip->i_d.di_nextents++; |
| 2120 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2122 | else { |
| 2123 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2124 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2126 | &i); |
| 2127 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2129 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2130 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2131 | error = xfs_btree_insert(bma->cur, &i); |
| 2132 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2134 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2136 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2137 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2138 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2139 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2140 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | rval |= tmp_rval; |
| 2142 | if (error) |
| 2143 | goto done; |
| 2144 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2145 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 2146 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2147 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2148 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2149 | if (diff > 0) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2150 | error = xfs_mod_fdblocks(bma->ip->i_mount, |
| 2151 | -((int64_t)diff), false); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2152 | ASSERT(!error); |
| 2153 | if (error) |
| 2154 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2156 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2157 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2158 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2159 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2160 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 2161 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2162 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2163 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2164 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2165 | bma->idx++; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2166 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | break; |
| 2168 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2169 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2170 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2171 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2172 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2173 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2174 | case BMAP_LEFT_CONTIG: |
| 2175 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | /* |
| 2177 | * These cases are all impossible. |
| 2178 | */ |
| 2179 | ASSERT(0); |
| 2180 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2181 | |
| 2182 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2183 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2184 | int tmp_logflags; /* partial log flag return val */ |
| 2185 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2186 | ASSERT(bma->cur == NULL); |
| 2187 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2188 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2189 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2190 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2191 | if (error) |
| 2192 | goto done; |
| 2193 | } |
| 2194 | |
| 2195 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2196 | if (da_old || da_new) { |
| 2197 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2198 | if (bma->cur) |
| 2199 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2200 | ASSERT(temp <= da_old); |
| 2201 | if (temp < da_old) |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2202 | xfs_mod_fdblocks(bma->ip->i_mount, |
| 2203 | (int64_t)(da_old - temp), false); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2207 | if (bma->cur) |
| 2208 | bma->cur->bc_private.b.allocated = 0; |
| 2209 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2210 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | done: |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2212 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | return error; |
| 2214 | #undef LEFT |
| 2215 | #undef RIGHT |
| 2216 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2220 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | */ |
| 2222 | STATIC int /* error */ |
| 2223 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2224 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2226 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2228 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2229 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2230 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2231 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2234 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2237 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 2239 | xfs_exntst_t newext; /* new extent state */ |
| 2240 | xfs_exntst_t oldext; /* old extent state */ |
| 2241 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2242 | /* left is 0, right is 1, prev is 2 */ |
| 2243 | int rval=0; /* return value (logging flags) */ |
| 2244 | int state = 0;/* state bits, accessed thru macros */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2245 | struct xfs_mount *mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2247 | *logflagsp = 0; |
| 2248 | |
| 2249 | cur = *curp; |
| 2250 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 2251 | |
| 2252 | ASSERT(*idx >= 0); |
| 2253 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
| 2254 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2255 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2256 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | #define LEFT r[0] |
| 2259 | #define RIGHT r[1] |
| 2260 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | /* |
| 2263 | * Set up a bunch of variables to make the tests simpler. |
| 2264 | */ |
| 2265 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2266 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | xfs_bmbt_get_all(ep, &PREV); |
| 2268 | newext = new->br_state; |
| 2269 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 2270 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 2271 | ASSERT(PREV.br_state == oldext); |
| 2272 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2273 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2274 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | /* |
| 2277 | * Set flags determining what part of the previous oldext allocation |
| 2278 | * extent is being replaced by a newext allocation. |
| 2279 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2280 | if (PREV.br_startoff == new->br_startoff) |
| 2281 | state |= BMAP_LEFT_FILLING; |
| 2282 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2283 | state |= BMAP_RIGHT_FILLING; |
| 2284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | /* |
| 2286 | * Check and set flags if this segment has a left neighbor. |
| 2287 | * Don't set contiguous if the combined extent would be too large. |
| 2288 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2289 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2290 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2291 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2292 | |
| 2293 | if (isnullstartblock(LEFT.br_startblock)) |
| 2294 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2296 | |
| 2297 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2298 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2299 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 2300 | LEFT.br_state == newext && |
| 2301 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2302 | state |= BMAP_LEFT_CONTIG; |
| 2303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | /* |
| 2305 | * Check and set flags if this segment has a right neighbor. |
| 2306 | * Don't set contiguous if the combined extent would be too large. |
| 2307 | * Also check for all-three-contiguous being too large. |
| 2308 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2309 | if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2310 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2311 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2312 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2313 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2315 | |
| 2316 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2317 | new_endoff == RIGHT.br_startoff && |
| 2318 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 2319 | newext == RIGHT.br_state && |
| 2320 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2321 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2322 | BMAP_RIGHT_FILLING)) != |
| 2323 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2324 | BMAP_RIGHT_FILLING) || |
| 2325 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2326 | <= MAXEXTLEN)) |
| 2327 | state |= BMAP_RIGHT_CONTIG; |
| 2328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | /* |
| 2330 | * Switch out based on the FILLING and CONTIG state bits. |
| 2331 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2332 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2333 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2334 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2335 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | /* |
| 2337 | * Setting all of a previous oldext extent to newext. |
| 2338 | * The left and right neighbors are both contiguous with new. |
| 2339 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2340 | --*idx; |
| 2341 | |
| 2342 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2343 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2345 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2346 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2347 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2348 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | ip->i_d.di_nextents -= 2; |
| 2350 | if (cur == NULL) |
| 2351 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2352 | else { |
| 2353 | rval = XFS_ILOG_CORE; |
| 2354 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2355 | RIGHT.br_startblock, |
| 2356 | RIGHT.br_blockcount, &i))) |
| 2357 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2358 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2359 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2361 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2362 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2364 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2365 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2367 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2368 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2370 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2372 | LEFT.br_startblock, |
| 2373 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2374 | RIGHT.br_blockcount, LEFT.br_state))) |
| 2375 | goto done; |
| 2376 | } |
| 2377 | break; |
| 2378 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2379 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | /* |
| 2381 | * Setting all of a previous oldext extent to newext. |
| 2382 | * The left neighbor is contiguous, the right is not. |
| 2383 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2384 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2385 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2386 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2387 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 2388 | LEFT.br_blockcount + PREV.br_blockcount); |
| 2389 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2390 | |
| 2391 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | ip->i_d.di_nextents--; |
| 2393 | if (cur == NULL) |
| 2394 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2395 | else { |
| 2396 | rval = XFS_ILOG_CORE; |
| 2397 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2398 | PREV.br_startblock, PREV.br_blockcount, |
| 2399 | &i))) |
| 2400 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2401 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2402 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2404 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2405 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2407 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2409 | LEFT.br_startblock, |
| 2410 | LEFT.br_blockcount + PREV.br_blockcount, |
| 2411 | LEFT.br_state))) |
| 2412 | goto done; |
| 2413 | } |
| 2414 | break; |
| 2415 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2416 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | /* |
| 2418 | * Setting all of a previous oldext extent to newext. |
| 2419 | * The right neighbor is contiguous, the left is not. |
| 2420 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2421 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | xfs_bmbt_set_blockcount(ep, |
| 2423 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 2424 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2425 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2426 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | ip->i_d.di_nextents--; |
| 2428 | if (cur == NULL) |
| 2429 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2430 | else { |
| 2431 | rval = XFS_ILOG_CORE; |
| 2432 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2433 | RIGHT.br_startblock, |
| 2434 | RIGHT.br_blockcount, &i))) |
| 2435 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2436 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2437 | if ((error = xfs_btree_delete(cur, &i))) |
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); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2440 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2442 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2444 | new->br_startblock, |
| 2445 | new->br_blockcount + RIGHT.br_blockcount, |
| 2446 | newext))) |
| 2447 | goto done; |
| 2448 | } |
| 2449 | break; |
| 2450 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2451 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | /* |
| 2453 | * Setting all of a previous oldext extent to newext. |
| 2454 | * Neither the left nor right neighbors are contiguous with |
| 2455 | * the new one. |
| 2456 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2457 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2459 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | if (cur == NULL) |
| 2462 | rval = XFS_ILOG_DEXT; |
| 2463 | else { |
| 2464 | rval = 0; |
| 2465 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2466 | new->br_startblock, new->br_blockcount, |
| 2467 | &i))) |
| 2468 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2469 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2471 | new->br_startblock, new->br_blockcount, |
| 2472 | newext))) |
| 2473 | goto done; |
| 2474 | } |
| 2475 | break; |
| 2476 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2477 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | /* |
| 2479 | * Setting the first part of a previous oldext extent to newext. |
| 2480 | * The left neighbor is contiguous. |
| 2481 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2482 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 2483 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | LEFT.br_blockcount + new->br_blockcount); |
| 2485 | xfs_bmbt_set_startoff(ep, |
| 2486 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2487 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2488 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2489 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | xfs_bmbt_set_startblock(ep, |
| 2491 | new->br_startblock + new->br_blockcount); |
| 2492 | xfs_bmbt_set_blockcount(ep, |
| 2493 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2494 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2495 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2496 | --*idx; |
| 2497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | if (cur == NULL) |
| 2499 | rval = XFS_ILOG_DEXT; |
| 2500 | else { |
| 2501 | rval = 0; |
| 2502 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2503 | PREV.br_startblock, PREV.br_blockcount, |
| 2504 | &i))) |
| 2505 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2506 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | if ((error = xfs_bmbt_update(cur, |
| 2508 | PREV.br_startoff + new->br_blockcount, |
| 2509 | PREV.br_startblock + new->br_blockcount, |
| 2510 | PREV.br_blockcount - new->br_blockcount, |
| 2511 | oldext))) |
| 2512 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2513 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2515 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | LEFT.br_startblock, |
| 2517 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2518 | LEFT.br_state); |
| 2519 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | goto done; |
| 2521 | } |
| 2522 | break; |
| 2523 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2524 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | /* |
| 2526 | * Setting the first part of a previous oldext extent to newext. |
| 2527 | * The left neighbor is not contiguous. |
| 2528 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2529 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 2531 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 2532 | xfs_bmbt_set_blockcount(ep, |
| 2533 | PREV.br_blockcount - new->br_blockcount); |
| 2534 | xfs_bmbt_set_startblock(ep, |
| 2535 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2536 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2537 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2538 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | ip->i_d.di_nextents++; |
| 2540 | if (cur == NULL) |
| 2541 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2542 | else { |
| 2543 | rval = XFS_ILOG_CORE; |
| 2544 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2545 | PREV.br_startblock, PREV.br_blockcount, |
| 2546 | &i))) |
| 2547 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2548 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | if ((error = xfs_bmbt_update(cur, |
| 2550 | PREV.br_startoff + new->br_blockcount, |
| 2551 | PREV.br_startblock + new->br_blockcount, |
| 2552 | PREV.br_blockcount - new->br_blockcount, |
| 2553 | oldext))) |
| 2554 | goto done; |
| 2555 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2556 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2558 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | } |
| 2560 | break; |
| 2561 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2562 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | /* |
| 2564 | * Setting the last part of a previous oldext extent to newext. |
| 2565 | * The right neighbor is contiguous with the new allocation. |
| 2566 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2567 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | xfs_bmbt_set_blockcount(ep, |
| 2569 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2570 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2571 | |
| 2572 | ++*idx; |
| 2573 | |
| 2574 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2575 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2576 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2578 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | if (cur == NULL) |
| 2581 | rval = XFS_ILOG_DEXT; |
| 2582 | else { |
| 2583 | rval = 0; |
| 2584 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2585 | PREV.br_startblock, |
| 2586 | PREV.br_blockcount, &i))) |
| 2587 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2588 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2590 | PREV.br_startblock, |
| 2591 | PREV.br_blockcount - new->br_blockcount, |
| 2592 | oldext))) |
| 2593 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2594 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | goto done; |
| 2596 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2597 | new->br_startblock, |
| 2598 | new->br_blockcount + RIGHT.br_blockcount, |
| 2599 | newext))) |
| 2600 | goto done; |
| 2601 | } |
| 2602 | break; |
| 2603 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2604 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | /* |
| 2606 | * Setting the last part of a previous oldext extent to newext. |
| 2607 | * The right neighbor is not contiguous. |
| 2608 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2609 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | xfs_bmbt_set_blockcount(ep, |
| 2611 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2612 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2613 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2614 | ++*idx; |
| 2615 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | ip->i_d.di_nextents++; |
| 2618 | if (cur == NULL) |
| 2619 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2620 | else { |
| 2621 | rval = XFS_ILOG_CORE; |
| 2622 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2623 | PREV.br_startblock, PREV.br_blockcount, |
| 2624 | &i))) |
| 2625 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2626 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2628 | PREV.br_startblock, |
| 2629 | PREV.br_blockcount - new->br_blockcount, |
| 2630 | oldext))) |
| 2631 | goto done; |
| 2632 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2633 | new->br_startblock, new->br_blockcount, |
| 2634 | &i))) |
| 2635 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2636 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2638 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2640 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | } |
| 2642 | break; |
| 2643 | |
| 2644 | case 0: |
| 2645 | /* |
| 2646 | * Setting the middle part of a previous oldext extent to |
| 2647 | * newext. Contiguity is impossible here. |
| 2648 | * One extent becomes three extents. |
| 2649 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2650 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | xfs_bmbt_set_blockcount(ep, |
| 2652 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2653 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | r[0] = *new; |
| 2656 | r[1].br_startoff = new_endoff; |
| 2657 | r[1].br_blockcount = |
| 2658 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 2659 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 2660 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2661 | |
| 2662 | ++*idx; |
| 2663 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 2664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | ip->i_d.di_nextents += 2; |
| 2666 | if (cur == NULL) |
| 2667 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2668 | else { |
| 2669 | rval = XFS_ILOG_CORE; |
| 2670 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2671 | PREV.br_startblock, PREV.br_blockcount, |
| 2672 | &i))) |
| 2673 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2674 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | /* new right extent - oldext */ |
| 2676 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 2677 | r[1].br_startblock, r[1].br_blockcount, |
| 2678 | r[1].br_state))) |
| 2679 | goto done; |
| 2680 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 2682 | cur->bc_rec.b.br_blockcount = |
| 2683 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2684 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2686 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2687 | /* |
| 2688 | * Reset the cursor to the position of the new extent |
| 2689 | * we are about to insert as we can't trust it after |
| 2690 | * the previous insert. |
| 2691 | */ |
| 2692 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2693 | new->br_startblock, new->br_blockcount, |
| 2694 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2696 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2698 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2699 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2701 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | } |
| 2703 | break; |
| 2704 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2705 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2706 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2707 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2708 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2709 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2710 | case BMAP_LEFT_CONTIG: |
| 2711 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | /* |
| 2713 | * These cases are all impossible. |
| 2714 | */ |
| 2715 | ASSERT(0); |
| 2716 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2717 | |
| 2718 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2719 | if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2720 | int tmp_logflags; /* partial log flag return val */ |
| 2721 | |
| 2722 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2723 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2724 | 0, &tmp_logflags, XFS_DATA_FORK); |
| 2725 | *logflagsp |= tmp_logflags; |
| 2726 | if (error) |
| 2727 | goto done; |
| 2728 | } |
| 2729 | |
| 2730 | /* clear out the allocated field, done with it now in any case. */ |
| 2731 | if (cur) { |
| 2732 | cur->bc_private.b.allocated = 0; |
| 2733 | *curp = cur; |
| 2734 | } |
| 2735 | |
| 2736 | xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2738 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | return error; |
| 2740 | #undef LEFT |
| 2741 | #undef RIGHT |
| 2742 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2746 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2748 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | xfs_bmap_add_extent_hole_delay( |
| 2750 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2751 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2752 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2754 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2756 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2757 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2758 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 2759 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2760 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2762 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2764 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | /* |
| 2767 | * Check and set flags if this segment has a left neighbor |
| 2768 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2769 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2770 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2771 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2772 | |
| 2773 | if (isnullstartblock(left.br_startblock)) |
| 2774 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | /* |
| 2778 | * Check and set flags if the current (right) segment exists. |
| 2779 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2780 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2781 | if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2782 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2783 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2784 | |
| 2785 | if (isnullstartblock(right.br_startblock)) |
| 2786 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2787 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | /* |
| 2790 | * Set contiguity flags on the left and right neighbors. |
| 2791 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2792 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2793 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2794 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2795 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2796 | state |= BMAP_LEFT_CONTIG; |
| 2797 | |
| 2798 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2799 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2800 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2801 | (!(state & BMAP_LEFT_CONTIG) || |
| 2802 | (left.br_blockcount + new->br_blockcount + |
| 2803 | right.br_blockcount <= MAXEXTLEN))) |
| 2804 | state |= BMAP_RIGHT_CONTIG; |
| 2805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | /* |
| 2807 | * Switch out based on the contiguity flags. |
| 2808 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2809 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2810 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | /* |
| 2812 | * New allocation is contiguous with delayed allocations |
| 2813 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2814 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2816 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | temp = left.br_blockcount + new->br_blockcount + |
| 2818 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2819 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2820 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2821 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2822 | oldlen = startblockval(left.br_startblock) + |
| 2823 | startblockval(new->br_startblock) + |
| 2824 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2826 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2827 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2828 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2829 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2830 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | break; |
| 2832 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2833 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | /* |
| 2835 | * New allocation is contiguous with a delayed allocation |
| 2836 | * on the left. |
| 2837 | * Merge the new allocation with the left neighbor. |
| 2838 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2839 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2841 | |
| 2842 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2843 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2844 | oldlen = startblockval(left.br_startblock) + |
| 2845 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2847 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2848 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2849 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | break; |
| 2851 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2852 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | /* |
| 2854 | * New allocation is contiguous with a delayed allocation |
| 2855 | * on the right. |
| 2856 | * Merge the new allocation with the right neighbor. |
| 2857 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2858 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2860 | oldlen = startblockval(new->br_startblock) + |
| 2861 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2863 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 2864 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2865 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2866 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | break; |
| 2868 | |
| 2869 | case 0: |
| 2870 | /* |
| 2871 | * New allocation is not contiguous with another |
| 2872 | * delayed allocation. |
| 2873 | * Insert a new entry. |
| 2874 | */ |
| 2875 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2876 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | break; |
| 2878 | } |
| 2879 | if (oldlen != newlen) { |
| 2880 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2881 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2882 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | /* |
| 2884 | * Nothing to do for disk quota accounting here. |
| 2885 | */ |
| 2886 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2890 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | */ |
| 2892 | STATIC int /* error */ |
| 2893 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2894 | struct xfs_bmalloca *bma, |
| 2895 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | { |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2897 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | int i; /* temp state */ |
| 2900 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2901 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2902 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2903 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | int state; /* state bits, accessed thru macros */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2905 | struct xfs_mount *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 2907 | mp = bma->ip->i_mount; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2908 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2909 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2910 | ASSERT(bma->idx >= 0); |
| 2911 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2912 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2913 | ASSERT(!bma->cur || |
| 2914 | !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2915 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2916 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2917 | |
| 2918 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2919 | if (whichfork == XFS_ATTR_FORK) |
| 2920 | state |= BMAP_ATTRFORK; |
| 2921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | /* |
| 2923 | * Check and set flags if this segment has a left neighbor. |
| 2924 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2925 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2926 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2927 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2928 | if (isnullstartblock(left.br_startblock)) |
| 2929 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | /* |
| 2933 | * Check and set flags if this segment has a current value. |
| 2934 | * Not true if we're inserting into the "hole" at eof. |
| 2935 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2936 | if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2937 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2938 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2939 | if (isnullstartblock(right.br_startblock)) |
| 2940 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | /* |
| 2944 | * We're inserting a real allocation between "left" and "right". |
| 2945 | * Set the contiguity flags. Don't let extents get too large. |
| 2946 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2947 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2948 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2949 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2950 | left.br_state == new->br_state && |
| 2951 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2952 | state |= BMAP_LEFT_CONTIG; |
| 2953 | |
| 2954 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2955 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2956 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2957 | new->br_state == right.br_state && |
| 2958 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2959 | (!(state & BMAP_LEFT_CONTIG) || |
| 2960 | left.br_blockcount + new->br_blockcount + |
| 2961 | right.br_blockcount <= MAXEXTLEN)) |
| 2962 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2964 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | /* |
| 2966 | * Select which case we're in here, and implement it. |
| 2967 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2968 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2969 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | /* |
| 2971 | * New allocation is contiguous with real allocations on the |
| 2972 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2973 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2975 | --bma->idx; |
| 2976 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2977 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | left.br_blockcount + new->br_blockcount + |
| 2979 | right.br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2980 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2981 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2982 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2983 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2984 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 2985 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1); |
| 2986 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2987 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2988 | } else { |
| 2989 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2990 | error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff, |
| 2991 | right.br_startblock, right.br_blockcount, |
| 2992 | &i); |
| 2993 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2994 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2995 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2996 | error = xfs_btree_delete(bma->cur, &i); |
| 2997 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2998 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2999 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3000 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 3001 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3002 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3003 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3004 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3005 | left.br_startblock, |
| 3006 | left.br_blockcount + |
| 3007 | new->br_blockcount + |
| 3008 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3009 | left.br_state); |
| 3010 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3011 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3013 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3015 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | /* |
| 3017 | * New allocation is contiguous with a real allocation |
| 3018 | * on the left. |
| 3019 | * Merge the new allocation with the left neighbor. |
| 3020 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3021 | --bma->idx; |
| 3022 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3023 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3025 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3026 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3027 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3028 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3029 | } else { |
| 3030 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3031 | error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff, |
| 3032 | left.br_startblock, left.br_blockcount, |
| 3033 | &i); |
| 3034 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3035 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3036 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3037 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3038 | left.br_startblock, |
| 3039 | left.br_blockcount + |
| 3040 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3041 | left.br_state); |
| 3042 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3043 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3045 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3047 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | /* |
| 3049 | * New allocation is contiguous with a real allocation |
| 3050 | * on the right. |
| 3051 | * Merge the new allocation with the right neighbor. |
| 3052 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3053 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3054 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 3055 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | new->br_blockcount + right.br_blockcount, |
| 3057 | right.br_state); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3058 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3059 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3060 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3061 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3062 | } else { |
| 3063 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3064 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3065 | right.br_startoff, |
| 3066 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3067 | right.br_blockcount, &i); |
| 3068 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3069 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3070 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3071 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3072 | new->br_startblock, |
| 3073 | new->br_blockcount + |
| 3074 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3075 | right.br_state); |
| 3076 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3077 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3079 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3080 | |
| 3081 | case 0: |
| 3082 | /* |
| 3083 | * New allocation is not contiguous with another |
| 3084 | * real allocation. |
| 3085 | * Insert a new entry. |
| 3086 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3087 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 3088 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 3089 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1); |
| 3090 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3091 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3092 | } else { |
| 3093 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3094 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3095 | new->br_startoff, |
| 3096 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3097 | new->br_blockcount, &i); |
| 3098 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3099 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3100 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3101 | bma->cur->bc_rec.b.br_state = new->br_state; |
| 3102 | error = xfs_btree_insert(bma->cur, &i); |
| 3103 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3104 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3105 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3107 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3109 | |
| 3110 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3111 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3112 | int tmp_logflags; /* partial log flag return val */ |
| 3113 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3114 | ASSERT(bma->cur == NULL); |
| 3115 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3116 | bma->firstblock, bma->dfops, &bma->cur, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3117 | 0, &tmp_logflags, whichfork); |
| 3118 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3119 | if (error) |
| 3120 | goto done; |
| 3121 | } |
| 3122 | |
| 3123 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3124 | if (bma->cur) |
| 3125 | bma->cur->bc_private.b.allocated = 0; |
| 3126 | |
| 3127 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3128 | done: |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3129 | bma->logflags |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3130 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | } |
| 3132 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3133 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 3134 | * Functions used in the extent read, allocate and remove paths |
| 3135 | */ |
| 3136 | |
| 3137 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3138 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 3139 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3140 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3141 | xfs_bmap_extsize_align( |
| 3142 | xfs_mount_t *mp, |
| 3143 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 3144 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 3145 | xfs_extlen_t extsz, /* align to this extent size */ |
| 3146 | int rt, /* is this a realtime inode? */ |
| 3147 | int eof, /* is extent at end-of-file? */ |
| 3148 | int delay, /* creating delalloc extent? */ |
| 3149 | int convert, /* overwriting unwritten extent? */ |
| 3150 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 3151 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 3152 | { |
| 3153 | xfs_fileoff_t orig_off; /* original offset */ |
| 3154 | xfs_extlen_t orig_alen; /* original length */ |
| 3155 | xfs_fileoff_t orig_end; /* original off+len */ |
| 3156 | xfs_fileoff_t nexto; /* next file offset */ |
| 3157 | xfs_fileoff_t prevo; /* previous file offset */ |
| 3158 | xfs_fileoff_t align_off; /* temp for offset */ |
| 3159 | xfs_extlen_t align_alen; /* temp for length */ |
| 3160 | xfs_extlen_t temp; /* temp for calculations */ |
| 3161 | |
| 3162 | if (convert) |
| 3163 | return 0; |
| 3164 | |
| 3165 | orig_off = align_off = *offp; |
| 3166 | orig_alen = align_alen = *lenp; |
| 3167 | orig_end = orig_off + orig_alen; |
| 3168 | |
| 3169 | /* |
| 3170 | * If this request overlaps an existing extent, then don't |
| 3171 | * attempt to perform any additional alignment. |
| 3172 | */ |
| 3173 | if (!delay && !eof && |
| 3174 | (orig_off >= gotp->br_startoff) && |
| 3175 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 3176 | return 0; |
| 3177 | } |
| 3178 | |
| 3179 | /* |
| 3180 | * If the file offset is unaligned vs. the extent size |
| 3181 | * we need to align it. This will be possible unless |
| 3182 | * the file was previously written with a kernel that didn't |
| 3183 | * perform this alignment, or if a truncate shot us in the |
| 3184 | * foot. |
| 3185 | */ |
| 3186 | temp = do_mod(orig_off, extsz); |
| 3187 | if (temp) { |
| 3188 | align_alen += temp; |
| 3189 | align_off -= temp; |
| 3190 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3191 | |
| 3192 | /* Same adjustment for the end of the requested area. */ |
| 3193 | temp = (align_alen % extsz); |
| 3194 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3195 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3196 | |
| 3197 | /* |
| 3198 | * For large extent hint sizes, the aligned extent might be larger than |
| 3199 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 3200 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 3201 | * short allocation just fine, so it is safe to do this. We only want to |
| 3202 | * do it when we are forced to, though, because it means more allocation |
| 3203 | * operations are required. |
| 3204 | */ |
| 3205 | while (align_alen > MAXEXTLEN) |
| 3206 | align_alen -= extsz; |
| 3207 | ASSERT(align_alen <= MAXEXTLEN); |
| 3208 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3209 | /* |
| 3210 | * If the previous block overlaps with this proposed allocation |
| 3211 | * then move the start forward without adjusting the length. |
| 3212 | */ |
| 3213 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3214 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3215 | prevo = prevp->br_startoff; |
| 3216 | else |
| 3217 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3218 | } else |
| 3219 | prevo = 0; |
| 3220 | if (align_off != orig_off && align_off < prevo) |
| 3221 | align_off = prevo; |
| 3222 | /* |
| 3223 | * If the next block overlaps with this proposed allocation |
| 3224 | * then move the start back without adjusting the length, |
| 3225 | * but not before offset 0. |
| 3226 | * This may of course make the start overlap previous block, |
| 3227 | * and if we hit the offset 0 limit then the next block |
| 3228 | * can still overlap too. |
| 3229 | */ |
| 3230 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3231 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3232 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3233 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3234 | else |
| 3235 | nexto = gotp->br_startoff; |
| 3236 | } else |
| 3237 | nexto = NULLFILEOFF; |
| 3238 | if (!eof && |
| 3239 | align_off + align_alen != orig_end && |
| 3240 | align_off + align_alen > nexto) |
| 3241 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3242 | /* |
| 3243 | * If we're now overlapping the next or previous extent that |
| 3244 | * means we can't fit an extsz piece in this hole. Just move |
| 3245 | * the start forward to the first valid spot and set |
| 3246 | * the length so we hit the end. |
| 3247 | */ |
| 3248 | if (align_off != orig_off && align_off < prevo) |
| 3249 | align_off = prevo; |
| 3250 | if (align_off + align_alen != orig_end && |
| 3251 | align_off + align_alen > nexto && |
| 3252 | nexto != NULLFILEOFF) { |
| 3253 | ASSERT(nexto > prevo); |
| 3254 | align_alen = nexto - align_off; |
| 3255 | } |
| 3256 | |
| 3257 | /* |
| 3258 | * If realtime, and the result isn't a multiple of the realtime |
| 3259 | * extent size we need to remove blocks until it is. |
| 3260 | */ |
| 3261 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3262 | /* |
| 3263 | * We're not covering the original request, or |
| 3264 | * we won't be able to once we fix the length. |
| 3265 | */ |
| 3266 | if (orig_off < align_off || |
| 3267 | orig_end > align_off + align_alen || |
| 3268 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3269 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3270 | /* |
| 3271 | * Try to fix it by moving the start up. |
| 3272 | */ |
| 3273 | if (align_off + temp <= orig_off) { |
| 3274 | align_alen -= temp; |
| 3275 | align_off += temp; |
| 3276 | } |
| 3277 | /* |
| 3278 | * Try to fix it by moving the end in. |
| 3279 | */ |
| 3280 | else if (align_off + align_alen - temp >= orig_end) |
| 3281 | align_alen -= temp; |
| 3282 | /* |
| 3283 | * Set the start to the minimum then trim the length. |
| 3284 | */ |
| 3285 | else { |
| 3286 | align_alen -= orig_off - align_off; |
| 3287 | align_off = orig_off; |
| 3288 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3289 | } |
| 3290 | /* |
| 3291 | * Result doesn't cover the request, fail it. |
| 3292 | */ |
| 3293 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3294 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3295 | } else { |
| 3296 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3297 | /* see MAXEXTLEN handling above */ |
| 3298 | ASSERT(orig_end <= align_off + align_alen || |
| 3299 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | #ifdef DEBUG |
| 3303 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3304 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3305 | if (prevp->br_startoff != NULLFILEOFF) |
| 3306 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3307 | #endif |
| 3308 | |
| 3309 | *lenp = align_alen; |
| 3310 | *offp = align_off; |
| 3311 | return 0; |
| 3312 | } |
| 3313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3315 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3316 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3317 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3318 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | { |
| 3320 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3322 | xfs_mount_t *mp; /* mount point structure */ |
| 3323 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3324 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3325 | |
| 3326 | #define ISVALID(x,y) \ |
| 3327 | (rt ? \ |
| 3328 | (x) < mp->m_sb.sb_rblocks : \ |
| 3329 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3330 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3331 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3334 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3336 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3337 | /* |
| 3338 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3339 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3341 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3342 | !isnullstartblock(ap->prev.br_startblock) && |
| 3343 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3344 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3345 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | /* |
| 3347 | * Adjust for the gap between prevp and us. |
| 3348 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3349 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3350 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3351 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3352 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3353 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | } |
| 3355 | /* |
| 3356 | * If not at eof, then compare the two neighbor blocks. |
| 3357 | * Figure out whether either one gives us a good starting point, |
| 3358 | * and pick the better one. |
| 3359 | */ |
| 3360 | else if (!ap->eof) { |
| 3361 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3362 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3363 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3364 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3365 | |
| 3366 | /* |
| 3367 | * If there's a previous (left) block, select a requested |
| 3368 | * start block based on it. |
| 3369 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3370 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3371 | !isnullstartblock(ap->prev.br_startblock) && |
| 3372 | (prevbno = ap->prev.br_startblock + |
| 3373 | ap->prev.br_blockcount) && |
| 3374 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | /* |
| 3376 | * Calculate gap to end of previous block. |
| 3377 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3378 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3379 | (ap->prev.br_startoff + |
| 3380 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | /* |
| 3382 | * Figure the startblock based on the previous block's |
| 3383 | * end and the gap size. |
| 3384 | * Heuristic! |
| 3385 | * If the gap is large relative to the piece we're |
| 3386 | * allocating, or using it gives us an invalid block |
| 3387 | * number, then just use the end of the previous block. |
| 3388 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3389 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3391 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | prevbno += adjust; |
| 3393 | else |
| 3394 | prevdiff += adjust; |
| 3395 | /* |
| 3396 | * If the firstblock forbids it, can't use it, |
| 3397 | * must use default. |
| 3398 | */ |
| 3399 | if (!rt && !nullfb && |
| 3400 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3401 | prevbno = NULLFSBLOCK; |
| 3402 | } |
| 3403 | /* |
| 3404 | * No previous block or can't follow it, just default. |
| 3405 | */ |
| 3406 | else |
| 3407 | prevbno = NULLFSBLOCK; |
| 3408 | /* |
| 3409 | * If there's a following (right) block, select a requested |
| 3410 | * start block based on it. |
| 3411 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3412 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | /* |
| 3414 | * Calculate gap to start of next block. |
| 3415 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3416 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | /* |
| 3418 | * Figure the startblock based on the next block's |
| 3419 | * start and the gap size. |
| 3420 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3421 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | /* |
| 3423 | * Heuristic! |
| 3424 | * If the gap is large relative to the piece we're |
| 3425 | * allocating, or using it gives us an invalid block |
| 3426 | * number, then just use the start of the next block |
| 3427 | * offset by our length. |
| 3428 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3429 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3431 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3432 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3433 | gotbno -= ap->length; |
| 3434 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | } else |
| 3436 | gotdiff += adjust; |
| 3437 | /* |
| 3438 | * If the firstblock forbids it, can't use it, |
| 3439 | * must use default. |
| 3440 | */ |
| 3441 | if (!rt && !nullfb && |
| 3442 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3443 | gotbno = NULLFSBLOCK; |
| 3444 | } |
| 3445 | /* |
| 3446 | * No next block, just default. |
| 3447 | */ |
| 3448 | else |
| 3449 | gotbno = NULLFSBLOCK; |
| 3450 | /* |
| 3451 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3452 | * 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] | 3453 | */ |
| 3454 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3455 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3457 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3459 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3461 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3462 | } |
| 3463 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3464 | static int |
| 3465 | xfs_bmap_longest_free_extent( |
| 3466 | struct xfs_trans *tp, |
| 3467 | xfs_agnumber_t ag, |
| 3468 | xfs_extlen_t *blen, |
| 3469 | int *notinit) |
| 3470 | { |
| 3471 | struct xfs_mount *mp = tp->t_mountp; |
| 3472 | struct xfs_perag *pag; |
| 3473 | xfs_extlen_t longest; |
| 3474 | int error = 0; |
| 3475 | |
| 3476 | pag = xfs_perag_get(mp, ag); |
| 3477 | if (!pag->pagf_init) { |
| 3478 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3479 | if (error) |
| 3480 | goto out; |
| 3481 | |
| 3482 | if (!pag->pagf_init) { |
| 3483 | *notinit = 1; |
| 3484 | goto out; |
| 3485 | } |
| 3486 | } |
| 3487 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 3488 | longest = xfs_alloc_longest_free_extent(mp, pag, |
Dave Chinner | 496817b | 2015-06-22 10:13:30 +1000 | [diff] [blame] | 3489 | xfs_alloc_min_freelist(mp, pag)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3490 | if (*blen < longest) |
| 3491 | *blen = longest; |
| 3492 | |
| 3493 | out: |
| 3494 | xfs_perag_put(pag); |
| 3495 | return error; |
| 3496 | } |
| 3497 | |
| 3498 | static void |
| 3499 | xfs_bmap_select_minlen( |
| 3500 | struct xfs_bmalloca *ap, |
| 3501 | struct xfs_alloc_arg *args, |
| 3502 | xfs_extlen_t *blen, |
| 3503 | int notinit) |
| 3504 | { |
| 3505 | if (notinit || *blen < ap->minlen) { |
| 3506 | /* |
| 3507 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3508 | * there is space for this request. |
| 3509 | */ |
| 3510 | args->minlen = ap->minlen; |
| 3511 | } else if (*blen < args->maxlen) { |
| 3512 | /* |
| 3513 | * If the best seen length is less than the request length, |
| 3514 | * use the best as the minimum. |
| 3515 | */ |
| 3516 | args->minlen = *blen; |
| 3517 | } else { |
| 3518 | /* |
| 3519 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3520 | * as the minimum. |
| 3521 | */ |
| 3522 | args->minlen = args->maxlen; |
| 3523 | } |
| 3524 | } |
| 3525 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3526 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3527 | xfs_bmap_btalloc_nullfb( |
| 3528 | struct xfs_bmalloca *ap, |
| 3529 | struct xfs_alloc_arg *args, |
| 3530 | xfs_extlen_t *blen) |
| 3531 | { |
| 3532 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3533 | xfs_agnumber_t ag, startag; |
| 3534 | int notinit = 0; |
| 3535 | int error; |
| 3536 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3537 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3538 | args->total = ap->total; |
| 3539 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3540 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3541 | if (startag == NULLAGNUMBER) |
| 3542 | startag = ag = 0; |
| 3543 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3544 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3545 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3546 | ¬init); |
| 3547 | if (error) |
| 3548 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3549 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3550 | if (++ag == mp->m_sb.sb_agcount) |
| 3551 | ag = 0; |
| 3552 | if (ag == startag) |
| 3553 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3554 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3555 | |
| 3556 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3557 | return 0; |
| 3558 | } |
| 3559 | |
| 3560 | STATIC int |
| 3561 | xfs_bmap_btalloc_filestreams( |
| 3562 | struct xfs_bmalloca *ap, |
| 3563 | struct xfs_alloc_arg *args, |
| 3564 | xfs_extlen_t *blen) |
| 3565 | { |
| 3566 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3567 | xfs_agnumber_t ag; |
| 3568 | int notinit = 0; |
| 3569 | int error; |
| 3570 | |
| 3571 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3572 | args->total = ap->total; |
| 3573 | |
| 3574 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3575 | if (ag == NULLAGNUMBER) |
| 3576 | ag = 0; |
| 3577 | |
| 3578 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3579 | if (error) |
| 3580 | return error; |
| 3581 | |
| 3582 | if (*blen < args->maxlen) { |
| 3583 | error = xfs_filestream_new_ag(ap, &ag); |
| 3584 | if (error) |
| 3585 | return error; |
| 3586 | |
| 3587 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3588 | ¬init); |
| 3589 | if (error) |
| 3590 | return error; |
| 3591 | |
| 3592 | } |
| 3593 | |
| 3594 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3595 | |
| 3596 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3597 | * Set the failure fallback case to look in the selected AG as stream |
| 3598 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3599 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3600 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3601 | return 0; |
| 3602 | } |
| 3603 | |
| 3604 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3605 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3606 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3607 | { |
| 3608 | xfs_mount_t *mp; /* mount point structure */ |
| 3609 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 3610 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3611 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3612 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3613 | xfs_alloc_arg_t args; |
| 3614 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3615 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3616 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3617 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3618 | int tryagain; |
| 3619 | int error; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3620 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3621 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3622 | ASSERT(ap->length); |
| 3623 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3624 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3625 | |
| 3626 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3627 | stripe_align = 0; |
| 3628 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3629 | stripe_align = mp->m_swidth; |
| 3630 | else if (mp->m_dalign) |
| 3631 | stripe_align = mp->m_dalign; |
| 3632 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3633 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3634 | if (unlikely(align)) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3635 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3636 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3637 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3638 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3639 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3640 | } |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3641 | |
| 3642 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3643 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3644 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3645 | if (nullfb) { |
| 3646 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 3647 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3648 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3649 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3650 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3651 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3652 | } |
| 3653 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3654 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3655 | |
| 3656 | xfs_bmap_adjacent(ap); |
| 3657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3658 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3659 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | * it's in the right allocation group. |
| 3661 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3662 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | ; |
| 3664 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3665 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | * Normal allocation, done through xfs_alloc_vextent. |
| 3668 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3669 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3670 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3671 | args.tp = ap->tp; |
| 3672 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3673 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame^] | 3674 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3675 | |
| 3676 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3677 | args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3678 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3679 | blen = 0; |
| 3680 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3681 | /* |
| 3682 | * Search for an allocation group with a single extent large |
| 3683 | * enough for the request. If one isn't found, then adjust |
| 3684 | * the minimum allocation size to the largest space found. |
| 3685 | */ |
| 3686 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 3687 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3688 | else |
| 3689 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3690 | if (error) |
| 3691 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3692 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3693 | if (xfs_inode_is_filestream(ap->ip)) |
| 3694 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3695 | else |
| 3696 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3697 | args.total = args.minlen = ap->minlen; |
| 3698 | } else { |
| 3699 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3700 | args.total = ap->total; |
| 3701 | args.minlen = ap->minlen; |
| 3702 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3703 | /* apply extent size hints if obtained earlier */ |
| 3704 | if (unlikely(align)) { |
| 3705 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3706 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3707 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3708 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3709 | args.prod = 1; |
| 3710 | args.mod = 0; |
| 3711 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3712 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3713 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3714 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3715 | } |
| 3716 | /* |
| 3717 | * If we are not low on available data blocks, and the |
| 3718 | * underlying logical volume manager is a stripe, and |
| 3719 | * the file offset is zero then try to allocate data |
| 3720 | * blocks on stripe unit boundary. |
| 3721 | * NOTE: ap->aeof is only set if the allocation length |
| 3722 | * is >= the stripe unit and the allocation offset is |
| 3723 | * at the end of file. |
| 3724 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3725 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3726 | if (!ap->offset) { |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3727 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3728 | atype = args.type; |
| 3729 | isaligned = 1; |
| 3730 | /* |
| 3731 | * Adjust for alignment |
| 3732 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3733 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3734 | args.minlen = blen - args.alignment; |
| 3735 | args.minalignslop = 0; |
| 3736 | } else { |
| 3737 | /* |
| 3738 | * First try an exact bno allocation. |
| 3739 | * If it fails then do a near or start bno |
| 3740 | * allocation with alignment turned on. |
| 3741 | */ |
| 3742 | atype = args.type; |
| 3743 | tryagain = 1; |
| 3744 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3745 | args.alignment = 1; |
| 3746 | /* |
| 3747 | * Compute the minlen+alignment for the |
| 3748 | * next case. Set slop so that the value |
| 3749 | * of minlen+alignment+slop doesn't go up |
| 3750 | * between the calls. |
| 3751 | */ |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3752 | if (blen > stripe_align && blen <= args.maxlen) |
| 3753 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3754 | else |
| 3755 | nextminlen = args.minlen; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3756 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3757 | args.minalignslop = |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3758 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3759 | args.minlen - 1; |
| 3760 | else |
| 3761 | args.minalignslop = 0; |
| 3762 | } |
| 3763 | } else { |
| 3764 | args.alignment = 1; |
| 3765 | args.minalignslop = 0; |
| 3766 | } |
| 3767 | args.minleft = ap->minleft; |
| 3768 | args.wasdel = ap->wasdel; |
| 3769 | args.isfl = 0; |
| 3770 | args.userdata = ap->userdata; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3771 | if (ap->userdata & XFS_ALLOC_USERDATA_ZERO) |
| 3772 | args.ip = ap->ip; |
| 3773 | |
| 3774 | error = xfs_alloc_vextent(&args); |
| 3775 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3776 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3777 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3778 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3779 | /* |
| 3780 | * Exact allocation failed. Now try with alignment |
| 3781 | * turned on. |
| 3782 | */ |
| 3783 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3784 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3785 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3786 | args.minlen = nextminlen; |
| 3787 | args.minalignslop = 0; |
| 3788 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | if ((error = xfs_alloc_vextent(&args))) |
| 3790 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3791 | } |
| 3792 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3793 | /* |
| 3794 | * allocation failed, so turn off alignment and |
| 3795 | * try again. |
| 3796 | */ |
| 3797 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3798 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3799 | args.alignment = 0; |
| 3800 | if ((error = xfs_alloc_vextent(&args))) |
| 3801 | return error; |
| 3802 | } |
| 3803 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3804 | args.minlen > ap->minlen) { |
| 3805 | args.minlen = ap->minlen; |
| 3806 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3807 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3808 | if ((error = xfs_alloc_vextent(&args))) |
| 3809 | return error; |
| 3810 | } |
| 3811 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3812 | args.fsbno = 0; |
| 3813 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3814 | args.total = ap->minlen; |
| 3815 | args.minleft = 0; |
| 3816 | if ((error = xfs_alloc_vextent(&args))) |
| 3817 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3818 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3819 | } |
| 3820 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3821 | /* |
| 3822 | * check the allocation happened at the same or higher AG than |
| 3823 | * the first block that was allocated. |
| 3824 | */ |
| 3825 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
| 3826 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == |
| 3827 | XFS_FSB_TO_AGNO(mp, args.fsbno) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3828 | (ap->dfops->dop_low && |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3829 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < |
| 3830 | XFS_FSB_TO_AGNO(mp, args.fsbno))); |
| 3831 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3832 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3833 | if (*ap->firstblock == NULLFSBLOCK) |
| 3834 | *ap->firstblock = args.fsbno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3835 | ASSERT(nullfb || fb_agno == args.agno || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3836 | (ap->dfops->dop_low && fb_agno < args.agno)); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3837 | ap->length = args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3838 | ap->ip->i_d.di_nblocks += args.len; |
| 3839 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3840 | if (ap->wasdel) |
| 3841 | ap->ip->i_delayed_blks -= args.len; |
| 3842 | /* |
| 3843 | * Adjust the disk quota also. This was reserved |
| 3844 | * earlier. |
| 3845 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3846 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3847 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3848 | XFS_TRANS_DQ_BCOUNT, |
| 3849 | (long) args.len); |
| 3850 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3851 | ap->blkno = NULLFSBLOCK; |
| 3852 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | } |
| 3854 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | /* |
| 3858 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3859 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3860 | */ |
| 3861 | STATIC int |
| 3862 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3863 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3864 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3865 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3866 | return xfs_bmap_rtalloc(ap); |
| 3867 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3871 | * Trim the returned map to the required bounds |
| 3872 | */ |
| 3873 | STATIC void |
| 3874 | xfs_bmapi_trim_map( |
| 3875 | struct xfs_bmbt_irec *mval, |
| 3876 | struct xfs_bmbt_irec *got, |
| 3877 | xfs_fileoff_t *bno, |
| 3878 | xfs_filblks_t len, |
| 3879 | xfs_fileoff_t obno, |
| 3880 | xfs_fileoff_t end, |
| 3881 | int n, |
| 3882 | int flags) |
| 3883 | { |
| 3884 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3885 | got->br_startoff + got->br_blockcount <= obno) { |
| 3886 | *mval = *got; |
| 3887 | if (isnullstartblock(got->br_startblock)) |
| 3888 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3889 | return; |
| 3890 | } |
| 3891 | |
| 3892 | if (obno > *bno) |
| 3893 | *bno = obno; |
| 3894 | ASSERT((*bno >= obno) || (n == 0)); |
| 3895 | ASSERT(*bno < end); |
| 3896 | mval->br_startoff = *bno; |
| 3897 | if (isnullstartblock(got->br_startblock)) |
| 3898 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3899 | else |
| 3900 | mval->br_startblock = got->br_startblock + |
| 3901 | (*bno - got->br_startoff); |
| 3902 | /* |
| 3903 | * Return the minimum of what we got and what we asked for for |
| 3904 | * the length. We can use the len variable here because it is |
| 3905 | * modified below and we could have been there before coming |
| 3906 | * here if the first part of the allocation didn't overlap what |
| 3907 | * was asked for. |
| 3908 | */ |
| 3909 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3910 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3911 | mval->br_state = got->br_state; |
| 3912 | ASSERT(mval->br_blockcount <= len); |
| 3913 | return; |
| 3914 | } |
| 3915 | |
| 3916 | /* |
| 3917 | * Update and validate the extent map to return |
| 3918 | */ |
| 3919 | STATIC void |
| 3920 | xfs_bmapi_update_map( |
| 3921 | struct xfs_bmbt_irec **map, |
| 3922 | xfs_fileoff_t *bno, |
| 3923 | xfs_filblks_t *len, |
| 3924 | xfs_fileoff_t obno, |
| 3925 | xfs_fileoff_t end, |
| 3926 | int *n, |
| 3927 | int flags) |
| 3928 | { |
| 3929 | xfs_bmbt_irec_t *mval = *map; |
| 3930 | |
| 3931 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3932 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3933 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3934 | (mval->br_startoff < obno)); |
| 3935 | |
| 3936 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3937 | *len = end - *bno; |
| 3938 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3939 | /* update previous map with new information */ |
| 3940 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3941 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3942 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3943 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3944 | mval[-1].br_state = mval->br_state; |
| 3945 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3946 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3947 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3948 | mval->br_startblock == mval[-1].br_startblock + |
| 3949 | mval[-1].br_blockcount && |
| 3950 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3951 | mval[-1].br_state == mval->br_state)) { |
| 3952 | ASSERT(mval->br_startoff == |
| 3953 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3954 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3955 | } else if (*n > 0 && |
| 3956 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3957 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3958 | mval->br_startoff == |
| 3959 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3960 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3961 | mval[-1].br_state = mval->br_state; |
| 3962 | } else if (!((*n == 0) && |
| 3963 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3964 | obno))) { |
| 3965 | mval++; |
| 3966 | (*n)++; |
| 3967 | } |
| 3968 | *map = mval; |
| 3969 | } |
| 3970 | |
| 3971 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3972 | * Map file blocks to filesystem blocks without allocation. |
| 3973 | */ |
| 3974 | int |
| 3975 | xfs_bmapi_read( |
| 3976 | struct xfs_inode *ip, |
| 3977 | xfs_fileoff_t bno, |
| 3978 | xfs_filblks_t len, |
| 3979 | struct xfs_bmbt_irec *mval, |
| 3980 | int *nmap, |
| 3981 | int flags) |
| 3982 | { |
| 3983 | struct xfs_mount *mp = ip->i_mount; |
| 3984 | struct xfs_ifork *ifp; |
| 3985 | struct xfs_bmbt_irec got; |
| 3986 | struct xfs_bmbt_irec prev; |
| 3987 | xfs_fileoff_t obno; |
| 3988 | xfs_fileoff_t end; |
| 3989 | xfs_extnum_t lastx; |
| 3990 | int error; |
| 3991 | int eof; |
| 3992 | int n = 0; |
| 3993 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 3994 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 3995 | |
| 3996 | ASSERT(*nmap >= 1); |
| 3997 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
| 3998 | XFS_BMAPI_IGSTATE))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3999 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4000 | |
| 4001 | if (unlikely(XFS_TEST_ERROR( |
| 4002 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4003 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 4004 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4005 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4006 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4010 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4011 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4012 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4013 | |
| 4014 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4015 | |
| 4016 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4017 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4018 | if (error) |
| 4019 | return error; |
| 4020 | } |
| 4021 | |
| 4022 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev); |
| 4023 | end = bno + len; |
| 4024 | obno = bno; |
| 4025 | |
| 4026 | while (bno < end && n < *nmap) { |
| 4027 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4028 | if (eof) |
| 4029 | got.br_startoff = end; |
| 4030 | if (got.br_startoff > bno) { |
| 4031 | /* Reading in a hole. */ |
| 4032 | mval->br_startoff = bno; |
| 4033 | mval->br_startblock = HOLESTARTBLOCK; |
| 4034 | mval->br_blockcount = |
| 4035 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4036 | mval->br_state = XFS_EXT_NORM; |
| 4037 | bno += mval->br_blockcount; |
| 4038 | len -= mval->br_blockcount; |
| 4039 | mval++; |
| 4040 | n++; |
| 4041 | continue; |
| 4042 | } |
| 4043 | |
| 4044 | /* set up the extent map to return. */ |
| 4045 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4046 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4047 | |
| 4048 | /* If we're done, stop now. */ |
| 4049 | if (bno >= end || n >= *nmap) |
| 4050 | break; |
| 4051 | |
| 4052 | /* Else go on to the next record. */ |
| 4053 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4054 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4055 | else |
| 4056 | eof = 1; |
| 4057 | } |
| 4058 | *nmap = n; |
| 4059 | return 0; |
| 4060 | } |
| 4061 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4062 | STATIC int |
| 4063 | xfs_bmapi_reserve_delalloc( |
| 4064 | struct xfs_inode *ip, |
| 4065 | xfs_fileoff_t aoff, |
| 4066 | xfs_filblks_t len, |
| 4067 | struct xfs_bmbt_irec *got, |
| 4068 | struct xfs_bmbt_irec *prev, |
| 4069 | xfs_extnum_t *lastx, |
| 4070 | int eof) |
| 4071 | { |
| 4072 | struct xfs_mount *mp = ip->i_mount; |
| 4073 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4074 | xfs_extlen_t alen; |
| 4075 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4076 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4077 | xfs_extlen_t extsz; |
| 4078 | int error; |
| 4079 | |
| 4080 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4081 | if (!eof) |
| 4082 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
| 4083 | |
| 4084 | /* Figure out the extent size, adjust alen */ |
| 4085 | extsz = xfs_get_extsz_hint(ip); |
| 4086 | if (extsz) { |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4087 | error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof, |
| 4088 | 1, 0, &aoff, &alen); |
| 4089 | ASSERT(!error); |
| 4090 | } |
| 4091 | |
| 4092 | if (rt) |
| 4093 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4094 | |
| 4095 | /* |
| 4096 | * Make a transaction-less quota reservation for delayed allocation |
| 4097 | * blocks. This number gets adjusted later. We return if we haven't |
| 4098 | * allocated blocks already inside this loop. |
| 4099 | */ |
| 4100 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4101 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4102 | if (error) |
| 4103 | return error; |
| 4104 | |
| 4105 | /* |
| 4106 | * Split changing sb for alen and indlen since they could be coming |
| 4107 | * from different places. |
| 4108 | */ |
| 4109 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4110 | ASSERT(indlen > 0); |
| 4111 | |
| 4112 | if (rt) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4113 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4114 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4115 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | if (error) |
| 4119 | goto out_unreserve_quota; |
| 4120 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4121 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4122 | if (error) |
| 4123 | goto out_unreserve_blocks; |
| 4124 | |
| 4125 | |
| 4126 | ip->i_delayed_blks += alen; |
| 4127 | |
| 4128 | got->br_startoff = aoff; |
| 4129 | got->br_startblock = nullstartblock(indlen); |
| 4130 | got->br_blockcount = alen; |
| 4131 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4132 | xfs_bmap_add_extent_hole_delay(ip, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4133 | |
| 4134 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4135 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay |
| 4136 | * might have merged it into one of the neighbouring ones. |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4137 | */ |
| 4138 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); |
| 4139 | |
| 4140 | ASSERT(got->br_startoff <= aoff); |
| 4141 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); |
| 4142 | ASSERT(isnullstartblock(got->br_startblock)); |
| 4143 | ASSERT(got->br_state == XFS_EXT_NORM); |
| 4144 | return 0; |
| 4145 | |
| 4146 | out_unreserve_blocks: |
| 4147 | if (rt) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4148 | xfs_mod_frextents(mp, extsz); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4149 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4150 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4151 | out_unreserve_quota: |
| 4152 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 4153 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4154 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4155 | return error; |
| 4156 | } |
| 4157 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4158 | /* |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4159 | * Map file blocks to filesystem blocks, adding delayed allocations as needed. |
| 4160 | */ |
| 4161 | int |
| 4162 | xfs_bmapi_delay( |
| 4163 | struct xfs_inode *ip, /* incore inode */ |
| 4164 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4165 | xfs_filblks_t len, /* length to map in file */ |
| 4166 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4167 | int *nmap, /* i/o: mval size/count */ |
| 4168 | int flags) /* XFS_BMAPI_... */ |
| 4169 | { |
| 4170 | struct xfs_mount *mp = ip->i_mount; |
| 4171 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4172 | struct xfs_bmbt_irec got; /* current file extent record */ |
| 4173 | struct xfs_bmbt_irec prev; /* previous file extent record */ |
| 4174 | xfs_fileoff_t obno; /* old block number (offset) */ |
| 4175 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4176 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4177 | int eof; /* we've hit the end of extents */ |
| 4178 | int n = 0; /* current extent index */ |
| 4179 | int error = 0; |
| 4180 | |
| 4181 | ASSERT(*nmap >= 1); |
| 4182 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4183 | ASSERT(!(flags & ~XFS_BMAPI_ENTIRE)); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4184 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4185 | |
| 4186 | if (unlikely(XFS_TEST_ERROR( |
| 4187 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4188 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 4189 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4190 | XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4191 | return -EFSCORRUPTED; |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4195 | return -EIO; |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4196 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4197 | XFS_STATS_INC(mp, xs_blk_mapw); |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4198 | |
| 4199 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4200 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4201 | if (error) |
| 4202 | return error; |
| 4203 | } |
| 4204 | |
| 4205 | xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev); |
| 4206 | end = bno + len; |
| 4207 | obno = bno; |
| 4208 | |
| 4209 | while (bno < end && n < *nmap) { |
| 4210 | if (eof || got.br_startoff > bno) { |
| 4211 | error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got, |
| 4212 | &prev, &lastx, eof); |
| 4213 | if (error) { |
| 4214 | if (n == 0) { |
| 4215 | *nmap = 0; |
| 4216 | return error; |
| 4217 | } |
| 4218 | break; |
| 4219 | } |
| 4220 | } |
| 4221 | |
| 4222 | /* set up the extent map to return. */ |
| 4223 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4224 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4225 | |
| 4226 | /* If we're done, stop now. */ |
| 4227 | if (bno >= end || n >= *nmap) |
| 4228 | break; |
| 4229 | |
| 4230 | /* Else go on to the next record. */ |
| 4231 | prev = got; |
| 4232 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4233 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4234 | else |
| 4235 | eof = 1; |
| 4236 | } |
| 4237 | |
| 4238 | *nmap = n; |
| 4239 | return 0; |
| 4240 | } |
| 4241 | |
| 4242 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4243 | static int |
| 4244 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4245 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4246 | { |
| 4247 | struct xfs_mount *mp = bma->ip->i_mount; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4248 | int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4249 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4250 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4251 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4252 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4253 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4254 | ASSERT(bma->length > 0); |
| 4255 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4256 | /* |
| 4257 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4258 | * for in this bmap call but that wouldn't be as good. |
| 4259 | */ |
| 4260 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4261 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4262 | bma->offset = bma->got.br_startoff; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4263 | if (bma->idx != NULLEXTNUM && bma->idx) { |
| 4264 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4265 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4266 | } |
| 4267 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4268 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4269 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4270 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4271 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | /* |
| 4275 | * Indicate if this is the first user data in the file, or just any |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4276 | * user data. And if it is userdata, indicate whether it needs to |
| 4277 | * be initialised to zero during allocation. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4278 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4279 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4280 | bma->userdata = (bma->offset == 0) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4281 | XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4282 | if (bma->flags & XFS_BMAPI_ZERO) |
| 4283 | bma->userdata |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4286 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4287 | |
| 4288 | /* |
| 4289 | * Only want to do the alignment at the eof if it is userdata and |
| 4290 | * allocation length is larger than a stripe unit. |
| 4291 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4292 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4293 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4294 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4295 | if (error) |
| 4296 | return error; |
| 4297 | } |
| 4298 | |
| 4299 | error = xfs_bmap_alloc(bma); |
| 4300 | if (error) |
| 4301 | return error; |
| 4302 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4303 | if (bma->dfops->dop_low) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4304 | bma->minleft = 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4305 | if (bma->cur) |
| 4306 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4307 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4308 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4309 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4310 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4311 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4312 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4313 | } |
| 4314 | /* |
| 4315 | * Bump the number of extents we've allocated |
| 4316 | * in this call. |
| 4317 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4318 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4319 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4320 | if (bma->cur) |
| 4321 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4322 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4323 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4324 | bma->got.br_startoff = bma->offset; |
| 4325 | bma->got.br_startblock = bma->blkno; |
| 4326 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4327 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4328 | |
| 4329 | /* |
| 4330 | * A wasdelay extent has been initialized, so shouldn't be flagged |
| 4331 | * as unwritten. |
| 4332 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4333 | if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4334 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4335 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4336 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4337 | if (bma->wasdel) |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 4338 | error = xfs_bmap_add_extent_delay_real(bma); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4339 | else |
| 4340 | error = xfs_bmap_add_extent_hole_real(bma, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4341 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4342 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4343 | if (error) |
| 4344 | return error; |
| 4345 | |
| 4346 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4347 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4348 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4349 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4350 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4351 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4352 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4353 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4354 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4355 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4356 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4357 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4358 | return 0; |
| 4359 | } |
| 4360 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4361 | STATIC int |
| 4362 | xfs_bmapi_convert_unwritten( |
| 4363 | struct xfs_bmalloca *bma, |
| 4364 | struct xfs_bmbt_irec *mval, |
| 4365 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4366 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4367 | { |
| 4368 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4369 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4370 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4371 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4372 | int error; |
| 4373 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4374 | /* check if we need to do unwritten->real conversion */ |
| 4375 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4376 | (flags & XFS_BMAPI_PREALLOC)) |
| 4377 | return 0; |
| 4378 | |
| 4379 | /* check if we need to do real->unwritten conversion */ |
| 4380 | if (mval->br_state == XFS_EXT_NORM && |
| 4381 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4382 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4383 | return 0; |
| 4384 | |
| 4385 | /* |
| 4386 | * Modify (by adding) the state flag, if writing. |
| 4387 | */ |
| 4388 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4389 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4390 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4391 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4392 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4393 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4394 | } |
| 4395 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4396 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4397 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4398 | /* |
| 4399 | * Before insertion into the bmbt, zero the range being converted |
| 4400 | * if required. |
| 4401 | */ |
| 4402 | if (flags & XFS_BMAPI_ZERO) { |
| 4403 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4404 | mval->br_blockcount); |
| 4405 | if (error) |
| 4406 | return error; |
| 4407 | } |
| 4408 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4409 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4410 | &bma->cur, mval, bma->firstblock, bma->dfops, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4411 | &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4412 | /* |
| 4413 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4414 | * path because the conversion might not have done so (e.g., if the |
| 4415 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4416 | * in the transaction for the sake of fsync(), even if nothing has |
| 4417 | * changed, because fsync() will not force the log for this transaction |
| 4418 | * unless it sees the inode pinned. |
| 4419 | */ |
| 4420 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4421 | if (error) |
| 4422 | return error; |
| 4423 | |
| 4424 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4425 | * Update our extent pointer, given that |
| 4426 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4427 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4428 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4429 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4430 | |
| 4431 | /* |
| 4432 | * We may have combined previously unwritten space with written space, |
| 4433 | * so generate another request. |
| 4434 | */ |
| 4435 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4436 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4437 | return 0; |
| 4438 | } |
| 4439 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4440 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4441 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4442 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4443 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4444 | * locking problems. |
| 4445 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | * The returned value in "firstblock" from the first call in a transaction |
| 4447 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4448 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4449 | * the first call in "total"; if no allocation group has that many free |
| 4450 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4451 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4452 | int |
| 4453 | xfs_bmapi_write( |
| 4454 | struct xfs_trans *tp, /* transaction pointer */ |
| 4455 | struct xfs_inode *ip, /* incore inode */ |
| 4456 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4457 | xfs_filblks_t len, /* length to map in file */ |
| 4458 | int flags, /* XFS_BMAPI_... */ |
| 4459 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4460 | controls a.g. for allocs */ |
| 4461 | xfs_extlen_t total, /* total blocks needed */ |
| 4462 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4463 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4464 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4466 | struct xfs_mount *mp = ip->i_mount; |
| 4467 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4468 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4469 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4470 | int eof; /* after the end of extents */ |
| 4471 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4472 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4473 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4474 | int whichfork; /* data or attr fork */ |
| 4475 | char inhole; /* current location is hole in file */ |
| 4476 | char wasdelay; /* old extent was delayed */ |
| 4477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4479 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4480 | int orig_flags; /* original flags arg value */ |
| 4481 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4482 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4483 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | |
| 4485 | orig_bno = bno; |
| 4486 | orig_len = len; |
| 4487 | orig_flags = flags; |
| 4488 | orig_mval = mval; |
| 4489 | orig_nmap = *nmap; |
| 4490 | #endif |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4491 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4492 | XFS_ATTR_FORK : XFS_DATA_FORK; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4495 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4496 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
| 4497 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4498 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4499 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4500 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4501 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4502 | /* zeroing is for currently only for data extents, not metadata */ |
| 4503 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4504 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4505 | /* |
| 4506 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4507 | * but it makes no sense to do both at once. This would result in |
| 4508 | * zeroing the unwritten extent twice, but it still being an |
| 4509 | * unwritten extent.... |
| 4510 | */ |
| 4511 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4512 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | if (unlikely(XFS_TEST_ERROR( |
| 4515 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4516 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4518 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4519 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4523 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4526 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4527 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4528 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4529 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4531 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4533 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4534 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4535 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
| 4538 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4539 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4540 | if (error) |
| 4541 | goto error0; |
| 4542 | } |
| 4543 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4544 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4545 | &bma.prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | n = 0; |
| 4547 | end = bno + len; |
| 4548 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4549 | |
| 4550 | bma.tp = tp; |
| 4551 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4552 | bma.total = total; |
| 4553 | bma.userdata = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4554 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4555 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | while (bno < end && n < *nmap) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4558 | inhole = eof || bma.got.br_startoff > bno; |
| 4559 | wasdelay = !inhole && isnullstartblock(bma.got.br_startblock); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | /* |
| 4562 | * First, deal with the hole before the allocated space |
| 4563 | * that we found, if any. |
| 4564 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4565 | if (inhole || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4566 | bma.eof = eof; |
| 4567 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4568 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4569 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4570 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4571 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4572 | /* |
| 4573 | * There's a 32/64 bit type mismatch between the |
| 4574 | * allocation length request (which can be 64 bits in |
| 4575 | * length) and the bma length request, which is |
| 4576 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4577 | * check for 32-bit overflows and handle them here. |
| 4578 | */ |
| 4579 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4580 | bma.length = MAXEXTLEN; |
| 4581 | else |
| 4582 | bma.length = len; |
| 4583 | |
| 4584 | ASSERT(len > 0); |
| 4585 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4586 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | if (error) |
| 4588 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4589 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4590 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4592 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4593 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4594 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4595 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4597 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4598 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4599 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4600 | continue; |
| 4601 | if (error) |
| 4602 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4603 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4604 | /* update the extent map to return */ |
| 4605 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | /* |
| 4608 | * If we're done, stop now. Stop when we've allocated |
| 4609 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4610 | * the transaction may get too big. |
| 4611 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4612 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4614 | |
| 4615 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4616 | bma.prev = bma.got; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4617 | if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) { |
| 4618 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx), |
| 4619 | &bma.got); |
| 4620 | } else |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4621 | eof = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | /* |
| 4626 | * Transform from btree to extents, give it cur. |
| 4627 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4628 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4629 | int tmp_logflags = 0; |
| 4630 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4631 | ASSERT(bma.cur); |
| 4632 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4634 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | if (error) |
| 4636 | goto error0; |
| 4637 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4640 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4641 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | error0: |
| 4644 | /* |
| 4645 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4646 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4648 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4650 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4651 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4653 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | /* |
| 4655 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4656 | * detecting a case where the data is changed, there's an error, |
| 4657 | * and it's not logged so we don't shutdown when we should. |
| 4658 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4659 | if (bma.logflags) |
| 4660 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4661 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4662 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | if (!error) { |
| 4664 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4665 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4666 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4667 | bma.cur->bc_private.b.firstblock) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4668 | (dfops->dop_low && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4670 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4671 | bma.cur->bc_private.b.firstblock))); |
| 4672 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4674 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4676 | } |
| 4677 | if (!error) |
| 4678 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4679 | orig_nmap, *nmap); |
| 4680 | return error; |
| 4681 | } |
| 4682 | |
| 4683 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4684 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4685 | * indlen reservation must be shared across the two new extents that are left |
| 4686 | * behind. |
| 4687 | * |
| 4688 | * Given the original reservation and the worst case indlen for the two new |
| 4689 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4690 | * reservation fairly across the two new extents. If necessary, steal available |
| 4691 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4692 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4693 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4694 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4695 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4696 | xfs_bmap_split_indlen( |
| 4697 | xfs_filblks_t ores, /* original res. */ |
| 4698 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4699 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4700 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4701 | { |
| 4702 | xfs_filblks_t len1 = *indlen1; |
| 4703 | xfs_filblks_t len2 = *indlen2; |
| 4704 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4705 | xfs_filblks_t stolen = 0; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4706 | |
| 4707 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4708 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4709 | * indlen for both new extents. |
| 4710 | */ |
| 4711 | while (nres > ores && avail) { |
| 4712 | nres--; |
| 4713 | avail--; |
| 4714 | stolen++; |
| 4715 | } |
| 4716 | |
| 4717 | /* |
| 4718 | * The only blocks available are those reserved for the original |
| 4719 | * extent and what we can steal from the extent being removed. |
| 4720 | * If this still isn't enough to satisfy the combined |
| 4721 | * requirements for the two new extents, skim blocks off of each |
| 4722 | * of the new reservations until they match what is available. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4723 | */ |
| 4724 | while (nres > ores) { |
| 4725 | if (len1) { |
| 4726 | len1--; |
| 4727 | nres--; |
| 4728 | } |
| 4729 | if (nres == ores) |
| 4730 | break; |
| 4731 | if (len2) { |
| 4732 | len2--; |
| 4733 | nres--; |
| 4734 | } |
| 4735 | } |
| 4736 | |
| 4737 | *indlen1 = len1; |
| 4738 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4739 | |
| 4740 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4744 | * Called by xfs_bmapi to update file extent records and the btree |
| 4745 | * after removing space (or undoing a delayed allocation). |
| 4746 | */ |
| 4747 | STATIC int /* error */ |
| 4748 | xfs_bmap_del_extent( |
| 4749 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4750 | xfs_trans_t *tp, /* current transaction pointer */ |
| 4751 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4752 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4753 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4754 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4755 | int *logflagsp, /* inode logging flags */ |
| 4756 | int whichfork) /* data or attr fork */ |
| 4757 | { |
| 4758 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 4759 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 4760 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4761 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 4762 | int delay; /* current block is delayed allocated */ |
| 4763 | int do_fx; /* free extent at end of routine */ |
| 4764 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
| 4765 | int error; /* error return value */ |
| 4766 | int flags; /* inode logging flags */ |
| 4767 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 4768 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4769 | int i; /* temp state */ |
| 4770 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4771 | xfs_mount_t *mp; /* mount structure */ |
| 4772 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4773 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4774 | /* REFERENCED */ |
| 4775 | uint qfield; /* quota field to update */ |
| 4776 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 4777 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 4778 | int state = 0; |
| 4779 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4780 | mp = ip->i_mount; |
| 4781 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4782 | |
| 4783 | if (whichfork == XFS_ATTR_FORK) |
| 4784 | state |= BMAP_ATTRFORK; |
| 4785 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4786 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4787 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / |
| 4788 | (uint)sizeof(xfs_bmbt_rec_t))); |
| 4789 | ASSERT(del->br_blockcount > 0); |
| 4790 | ep = xfs_iext_get_ext(ifp, *idx); |
| 4791 | xfs_bmbt_get_all(ep, &got); |
| 4792 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4793 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4794 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4795 | ASSERT(got_endoff >= del_endoff); |
| 4796 | delay = isnullstartblock(got.br_startblock); |
| 4797 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
| 4798 | flags = 0; |
| 4799 | qfield = 0; |
| 4800 | error = 0; |
| 4801 | /* |
| 4802 | * If deleting a real allocation, must free up the disk space. |
| 4803 | */ |
| 4804 | if (!delay) { |
| 4805 | flags = XFS_ILOG_CORE; |
| 4806 | /* |
| 4807 | * Realtime allocation. Free it and record di_nblocks update. |
| 4808 | */ |
| 4809 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4810 | xfs_fsblock_t bno; |
| 4811 | xfs_filblks_t len; |
| 4812 | |
| 4813 | ASSERT(do_mod(del->br_blockcount, |
| 4814 | mp->m_sb.sb_rextsize) == 0); |
| 4815 | ASSERT(do_mod(del->br_startblock, |
| 4816 | mp->m_sb.sb_rextsize) == 0); |
| 4817 | bno = del->br_startblock; |
| 4818 | len = del->br_blockcount; |
| 4819 | do_div(bno, mp->m_sb.sb_rextsize); |
| 4820 | do_div(len, mp->m_sb.sb_rextsize); |
| 4821 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4822 | if (error) |
| 4823 | goto done; |
| 4824 | do_fx = 0; |
| 4825 | nblks = len * mp->m_sb.sb_rextsize; |
| 4826 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4827 | } |
| 4828 | /* |
| 4829 | * Ordinary allocation. |
| 4830 | */ |
| 4831 | else { |
| 4832 | do_fx = 1; |
| 4833 | nblks = del->br_blockcount; |
| 4834 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4835 | } |
| 4836 | /* |
| 4837 | * Set up del_endblock and cur for later. |
| 4838 | */ |
| 4839 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4840 | if (cur) { |
| 4841 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 4842 | got.br_startblock, got.br_blockcount, |
| 4843 | &i))) |
| 4844 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4845 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4846 | } |
| 4847 | da_old = da_new = 0; |
| 4848 | } else { |
| 4849 | da_old = startblockval(got.br_startblock); |
| 4850 | da_new = 0; |
| 4851 | nblks = 0; |
| 4852 | do_fx = 0; |
| 4853 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame^] | 4854 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4855 | /* |
| 4856 | * Set flag value to use in switch statement. |
| 4857 | * Left-contig is 2, right-contig is 1. |
| 4858 | */ |
| 4859 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 4860 | (got_endoff == del_endoff)) { |
| 4861 | case 3: |
| 4862 | /* |
| 4863 | * Matches the whole extent. Delete the entry. |
| 4864 | */ |
| 4865 | xfs_iext_remove(ip, *idx, 1, |
| 4866 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 4867 | --*idx; |
| 4868 | if (delay) |
| 4869 | break; |
| 4870 | |
| 4871 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4872 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 4873 | flags |= XFS_ILOG_CORE; |
| 4874 | if (!cur) { |
| 4875 | flags |= xfs_ilog_fext(whichfork); |
| 4876 | break; |
| 4877 | } |
| 4878 | if ((error = xfs_btree_delete(cur, &i))) |
| 4879 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4880 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4881 | break; |
| 4882 | |
| 4883 | case 2: |
| 4884 | /* |
| 4885 | * Deleting the first part of the extent. |
| 4886 | */ |
| 4887 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4888 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 4889 | temp = got.br_blockcount - del->br_blockcount; |
| 4890 | xfs_bmbt_set_blockcount(ep, temp); |
| 4891 | if (delay) { |
| 4892 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 4893 | da_old); |
| 4894 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 4895 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4896 | da_new = temp; |
| 4897 | break; |
| 4898 | } |
| 4899 | xfs_bmbt_set_startblock(ep, del_endblock); |
| 4900 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4901 | if (!cur) { |
| 4902 | flags |= xfs_ilog_fext(whichfork); |
| 4903 | break; |
| 4904 | } |
| 4905 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 4906 | got.br_blockcount - del->br_blockcount, |
| 4907 | got.br_state))) |
| 4908 | goto done; |
| 4909 | break; |
| 4910 | |
| 4911 | case 1: |
| 4912 | /* |
| 4913 | * Deleting the last part of the extent. |
| 4914 | */ |
| 4915 | temp = got.br_blockcount - del->br_blockcount; |
| 4916 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4917 | xfs_bmbt_set_blockcount(ep, temp); |
| 4918 | if (delay) { |
| 4919 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 4920 | da_old); |
| 4921 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 4922 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4923 | da_new = temp; |
| 4924 | break; |
| 4925 | } |
| 4926 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4927 | if (!cur) { |
| 4928 | flags |= xfs_ilog_fext(whichfork); |
| 4929 | break; |
| 4930 | } |
| 4931 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 4932 | got.br_startblock, |
| 4933 | got.br_blockcount - del->br_blockcount, |
| 4934 | got.br_state))) |
| 4935 | goto done; |
| 4936 | break; |
| 4937 | |
| 4938 | case 0: |
| 4939 | /* |
| 4940 | * Deleting the middle of the extent. |
| 4941 | */ |
| 4942 | temp = del->br_startoff - got.br_startoff; |
| 4943 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4944 | xfs_bmbt_set_blockcount(ep, temp); |
| 4945 | new.br_startoff = del_endoff; |
| 4946 | temp2 = got_endoff - del_endoff; |
| 4947 | new.br_blockcount = temp2; |
| 4948 | new.br_state = got.br_state; |
| 4949 | if (!delay) { |
| 4950 | new.br_startblock = del_endblock; |
| 4951 | flags |= XFS_ILOG_CORE; |
| 4952 | if (cur) { |
| 4953 | if ((error = xfs_bmbt_update(cur, |
| 4954 | got.br_startoff, |
| 4955 | got.br_startblock, temp, |
| 4956 | got.br_state))) |
| 4957 | goto done; |
| 4958 | if ((error = xfs_btree_increment(cur, 0, &i))) |
| 4959 | goto done; |
| 4960 | cur->bc_rec.b = new; |
| 4961 | error = xfs_btree_insert(cur, &i); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4962 | if (error && error != -ENOSPC) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4963 | goto done; |
| 4964 | /* |
| 4965 | * If get no-space back from btree insert, |
| 4966 | * it tried a split, and we have a zero |
| 4967 | * block reservation. |
| 4968 | * Fix up our state and return the error. |
| 4969 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4970 | if (error == -ENOSPC) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4971 | /* |
| 4972 | * Reset the cursor, don't trust |
| 4973 | * it after any insert operation. |
| 4974 | */ |
| 4975 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 4976 | got.br_startoff, |
| 4977 | got.br_startblock, |
| 4978 | temp, &i))) |
| 4979 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4980 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 4981 | i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4982 | /* |
| 4983 | * Update the btree record back |
| 4984 | * to the original value. |
| 4985 | */ |
| 4986 | if ((error = xfs_bmbt_update(cur, |
| 4987 | got.br_startoff, |
| 4988 | got.br_startblock, |
| 4989 | got.br_blockcount, |
| 4990 | got.br_state))) |
| 4991 | goto done; |
| 4992 | /* |
| 4993 | * Reset the extent record back |
| 4994 | * to the original value. |
| 4995 | */ |
| 4996 | xfs_bmbt_set_blockcount(ep, |
| 4997 | got.br_blockcount); |
| 4998 | flags = 0; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4999 | error = -ENOSPC; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5000 | goto done; |
| 5001 | } |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5002 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5003 | } else |
| 5004 | flags |= xfs_ilog_fext(whichfork); |
| 5005 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5006 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5007 | } else { |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5008 | xfs_filblks_t stolen; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5009 | ASSERT(whichfork == XFS_DATA_FORK); |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5010 | |
| 5011 | /* |
| 5012 | * Distribute the original indlen reservation across the |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5013 | * two new extents. Steal blocks from the deleted extent |
| 5014 | * if necessary. Stealing blocks simply fudges the |
| 5015 | * fdblocks accounting in xfs_bunmapi(). |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5016 | */ |
| 5017 | temp = xfs_bmap_worst_indlen(ip, got.br_blockcount); |
| 5018 | temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5019 | stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2, |
| 5020 | del->br_blockcount); |
| 5021 | da_new = temp + temp2 - stolen; |
| 5022 | del->br_blockcount -= stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5023 | |
| 5024 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5025 | * Set the reservation for each extent. Warn if either |
| 5026 | * is zero as this can lead to delalloc problems. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5027 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5028 | WARN_ON_ONCE(!temp || !temp2); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5029 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5030 | new.br_startblock = nullstartblock((int)temp2); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5031 | } |
| 5032 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5033 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 5034 | ++*idx; |
| 5035 | break; |
| 5036 | } |
| 5037 | /* |
| 5038 | * If we need to, add to list of extents to delete. |
| 5039 | */ |
| 5040 | if (do_fx) |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5041 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame^] | 5042 | del->br_blockcount, NULL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5043 | /* |
| 5044 | * Adjust inode # blocks in the file. |
| 5045 | */ |
| 5046 | if (nblks) |
| 5047 | ip->i_d.di_nblocks -= nblks; |
| 5048 | /* |
| 5049 | * Adjust quota data. |
| 5050 | */ |
| 5051 | if (qfield) |
| 5052 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5053 | |
| 5054 | /* |
| 5055 | * Account for change in delayed indirect blocks. |
| 5056 | * Nothing to do for disk quota accounting here. |
| 5057 | */ |
| 5058 | ASSERT(da_old >= da_new); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 5059 | if (da_old > da_new) |
| 5060 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5061 | done: |
| 5062 | *logflagsp = flags; |
| 5063 | return error; |
| 5064 | } |
| 5065 | |
| 5066 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | * Unmap (remove) blocks from a file. |
| 5068 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5069 | * that value. If not all extents in the block range can be removed then |
| 5070 | * *done is set. |
| 5071 | */ |
| 5072 | int /* error */ |
| 5073 | xfs_bunmapi( |
| 5074 | xfs_trans_t *tp, /* transaction pointer */ |
| 5075 | struct xfs_inode *ip, /* incore inode */ |
| 5076 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5077 | xfs_filblks_t len, /* length to unmap in file */ |
| 5078 | int flags, /* misc flags */ |
| 5079 | xfs_extnum_t nexts, /* number of extents max */ |
| 5080 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5081 | controls a.g. for allocs */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5082 | struct xfs_defer_ops *dfops, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | int *done) /* set if not done yet */ |
| 5084 | { |
| 5085 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5086 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5087 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5088 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | int error; /* error return value */ |
| 5090 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5091 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5093 | int isrt; /* freeing in rt area */ |
| 5094 | xfs_extnum_t lastx; /* last extent index used */ |
| 5095 | int logflags; /* transaction logging flags */ |
| 5096 | xfs_extlen_t mod; /* rt extent offset */ |
| 5097 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5098 | xfs_extnum_t nextents; /* number of file extents */ |
| 5099 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5101 | int tmp_logflags; /* partial logging flags */ |
| 5102 | int wasdel; /* was a delayed alloc extent */ |
| 5103 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | xfs_fsblock_t sum; |
| 5105 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5106 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5109 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5110 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5111 | if (unlikely( |
| 5112 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5113 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5114 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5115 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5116 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | } |
| 5118 | mp = ip->i_mount; |
| 5119 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5120 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5121 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5122 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | ASSERT(len > 0); |
| 5124 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5127 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5128 | return error; |
| 5129 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5130 | if (nextents == 0) { |
| 5131 | *done = 1; |
| 5132 | return 0; |
| 5133 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5134 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5135 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | start = bno; |
| 5137 | bno = start + len - 1; |
| 5138 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5139 | &prev); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | /* |
| 5142 | * Check to see if the given block number is past the end of the |
| 5143 | * file, back up to the last block if so... |
| 5144 | */ |
| 5145 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5146 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | xfs_bmbt_get_all(ep, &got); |
| 5148 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5149 | } |
| 5150 | logflags = 0; |
| 5151 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5152 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5153 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5155 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | cur->bc_private.b.flags = 0; |
| 5157 | } else |
| 5158 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5159 | |
| 5160 | if (isrt) { |
| 5161 | /* |
| 5162 | * Synchronize by locking the bitmap inode. |
| 5163 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5164 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5165 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5166 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5167 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | extno = 0; |
| 5171 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5172 | (nexts == 0 || extno < nexts)) { |
| 5173 | /* |
| 5174 | * Is the found extent after a hole in which bno lives? |
| 5175 | * Just back up to the previous extent, if so. |
| 5176 | */ |
| 5177 | if (got.br_startoff > bno) { |
| 5178 | if (--lastx < 0) |
| 5179 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5180 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | xfs_bmbt_get_all(ep, &got); |
| 5182 | } |
| 5183 | /* |
| 5184 | * Is the last block of this extent before the range |
| 5185 | * we're supposed to delete? If so, we're done. |
| 5186 | */ |
| 5187 | bno = XFS_FILEOFF_MIN(bno, |
| 5188 | got.br_startoff + got.br_blockcount - 1); |
| 5189 | if (bno < start) |
| 5190 | break; |
| 5191 | /* |
| 5192 | * Then deal with the (possibly delayed) allocated space |
| 5193 | * we found. |
| 5194 | */ |
| 5195 | ASSERT(ep != NULL); |
| 5196 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5197 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | if (got.br_startoff < start) { |
| 5199 | del.br_startoff = start; |
| 5200 | del.br_blockcount -= start - got.br_startoff; |
| 5201 | if (!wasdel) |
| 5202 | del.br_startblock += start - got.br_startoff; |
| 5203 | } |
| 5204 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5205 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5206 | sum = del.br_startblock + del.br_blockcount; |
| 5207 | if (isrt && |
| 5208 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5209 | /* |
| 5210 | * Realtime extent not lined up at the end. |
| 5211 | * The extent could have been split into written |
| 5212 | * and unwritten pieces, or we could just be |
| 5213 | * unmapping part of it. But we can't really |
| 5214 | * get rid of part of a realtime extent. |
| 5215 | */ |
| 5216 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5217 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | /* |
| 5219 | * This piece is unwritten, or we're not |
| 5220 | * using unwritten extents. Skip over it. |
| 5221 | */ |
| 5222 | ASSERT(bno >= mod); |
| 5223 | bno -= mod > del.br_blockcount ? |
| 5224 | del.br_blockcount : mod; |
| 5225 | if (bno < got.br_startoff) { |
| 5226 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5227 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5228 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | } |
| 5230 | continue; |
| 5231 | } |
| 5232 | /* |
| 5233 | * It's written, turn it unwritten. |
| 5234 | * This is better than zeroing it. |
| 5235 | */ |
| 5236 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5237 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | /* |
| 5239 | * If this spans a realtime extent boundary, |
| 5240 | * chop it back to the start of the one we end at. |
| 5241 | */ |
| 5242 | if (del.br_blockcount > mod) { |
| 5243 | del.br_startoff += del.br_blockcount - mod; |
| 5244 | del.br_startblock += del.br_blockcount - mod; |
| 5245 | del.br_blockcount = mod; |
| 5246 | } |
| 5247 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5248 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5249 | &lastx, &cur, &del, firstblock, dfops, |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5250 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | if (error) |
| 5252 | goto error0; |
| 5253 | goto nodelete; |
| 5254 | } |
| 5255 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5256 | /* |
| 5257 | * Realtime extent is lined up at the end but not |
| 5258 | * at the front. We'll get rid of full extents if |
| 5259 | * we can. |
| 5260 | */ |
| 5261 | mod = mp->m_sb.sb_rextsize - mod; |
| 5262 | if (del.br_blockcount > mod) { |
| 5263 | del.br_blockcount -= mod; |
| 5264 | del.br_startoff += mod; |
| 5265 | del.br_startblock += mod; |
| 5266 | } else if ((del.br_startoff == start && |
| 5267 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5268 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5269 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | /* |
| 5271 | * Can't make it unwritten. There isn't |
| 5272 | * a full extent here so just skip it. |
| 5273 | */ |
| 5274 | ASSERT(bno >= del.br_blockcount); |
| 5275 | bno -= del.br_blockcount; |
Christoph Hellwig | f1c63b7 | 2011-05-11 15:04:09 +0000 | [diff] [blame] | 5276 | if (got.br_startoff > bno) { |
| 5277 | if (--lastx >= 0) { |
| 5278 | ep = xfs_iext_get_ext(ifp, |
| 5279 | lastx); |
| 5280 | xfs_bmbt_get_all(ep, &got); |
| 5281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | } |
| 5283 | continue; |
| 5284 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5285 | /* |
| 5286 | * This one is already unwritten. |
| 5287 | * It must have a written left neighbor. |
| 5288 | * Unwrite the killed part of that one and |
| 5289 | * try again. |
| 5290 | */ |
| 5291 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5292 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5293 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5295 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | ASSERT(del.br_startblock == |
| 5297 | prev.br_startblock + prev.br_blockcount); |
| 5298 | if (prev.br_startoff < start) { |
| 5299 | mod = start - prev.br_startoff; |
| 5300 | prev.br_blockcount -= mod; |
| 5301 | prev.br_startblock += mod; |
| 5302 | prev.br_startoff = start; |
| 5303 | } |
| 5304 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5305 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5306 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5307 | ip, &lastx, &cur, &prev, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5308 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | if (error) |
| 5310 | goto error0; |
| 5311 | goto nodelete; |
| 5312 | } else { |
| 5313 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5314 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5315 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5316 | ip, &lastx, &cur, &del, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5317 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | if (error) |
| 5319 | goto error0; |
| 5320 | goto nodelete; |
| 5321 | } |
| 5322 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | /* |
| 5325 | * If it's the case where the directory code is running |
| 5326 | * with no block reservation, and the deleted block is in |
| 5327 | * the middle of its extent, and the resulting insert |
| 5328 | * of an extent would cause transformation to btree format, |
| 5329 | * then reject it. The calling code will then swap |
| 5330 | * blocks around instead. |
| 5331 | * We have to do this now, rather than waiting for the |
| 5332 | * conversion to btree format, since the transaction |
| 5333 | * will be dirty. |
| 5334 | */ |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5335 | if (!wasdel && tp->t_blk_res == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5337 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ |
| 5338 | XFS_IFORK_MAXEXT(ip, whichfork) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | del.br_startoff > got.br_startoff && |
| 5340 | del.br_startoff + del.br_blockcount < |
| 5341 | got.br_startoff + got.br_blockcount) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5342 | error = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | goto error0; |
| 5344 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5345 | |
| 5346 | /* |
| 5347 | * Unreserve quota and update realtime free space, if |
| 5348 | * appropriate. If delayed allocation, update the inode delalloc |
| 5349 | * counter now and wait to update the sb counters as |
| 5350 | * xfs_bmap_del_extent() might need to borrow some blocks. |
| 5351 | */ |
| 5352 | if (wasdel) { |
| 5353 | ASSERT(startblockval(del.br_startblock) > 0); |
| 5354 | if (isrt) { |
| 5355 | xfs_filblks_t rtexts; |
| 5356 | |
| 5357 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5358 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5359 | xfs_mod_frextents(mp, (int64_t)rtexts); |
| 5360 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5361 | ip, -((long)del.br_blockcount), 0, |
| 5362 | XFS_QMOPT_RES_RTBLKS); |
| 5363 | } else { |
| 5364 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5365 | ip, -((long)del.br_blockcount), 0, |
| 5366 | XFS_QMOPT_RES_REGBLKS); |
| 5367 | } |
| 5368 | ip->i_delayed_blks -= del.br_blockcount; |
| 5369 | if (cur) |
| 5370 | cur->bc_private.b.flags |= |
| 5371 | XFS_BTCUR_BPRV_WASDEL; |
| 5372 | } else if (cur) |
| 5373 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5374 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5375 | error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5376 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | logflags |= tmp_logflags; |
| 5378 | if (error) |
| 5379 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5380 | |
| 5381 | if (!isrt && wasdel) |
| 5382 | xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false); |
| 5383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | bno = del.br_startoff - 1; |
| 5385 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | /* |
| 5387 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5389 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5390 | if (lastx >= 0) { |
| 5391 | ep = xfs_iext_get_ext(ifp, lastx); |
| 5392 | if (xfs_bmbt_get_startoff(ep) > bno) { |
| 5393 | if (--lastx >= 0) |
| 5394 | ep = xfs_iext_get_ext(ifp, |
| 5395 | lastx); |
| 5396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5398 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | extno++; |
| 5400 | } |
| 5401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | /* |
| 5405 | * Convert to a btree if necessary. |
| 5406 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5407 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5409 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | &cur, 0, &tmp_logflags, whichfork); |
| 5411 | logflags |= tmp_logflags; |
| 5412 | if (error) |
| 5413 | goto error0; |
| 5414 | } |
| 5415 | /* |
| 5416 | * transform from btree to extents, give it cur |
| 5417 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5418 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | ASSERT(cur != NULL); |
| 5420 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5421 | whichfork); |
| 5422 | logflags |= tmp_logflags; |
| 5423 | if (error) |
| 5424 | goto error0; |
| 5425 | } |
| 5426 | /* |
| 5427 | * transform from extents to local? |
| 5428 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | error = 0; |
| 5430 | error0: |
| 5431 | /* |
| 5432 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5433 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5435 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5437 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5438 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5440 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | /* |
| 5442 | * Log inode even in the error case, if the transaction |
| 5443 | * is dirty we'll need to shut down the filesystem. |
| 5444 | */ |
| 5445 | if (logflags) |
| 5446 | xfs_trans_log_inode(tp, ip, logflags); |
| 5447 | if (cur) { |
| 5448 | if (!error) { |
| 5449 | *firstblock = cur->bc_private.b.firstblock; |
| 5450 | cur->bc_private.b.allocated = 0; |
| 5451 | } |
| 5452 | xfs_btree_del_cursor(cur, |
| 5453 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5454 | } |
| 5455 | return error; |
| 5456 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5457 | |
| 5458 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5459 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5460 | * extent that precedes the target hole of the shift. |
| 5461 | */ |
| 5462 | STATIC bool |
| 5463 | xfs_bmse_can_merge( |
| 5464 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5465 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5466 | xfs_fileoff_t shift) /* shift fsb */ |
| 5467 | { |
| 5468 | xfs_fileoff_t startoff; |
| 5469 | |
| 5470 | startoff = got->br_startoff - shift; |
| 5471 | |
| 5472 | /* |
| 5473 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5474 | * the preceding extent. |
| 5475 | */ |
| 5476 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5477 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5478 | (left->br_state != got->br_state) || |
| 5479 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5480 | return false; |
| 5481 | |
| 5482 | return true; |
| 5483 | } |
| 5484 | |
| 5485 | /* |
| 5486 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5487 | * hole in the file. If an extent shift would result in the extent being fully |
| 5488 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5489 | * the preceding extent rather than do the shift. |
| 5490 | * |
| 5491 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5492 | * with the provided extents via xfs_bmse_can_merge(). |
| 5493 | */ |
| 5494 | STATIC int |
| 5495 | xfs_bmse_merge( |
| 5496 | struct xfs_inode *ip, |
| 5497 | int whichfork, |
| 5498 | xfs_fileoff_t shift, /* shift fsb */ |
| 5499 | int current_ext, /* idx of gotp */ |
| 5500 | struct xfs_bmbt_rec_host *gotp, /* extent to shift */ |
| 5501 | struct xfs_bmbt_rec_host *leftp, /* preceding extent */ |
| 5502 | struct xfs_btree_cur *cur, |
| 5503 | int *logflags) /* output */ |
| 5504 | { |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5505 | struct xfs_bmbt_irec got; |
| 5506 | struct xfs_bmbt_irec left; |
| 5507 | xfs_filblks_t blockcount; |
| 5508 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5509 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5510 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5511 | xfs_bmbt_get_all(gotp, &got); |
| 5512 | xfs_bmbt_get_all(leftp, &left); |
| 5513 | blockcount = left.br_blockcount + got.br_blockcount; |
| 5514 | |
| 5515 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5516 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 5517 | ASSERT(xfs_bmse_can_merge(&left, &got, shift)); |
| 5518 | |
| 5519 | /* |
| 5520 | * Merge the in-core extents. Note that the host record pointers and |
| 5521 | * current_ext index are invalid once the extent has been removed via |
| 5522 | * xfs_iext_remove(). |
| 5523 | */ |
| 5524 | xfs_bmbt_set_blockcount(leftp, blockcount); |
| 5525 | xfs_iext_remove(ip, current_ext, 1, 0); |
| 5526 | |
| 5527 | /* |
| 5528 | * Update the on-disk extent count, the btree if necessary and log the |
| 5529 | * inode. |
| 5530 | */ |
| 5531 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5532 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5533 | *logflags |= XFS_ILOG_CORE; |
| 5534 | if (!cur) { |
| 5535 | *logflags |= XFS_ILOG_DEXT; |
| 5536 | return 0; |
| 5537 | } |
| 5538 | |
| 5539 | /* lookup and remove the extent to merge */ |
| 5540 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5541 | got.br_blockcount, &i); |
| 5542 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5543 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5544 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5545 | |
| 5546 | error = xfs_btree_delete(cur, &i); |
| 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 | /* lookup and update size of the previous extent */ |
| 5552 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock, |
| 5553 | left.br_blockcount, &i); |
| 5554 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5555 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5556 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5557 | |
| 5558 | left.br_blockcount = blockcount; |
| 5559 | |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5560 | return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock, |
| 5561 | left.br_blockcount, left.br_state); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5562 | } |
| 5563 | |
| 5564 | /* |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5565 | * Shift a single extent. |
| 5566 | */ |
| 5567 | STATIC int |
| 5568 | xfs_bmse_shift_one( |
| 5569 | struct xfs_inode *ip, |
| 5570 | int whichfork, |
| 5571 | xfs_fileoff_t offset_shift_fsb, |
| 5572 | int *current_ext, |
| 5573 | struct xfs_bmbt_rec_host *gotp, |
| 5574 | struct xfs_btree_cur *cur, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5575 | int *logflags, |
| 5576 | enum shift_direction direction) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5577 | { |
| 5578 | struct xfs_ifork *ifp; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5579 | struct xfs_mount *mp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5580 | xfs_fileoff_t startoff; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5581 | struct xfs_bmbt_rec_host *adj_irecp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5582 | struct xfs_bmbt_irec got; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5583 | struct xfs_bmbt_irec adj_irec; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5584 | int error; |
| 5585 | int i; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5586 | int total_extents; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5587 | |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5588 | mp = ip->i_mount; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5589 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5590 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5591 | |
| 5592 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5593 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5594 | /* delalloc extents should be prevented by caller */ |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5595 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5596 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5597 | if (direction == SHIFT_LEFT) { |
| 5598 | startoff = got.br_startoff - offset_shift_fsb; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5599 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5600 | /* |
| 5601 | * Check for merge if we've got an extent to the left, |
| 5602 | * otherwise make sure there's enough room at the start |
| 5603 | * of the file for the shift. |
| 5604 | */ |
| 5605 | if (!*current_ext) { |
| 5606 | if (got.br_startoff < offset_shift_fsb) |
| 5607 | return -EINVAL; |
| 5608 | goto update_current_ext; |
| 5609 | } |
| 5610 | /* |
| 5611 | * grab the left extent and check for a large |
| 5612 | * enough hole. |
| 5613 | */ |
| 5614 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1); |
| 5615 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5616 | |
| 5617 | if (startoff < |
| 5618 | adj_irec.br_startoff + adj_irec.br_blockcount) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5619 | return -EINVAL; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5620 | |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5621 | /* check whether to merge the extent or shift it down */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5622 | if (xfs_bmse_can_merge(&adj_irec, &got, |
| 5623 | offset_shift_fsb)) { |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5624 | return xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5625 | *current_ext, gotp, adj_irecp, |
| 5626 | cur, logflags); |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5627 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5628 | } else { |
| 5629 | startoff = got.br_startoff + offset_shift_fsb; |
| 5630 | /* nothing to move if this is the last extent */ |
| 5631 | if (*current_ext >= (total_extents - 1)) |
| 5632 | goto update_current_ext; |
| 5633 | /* |
| 5634 | * If this is not the last extent in the file, make sure there |
| 5635 | * is enough room between current extent and next extent for |
| 5636 | * accommodating the shift. |
| 5637 | */ |
| 5638 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1); |
| 5639 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5640 | if (startoff + got.br_blockcount > adj_irec.br_startoff) |
| 5641 | return -EINVAL; |
| 5642 | /* |
| 5643 | * Unlike a left shift (which involves a hole punch), |
| 5644 | * a right shift does not modify extent neighbors |
| 5645 | * in any way. We should never find mergeable extents |
| 5646 | * in this scenario. Check anyways and warn if we |
| 5647 | * encounter two extents that could be one. |
| 5648 | */ |
| 5649 | if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb)) |
| 5650 | WARN_ON_ONCE(1); |
| 5651 | } |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5652 | /* |
| 5653 | * Increment the extent index for the next iteration, update the start |
| 5654 | * offset of the in-core extent and update the btree if applicable. |
| 5655 | */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5656 | update_current_ext: |
| 5657 | if (direction == SHIFT_LEFT) |
| 5658 | (*current_ext)++; |
| 5659 | else |
| 5660 | (*current_ext)--; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5661 | xfs_bmbt_set_startoff(gotp, startoff); |
| 5662 | *logflags |= XFS_ILOG_CORE; |
| 5663 | if (!cur) { |
| 5664 | *logflags |= XFS_ILOG_DEXT; |
| 5665 | return 0; |
| 5666 | } |
| 5667 | |
| 5668 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5669 | got.br_blockcount, &i); |
| 5670 | if (error) |
| 5671 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5672 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5673 | |
| 5674 | got.br_startoff = startoff; |
kbuild test robot | d254aae | 2014-12-01 08:42:52 +1100 | [diff] [blame] | 5675 | return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5676 | got.br_blockcount, got.br_state); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5677 | } |
| 5678 | |
| 5679 | /* |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5680 | * Shift extent records to the left/right to cover/create a hole. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5681 | * |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5682 | * The maximum number of extents to be shifted in a single operation is |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5683 | * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5684 | * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb |
| 5685 | * is the length by which each extent is shifted. If there is no hole to shift |
| 5686 | * the extents into, this will be considered invalid operation and we abort |
| 5687 | * immediately. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5688 | */ |
| 5689 | int |
| 5690 | xfs_bmap_shift_extents( |
| 5691 | struct xfs_trans *tp, |
| 5692 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5693 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5694 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5695 | int *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5696 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5697 | xfs_fsblock_t *firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5698 | struct xfs_defer_ops *dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5699 | enum shift_direction direction, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5700 | int num_exts) |
| 5701 | { |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5702 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5703 | struct xfs_bmbt_rec_host *gotp; |
| 5704 | struct xfs_bmbt_irec got; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5705 | struct xfs_mount *mp = ip->i_mount; |
| 5706 | struct xfs_ifork *ifp; |
| 5707 | xfs_extnum_t nexts = 0; |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5708 | xfs_extnum_t current_ext; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5709 | xfs_extnum_t total_extents; |
| 5710 | xfs_extnum_t stop_extent; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5711 | int error = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5712 | int whichfork = XFS_DATA_FORK; |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5713 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5714 | |
| 5715 | if (unlikely(XFS_TEST_ERROR( |
| 5716 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5717 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5718 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 5719 | XFS_ERROR_REPORT("xfs_bmap_shift_extents", |
| 5720 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5721 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5722 | } |
| 5723 | |
| 5724 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5725 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5726 | |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5727 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5728 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5729 | ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT); |
| 5730 | ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5731 | |
| 5732 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5733 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5734 | /* Read in all the extents */ |
| 5735 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5736 | if (error) |
| 5737 | return error; |
| 5738 | } |
| 5739 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5740 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5741 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5742 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5743 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5744 | cur->bc_private.b.flags = 0; |
| 5745 | } |
| 5746 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5747 | /* |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 5748 | * There may be delalloc extents in the data fork before the range we |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5749 | * are collapsing out, so we cannot use the count of real extents here. |
| 5750 | * Instead we have to calculate it from the incore fork. |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 5751 | */ |
| 5752 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5753 | if (total_extents == 0) { |
| 5754 | *done = 1; |
| 5755 | goto del_cursor; |
| 5756 | } |
| 5757 | |
| 5758 | /* |
| 5759 | * In case of first right shift, we need to initialize next_fsb |
| 5760 | */ |
| 5761 | if (*next_fsb == NULLFSBLOCK) { |
| 5762 | gotp = xfs_iext_get_ext(ifp, total_extents - 1); |
| 5763 | xfs_bmbt_get_all(gotp, &got); |
| 5764 | *next_fsb = got.br_startoff; |
| 5765 | if (stop_fsb > *next_fsb) { |
| 5766 | *done = 1; |
| 5767 | goto del_cursor; |
| 5768 | } |
| 5769 | } |
| 5770 | |
| 5771 | /* Lookup the extent index at which we have to stop */ |
| 5772 | if (direction == SHIFT_RIGHT) { |
| 5773 | gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent); |
| 5774 | /* Make stop_extent exclusive of shift range */ |
| 5775 | stop_extent--; |
| 5776 | } else |
| 5777 | stop_extent = total_extents; |
| 5778 | |
| 5779 | /* |
| 5780 | * Look up the extent index for the fsb where we start shifting. We can |
| 5781 | * henceforth iterate with current_ext as extent list changes are locked |
| 5782 | * out via ilock. |
| 5783 | * |
| 5784 | * gotp can be null in 2 cases: 1) if there are no extents or 2) |
| 5785 | * *next_fsb lies in a hole beyond which there are no extents. Either |
| 5786 | * way, we are done. |
| 5787 | */ |
| 5788 | gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext); |
| 5789 | if (!gotp) { |
| 5790 | *done = 1; |
| 5791 | goto del_cursor; |
| 5792 | } |
| 5793 | |
| 5794 | /* some sanity checking before we finally start shifting extents */ |
| 5795 | if ((direction == SHIFT_LEFT && current_ext >= stop_extent) || |
| 5796 | (direction == SHIFT_RIGHT && current_ext <= stop_extent)) { |
| 5797 | error = -EIO; |
| 5798 | goto del_cursor; |
| 5799 | } |
| 5800 | |
| 5801 | while (nexts++ < num_exts) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5802 | error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5803 | ¤t_ext, gotp, cur, &logflags, |
| 5804 | direction); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5805 | if (error) |
| 5806 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5807 | /* |
| 5808 | * If there was an extent merge during the shift, the extent |
| 5809 | * count can change. Update the total and grade the next record. |
| 5810 | */ |
| 5811 | if (direction == SHIFT_LEFT) { |
| 5812 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
| 5813 | stop_extent = total_extents; |
| 5814 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5815 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5816 | if (current_ext == stop_extent) { |
| 5817 | *done = 1; |
| 5818 | *next_fsb = NULLFSBLOCK; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5819 | break; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5820 | } |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5821 | gotp = xfs_iext_get_ext(ifp, current_ext); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5822 | } |
| 5823 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5824 | if (!*done) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5825 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5826 | *next_fsb = got.br_startoff; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5827 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5828 | |
| 5829 | del_cursor: |
| 5830 | if (cur) |
| 5831 | xfs_btree_del_cursor(cur, |
| 5832 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5833 | |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5834 | if (logflags) |
| 5835 | xfs_trans_log_inode(tp, ip, logflags); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5836 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5837 | return error; |
| 5838 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5839 | |
| 5840 | /* |
| 5841 | * Splits an extent into two extents at split_fsb block such that it is |
| 5842 | * the first block of the current_ext. @current_ext is a target extent |
| 5843 | * to be split. @split_fsb is a block where the extents is split. |
| 5844 | * If split_fsb lies in a hole or the first block of extents, just return 0. |
| 5845 | */ |
| 5846 | STATIC int |
| 5847 | xfs_bmap_split_extent_at( |
| 5848 | struct xfs_trans *tp, |
| 5849 | struct xfs_inode *ip, |
| 5850 | xfs_fileoff_t split_fsb, |
| 5851 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5852 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5853 | { |
| 5854 | int whichfork = XFS_DATA_FORK; |
| 5855 | struct xfs_btree_cur *cur = NULL; |
| 5856 | struct xfs_bmbt_rec_host *gotp; |
| 5857 | struct xfs_bmbt_irec got; |
| 5858 | struct xfs_bmbt_irec new; /* split extent */ |
| 5859 | struct xfs_mount *mp = ip->i_mount; |
| 5860 | struct xfs_ifork *ifp; |
| 5861 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
| 5862 | xfs_extnum_t current_ext; |
| 5863 | int error = 0; |
| 5864 | int logflags = 0; |
| 5865 | int i = 0; |
| 5866 | |
| 5867 | if (unlikely(XFS_TEST_ERROR( |
| 5868 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5869 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5870 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 5871 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5872 | XFS_ERRLEVEL_LOW, mp); |
| 5873 | return -EFSCORRUPTED; |
| 5874 | } |
| 5875 | |
| 5876 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5877 | return -EIO; |
| 5878 | |
| 5879 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5880 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5881 | /* Read in all the extents */ |
| 5882 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5883 | if (error) |
| 5884 | return error; |
| 5885 | } |
| 5886 | |
| 5887 | /* |
| 5888 | * gotp can be null in 2 cases: 1) if there are no extents |
| 5889 | * or 2) split_fsb lies in a hole beyond which there are |
| 5890 | * no extents. Either way, we are done. |
| 5891 | */ |
| 5892 | gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext); |
| 5893 | if (!gotp) |
| 5894 | return 0; |
| 5895 | |
| 5896 | xfs_bmbt_get_all(gotp, &got); |
| 5897 | |
| 5898 | /* |
| 5899 | * Check split_fsb lies in a hole or the start boundary offset |
| 5900 | * of the extent. |
| 5901 | */ |
| 5902 | if (got.br_startoff >= split_fsb) |
| 5903 | return 0; |
| 5904 | |
| 5905 | gotblkcnt = split_fsb - got.br_startoff; |
| 5906 | new.br_startoff = split_fsb; |
| 5907 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5908 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5909 | new.br_state = got.br_state; |
| 5910 | |
| 5911 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5912 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5913 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5914 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5915 | cur->bc_private.b.flags = 0; |
| 5916 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 5917 | got.br_startblock, |
| 5918 | got.br_blockcount, |
| 5919 | &i); |
| 5920 | if (error) |
| 5921 | goto del_cursor; |
| 5922 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5923 | } |
| 5924 | |
| 5925 | xfs_bmbt_set_blockcount(gotp, gotblkcnt); |
| 5926 | got.br_blockcount = gotblkcnt; |
| 5927 | |
| 5928 | logflags = XFS_ILOG_CORE; |
| 5929 | if (cur) { |
| 5930 | error = xfs_bmbt_update(cur, got.br_startoff, |
| 5931 | got.br_startblock, |
| 5932 | got.br_blockcount, |
| 5933 | got.br_state); |
| 5934 | if (error) |
| 5935 | goto del_cursor; |
| 5936 | } else |
| 5937 | logflags |= XFS_ILOG_DEXT; |
| 5938 | |
| 5939 | /* Add new extent */ |
| 5940 | current_ext++; |
| 5941 | xfs_iext_insert(ip, current_ext, 1, &new, 0); |
| 5942 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5943 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5944 | |
| 5945 | if (cur) { |
| 5946 | error = xfs_bmbt_lookup_eq(cur, new.br_startoff, |
| 5947 | new.br_startblock, new.br_blockcount, |
| 5948 | &i); |
| 5949 | if (error) |
| 5950 | goto del_cursor; |
| 5951 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
| 5952 | cur->bc_rec.b.br_state = new.br_state; |
| 5953 | |
| 5954 | error = xfs_btree_insert(cur, &i); |
| 5955 | if (error) |
| 5956 | goto del_cursor; |
| 5957 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5958 | } |
| 5959 | |
| 5960 | /* |
| 5961 | * Convert to a btree if necessary. |
| 5962 | */ |
| 5963 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 5964 | int tmp_logflags; /* partial log flag return val */ |
| 5965 | |
| 5966 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5967 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5968 | &cur, 0, &tmp_logflags, whichfork); |
| 5969 | logflags |= tmp_logflags; |
| 5970 | } |
| 5971 | |
| 5972 | del_cursor: |
| 5973 | if (cur) { |
| 5974 | cur->bc_private.b.allocated = 0; |
| 5975 | xfs_btree_del_cursor(cur, |
| 5976 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5977 | } |
| 5978 | |
| 5979 | if (logflags) |
| 5980 | xfs_trans_log_inode(tp, ip, logflags); |
| 5981 | return error; |
| 5982 | } |
| 5983 | |
| 5984 | int |
| 5985 | xfs_bmap_split_extent( |
| 5986 | struct xfs_inode *ip, |
| 5987 | xfs_fileoff_t split_fsb) |
| 5988 | { |
| 5989 | struct xfs_mount *mp = ip->i_mount; |
| 5990 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5991 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5992 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5993 | int error; |
| 5994 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 5995 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 5996 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 5997 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5998 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5999 | |
| 6000 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6001 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6002 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6003 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6004 | |
| 6005 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6006 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6007 | if (error) |
| 6008 | goto out; |
| 6009 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6010 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6011 | if (error) |
| 6012 | goto out; |
| 6013 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6014 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6015 | |
| 6016 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6017 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6018 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6019 | return error; |
| 6020 | } |