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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 52 | |
| 53 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 54 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | */ |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 58 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 59 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 61 | void |
| 62 | xfs_bmap_compute_maxlevels( |
| 63 | xfs_mount_t *mp, /* file system mount structure */ |
| 64 | int whichfork) /* data or attr fork */ |
| 65 | { |
| 66 | int level; /* btree level */ |
| 67 | uint maxblocks; /* max blocks at this level */ |
| 68 | uint maxleafents; /* max leaf entries possible */ |
| 69 | int maxrootrecs; /* max records in root block */ |
| 70 | int minleafrecs; /* min records in leaf block */ |
| 71 | int minnoderecs; /* min records in node block */ |
| 72 | int sz; /* root block size */ |
| 73 | |
| 74 | /* |
| 75 | * The maximum number of extents in a file, hence the maximum |
| 76 | * number of leaf entries, is controlled by the type of di_nextents |
| 77 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 78 | * (a signed 16-bit number, xfs_aextnum_t). |
| 79 | * |
| 80 | * Note that we can no longer assume that if we are in ATTR1 that |
| 81 | * the fork offset of all the inodes will be |
| 82 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 83 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 84 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 85 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 86 | * of a minimum size available. |
| 87 | */ |
| 88 | if (whichfork == XFS_DATA_FORK) { |
| 89 | maxleafents = MAXEXTNUM; |
| 90 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 91 | } else { |
| 92 | maxleafents = MAXAEXTNUM; |
| 93 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 94 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 95 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 96 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 97 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 98 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 99 | for (level = 1; maxblocks > 1; level++) { |
| 100 | if (maxblocks <= maxrootrecs) |
| 101 | maxblocks = 1; |
| 102 | else |
| 103 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 104 | } |
| 105 | mp->m_bm_maxlevels[whichfork] = level; |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 108 | STATIC int /* error */ |
| 109 | xfs_bmbt_lookup_eq( |
| 110 | struct xfs_btree_cur *cur, |
| 111 | xfs_fileoff_t off, |
| 112 | xfs_fsblock_t bno, |
| 113 | xfs_filblks_t len, |
| 114 | int *stat) /* success/failure */ |
| 115 | { |
| 116 | cur->bc_rec.b.br_startoff = off; |
| 117 | cur->bc_rec.b.br_startblock = bno; |
| 118 | cur->bc_rec.b.br_blockcount = len; |
| 119 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 120 | } |
| 121 | |
| 122 | STATIC int /* error */ |
| 123 | xfs_bmbt_lookup_ge( |
| 124 | struct xfs_btree_cur *cur, |
| 125 | xfs_fileoff_t off, |
| 126 | xfs_fsblock_t bno, |
| 127 | xfs_filblks_t len, |
| 128 | int *stat) /* success/failure */ |
| 129 | { |
| 130 | cur->bc_rec.b.br_startoff = off; |
| 131 | cur->bc_rec.b.br_startblock = bno; |
| 132 | cur->bc_rec.b.br_blockcount = len; |
| 133 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 134 | } |
| 135 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 136 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 137 | * Check if the inode needs to be converted to btree format. |
| 138 | */ |
| 139 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 140 | { |
| 141 | return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 142 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 143 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Check if the inode should be converted to extent format. |
| 148 | */ |
| 149 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 150 | { |
| 151 | return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 152 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 153 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Update the record referred to by cur to the value given |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 158 | * by [off, bno, len, state]. |
| 159 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 160 | */ |
| 161 | STATIC int |
| 162 | xfs_bmbt_update( |
| 163 | struct xfs_btree_cur *cur, |
| 164 | xfs_fileoff_t off, |
| 165 | xfs_fsblock_t bno, |
| 166 | xfs_filblks_t len, |
| 167 | xfs_exntst_t state) |
| 168 | { |
| 169 | union xfs_btree_rec rec; |
| 170 | |
| 171 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 172 | return xfs_btree_update(cur, &rec); |
| 173 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 176 | * Compute the worst-case number of indirect blocks that will be used |
| 177 | * for ip's delayed extent of length "len". |
| 178 | */ |
| 179 | STATIC xfs_filblks_t |
| 180 | xfs_bmap_worst_indlen( |
| 181 | xfs_inode_t *ip, /* incore inode pointer */ |
| 182 | xfs_filblks_t len) /* delayed extent length */ |
| 183 | { |
| 184 | int level; /* btree level number */ |
| 185 | int maxrecs; /* maximum record count at this level */ |
| 186 | xfs_mount_t *mp; /* mount structure */ |
| 187 | xfs_filblks_t rval; /* return value */ |
| 188 | |
| 189 | mp = ip->i_mount; |
| 190 | maxrecs = mp->m_bmap_dmxr[0]; |
| 191 | for (level = 0, rval = 0; |
| 192 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 193 | level++) { |
| 194 | len += maxrecs - 1; |
| 195 | do_div(len, maxrecs); |
| 196 | rval += len; |
| 197 | if (len == 1) |
| 198 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 199 | level - 1; |
| 200 | if (level == 0) |
| 201 | maxrecs = mp->m_bmap_dmxr[1]; |
| 202 | } |
| 203 | return rval; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Calculate the default attribute fork offset for newly created inodes. |
| 208 | */ |
| 209 | uint |
| 210 | xfs_default_attroffset( |
| 211 | struct xfs_inode *ip) |
| 212 | { |
| 213 | struct xfs_mount *mp = ip->i_mount; |
| 214 | uint offset; |
| 215 | |
| 216 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 217 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 218 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 219 | } else { |
| 220 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 221 | } |
| 222 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 223 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 224 | return offset; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Helper routine to reset inode di_forkoff field when switching |
| 229 | * attribute fork from local to extent format - we reset it where |
| 230 | * possible to make space available for inline data fork extents. |
| 231 | */ |
| 232 | STATIC void |
| 233 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 234 | xfs_inode_t *ip, |
| 235 | int whichfork) |
| 236 | { |
| 237 | if (whichfork == XFS_ATTR_FORK && |
| 238 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 239 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 240 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 241 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 242 | |
| 243 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 244 | ip->i_d.di_forkoff = dfl_forkoff; |
| 245 | } |
| 246 | } |
| 247 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 248 | #ifdef DEBUG |
| 249 | STATIC struct xfs_buf * |
| 250 | xfs_bmap_get_bp( |
| 251 | struct xfs_btree_cur *cur, |
| 252 | xfs_fsblock_t bno) |
| 253 | { |
| 254 | struct xfs_log_item_desc *lidp; |
| 255 | int i; |
| 256 | |
| 257 | if (!cur) |
| 258 | return NULL; |
| 259 | |
| 260 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 261 | if (!cur->bc_bufs[i]) |
| 262 | break; |
| 263 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 264 | return cur->bc_bufs[i]; |
| 265 | } |
| 266 | |
| 267 | /* Chase down all the log items to see if the bp is there */ |
| 268 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 269 | struct xfs_buf_log_item *bip; |
| 270 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 271 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 272 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 273 | return bip->bli_buf; |
| 274 | } |
| 275 | |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | STATIC void |
| 280 | xfs_check_block( |
| 281 | struct xfs_btree_block *block, |
| 282 | xfs_mount_t *mp, |
| 283 | int root, |
| 284 | short sz) |
| 285 | { |
| 286 | int i, j, dmxr; |
| 287 | __be64 *pp, *thispa; /* pointer to block address */ |
| 288 | xfs_bmbt_key_t *prevp, *keyp; |
| 289 | |
| 290 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 291 | |
| 292 | prevp = NULL; |
| 293 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 294 | dmxr = mp->m_bmap_dmxr[0]; |
| 295 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 296 | |
| 297 | if (prevp) { |
| 298 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 299 | be64_to_cpu(keyp->br_startoff)); |
| 300 | } |
| 301 | prevp = keyp; |
| 302 | |
| 303 | /* |
| 304 | * Compare the block numbers to see if there are dups. |
| 305 | */ |
| 306 | if (root) |
| 307 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 308 | else |
| 309 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 310 | |
| 311 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 312 | if (root) |
| 313 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 314 | else |
| 315 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 316 | if (*thispa == *pp) { |
| 317 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 318 | __func__, j, i, |
| 319 | (unsigned long long)be64_to_cpu(*thispa)); |
| 320 | panic("%s: ptrs are equal in node\n", |
| 321 | __func__); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * 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] | 329 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 330 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 331 | * them. The btree record ordering checks will still be done, so for such large |
| 332 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 333 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 334 | STATIC void |
| 335 | xfs_bmap_check_leaf_extents( |
| 336 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 337 | xfs_inode_t *ip, /* incore inode pointer */ |
| 338 | int whichfork) /* data or attr fork */ |
| 339 | { |
| 340 | struct xfs_btree_block *block; /* current btree block */ |
| 341 | xfs_fsblock_t bno; /* block # of "block" */ |
| 342 | xfs_buf_t *bp; /* buffer for "block" */ |
| 343 | int error; /* error return value */ |
| 344 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 345 | xfs_ifork_t *ifp; /* fork structure */ |
| 346 | int level; /* btree level, for checking */ |
| 347 | xfs_mount_t *mp; /* file system mount structure */ |
| 348 | __be64 *pp; /* pointer to block address */ |
| 349 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 350 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 351 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 352 | int bp_release = 0; |
| 353 | |
| 354 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 355 | return; |
| 356 | } |
| 357 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 358 | /* skip large extent count inodes */ |
| 359 | if (ip->i_d.di_nextents > 10000) |
| 360 | return; |
| 361 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 362 | bno = NULLFSBLOCK; |
| 363 | mp = ip->i_mount; |
| 364 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 365 | block = ifp->if_broot; |
| 366 | /* |
| 367 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 368 | */ |
| 369 | level = be16_to_cpu(block->bb_level); |
| 370 | ASSERT(level > 0); |
| 371 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 372 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 373 | bno = be64_to_cpu(*pp); |
| 374 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 375 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 376 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 377 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 378 | |
| 379 | /* |
| 380 | * Go down the tree until leaf level is reached, following the first |
| 381 | * pointer (leftmost) at each level. |
| 382 | */ |
| 383 | while (level-- > 0) { |
| 384 | /* See if buf is in cur first */ |
| 385 | bp_release = 0; |
| 386 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 387 | if (!bp) { |
| 388 | bp_release = 1; |
| 389 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 390 | XFS_BMAP_BTREE_REF, |
| 391 | &xfs_bmbt_buf_ops); |
| 392 | if (error) |
| 393 | goto error_norelse; |
| 394 | } |
| 395 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 396 | if (level == 0) |
| 397 | break; |
| 398 | |
| 399 | /* |
| 400 | * Check this block for basic sanity (increasing keys and |
| 401 | * no duplicate blocks). |
| 402 | */ |
| 403 | |
| 404 | xfs_check_block(block, mp, 0, 0); |
| 405 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 406 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 407 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 408 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 409 | if (bp_release) { |
| 410 | bp_release = 0; |
| 411 | xfs_trans_brelse(NULL, bp); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 417 | */ |
| 418 | i = 0; |
| 419 | |
| 420 | /* |
| 421 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 422 | */ |
| 423 | for (;;) { |
| 424 | xfs_fsblock_t nextbno; |
| 425 | xfs_extnum_t num_recs; |
| 426 | |
| 427 | |
| 428 | num_recs = xfs_btree_get_numrecs(block); |
| 429 | |
| 430 | /* |
| 431 | * Read-ahead the next leaf block, if any. |
| 432 | */ |
| 433 | |
| 434 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 435 | |
| 436 | /* |
| 437 | * Check all the extents to make sure they are OK. |
| 438 | * If we had a previous block, the last entry should |
| 439 | * conform with the first entry in this one. |
| 440 | */ |
| 441 | |
| 442 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 443 | if (i) { |
| 444 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 445 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 446 | xfs_bmbt_disk_get_startoff(ep)); |
| 447 | } |
| 448 | for (j = 1; j < num_recs; j++) { |
| 449 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 450 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 451 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 452 | xfs_bmbt_disk_get_startoff(nextp)); |
| 453 | ep = nextp; |
| 454 | } |
| 455 | |
| 456 | last = *ep; |
| 457 | i += num_recs; |
| 458 | if (bp_release) { |
| 459 | bp_release = 0; |
| 460 | xfs_trans_brelse(NULL, bp); |
| 461 | } |
| 462 | bno = nextbno; |
| 463 | /* |
| 464 | * If we've reached the end, stop. |
| 465 | */ |
| 466 | if (bno == NULLFSBLOCK) |
| 467 | break; |
| 468 | |
| 469 | bp_release = 0; |
| 470 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 471 | if (!bp) { |
| 472 | bp_release = 1; |
| 473 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 474 | XFS_BMAP_BTREE_REF, |
| 475 | &xfs_bmbt_buf_ops); |
| 476 | if (error) |
| 477 | goto error_norelse; |
| 478 | } |
| 479 | block = XFS_BUF_TO_BLOCK(bp); |
| 480 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 481 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 482 | return; |
| 483 | |
| 484 | error0: |
| 485 | xfs_warn(mp, "%s: at error0", __func__); |
| 486 | if (bp_release) |
| 487 | xfs_trans_brelse(NULL, bp); |
| 488 | error_norelse: |
| 489 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 490 | __func__, i); |
| 491 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Add bmap trace insert entries for all the contents of the extent records. |
| 497 | */ |
| 498 | void |
| 499 | xfs_bmap_trace_exlist( |
| 500 | xfs_inode_t *ip, /* incore inode pointer */ |
| 501 | xfs_extnum_t cnt, /* count of entries in the list */ |
| 502 | int whichfork, /* data or attr fork */ |
| 503 | unsigned long caller_ip) |
| 504 | { |
| 505 | xfs_extnum_t idx; /* extent record index */ |
| 506 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 507 | int state = 0; |
| 508 | |
| 509 | if (whichfork == XFS_ATTR_FORK) |
| 510 | state |= BMAP_ATTRFORK; |
| 511 | |
| 512 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 513 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
| 514 | for (idx = 0; idx < cnt; idx++) |
| 515 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 520 | * given the caller's original parameters. Specifically check the |
| 521 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 522 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 523 | */ |
| 524 | STATIC void |
| 525 | xfs_bmap_validate_ret( |
| 526 | xfs_fileoff_t bno, |
| 527 | xfs_filblks_t len, |
| 528 | int flags, |
| 529 | xfs_bmbt_irec_t *mval, |
| 530 | int nmap, |
| 531 | int ret_nmap) |
| 532 | { |
| 533 | int i; /* index to map values */ |
| 534 | |
| 535 | ASSERT(ret_nmap <= nmap); |
| 536 | |
| 537 | for (i = 0; i < ret_nmap; i++) { |
| 538 | ASSERT(mval[i].br_blockcount > 0); |
| 539 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 540 | ASSERT(mval[i].br_startoff >= bno); |
| 541 | ASSERT(mval[i].br_blockcount <= len); |
| 542 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 543 | bno + len); |
| 544 | } else { |
| 545 | ASSERT(mval[i].br_startoff < bno + len); |
| 546 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 547 | bno); |
| 548 | } |
| 549 | ASSERT(i == 0 || |
| 550 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 551 | mval[i].br_startoff); |
| 552 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 553 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 554 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 555 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | #else |
| 560 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
| 561 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 562 | #endif /* DEBUG */ |
| 563 | |
| 564 | /* |
| 565 | * bmap free list manipulation functions |
| 566 | */ |
| 567 | |
| 568 | /* |
| 569 | * Add the extent to the list of extents to be free at transaction end. |
| 570 | * The list is maintained sorted (by block number). |
| 571 | */ |
| 572 | void |
| 573 | xfs_bmap_add_free( |
Darrick J. Wong | 59bad07 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 574 | struct xfs_mount *mp, /* mount point structure */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 575 | struct xfs_defer_ops *dfops, /* list of extents */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 576 | xfs_fsblock_t bno, /* fs block number of extent */ |
Darrick J. Wong | 59bad07 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 577 | xfs_filblks_t len) /* length of extent */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 578 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 579 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 580 | #ifdef DEBUG |
| 581 | xfs_agnumber_t agno; |
| 582 | xfs_agblock_t agbno; |
| 583 | |
| 584 | ASSERT(bno != NULLFSBLOCK); |
| 585 | ASSERT(len > 0); |
| 586 | ASSERT(len <= MAXEXTLEN); |
| 587 | ASSERT(!isnullstartblock(bno)); |
| 588 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 589 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 590 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 591 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 592 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 593 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 594 | #endif |
| 595 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 596 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 597 | new->xefi_startblock = bno; |
| 598 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 599 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Inode fork format manipulation functions |
| 604 | */ |
| 605 | |
| 606 | /* |
| 607 | * Transform a btree format file with only one leaf node, where the |
| 608 | * extents list will fit in the inode, into an extents format file. |
| 609 | * Since the file extents are already in-core, all we have to do is |
| 610 | * give up the space for the btree root and pitch the leaf block. |
| 611 | */ |
| 612 | STATIC int /* error */ |
| 613 | xfs_bmap_btree_to_extents( |
| 614 | xfs_trans_t *tp, /* transaction pointer */ |
| 615 | xfs_inode_t *ip, /* incore inode pointer */ |
| 616 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 617 | int *logflagsp, /* inode logging flags */ |
| 618 | int whichfork) /* data or attr fork */ |
| 619 | { |
| 620 | /* REFERENCED */ |
| 621 | struct xfs_btree_block *cblock;/* child btree block */ |
| 622 | xfs_fsblock_t cbno; /* child block number */ |
| 623 | xfs_buf_t *cbp; /* child block's buffer */ |
| 624 | int error; /* error return value */ |
| 625 | xfs_ifork_t *ifp; /* inode fork data */ |
| 626 | xfs_mount_t *mp; /* mount point structure */ |
| 627 | __be64 *pp; /* ptr to block address */ |
| 628 | struct xfs_btree_block *rblock;/* root btree block */ |
| 629 | |
| 630 | mp = ip->i_mount; |
| 631 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 632 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 633 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 634 | rblock = ifp->if_broot; |
| 635 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 636 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 637 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 638 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 639 | cbno = be64_to_cpu(*pp); |
| 640 | *logflagsp = 0; |
| 641 | #ifdef DEBUG |
| 642 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
| 643 | return error; |
| 644 | #endif |
| 645 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 646 | &xfs_bmbt_buf_ops); |
| 647 | if (error) |
| 648 | return error; |
| 649 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 650 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 651 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 652 | xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 653 | ip->i_d.di_nblocks--; |
| 654 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 655 | xfs_trans_binval(tp, cbp); |
| 656 | if (cur->bc_bufs[0] == cbp) |
| 657 | cur->bc_bufs[0] = NULL; |
| 658 | xfs_iroot_realloc(ip, -1, whichfork); |
| 659 | ASSERT(ifp->if_broot == NULL); |
| 660 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 661 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 662 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Convert an extents-format file into a btree-format file. |
| 668 | * The new file will have a root block (in the inode) and a single child block. |
| 669 | */ |
| 670 | STATIC int /* error */ |
| 671 | xfs_bmap_extents_to_btree( |
| 672 | xfs_trans_t *tp, /* transaction pointer */ |
| 673 | xfs_inode_t *ip, /* incore inode pointer */ |
| 674 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 675 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 676 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 677 | int wasdel, /* converting a delayed alloc */ |
| 678 | int *logflagsp, /* inode logging flags */ |
| 679 | int whichfork) /* data or attr fork */ |
| 680 | { |
| 681 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 682 | xfs_buf_t *abp; /* buffer for ablock */ |
| 683 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 684 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 685 | struct xfs_btree_block *block; /* btree root block */ |
| 686 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 687 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 688 | int error; /* error return value */ |
| 689 | xfs_extnum_t i, cnt; /* extent record index */ |
| 690 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 691 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 692 | xfs_mount_t *mp; /* mount structure */ |
| 693 | xfs_extnum_t nextents; /* number of file extents */ |
| 694 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 695 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 696 | mp = ip->i_mount; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 697 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 698 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 699 | |
| 700 | /* |
| 701 | * Make space in the inode incore. |
| 702 | */ |
| 703 | xfs_iroot_realloc(ip, 1, whichfork); |
| 704 | ifp->if_flags |= XFS_IFBROOT; |
| 705 | |
| 706 | /* |
| 707 | * Fill in the root. |
| 708 | */ |
| 709 | block = ifp->if_broot; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 710 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 711 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 712 | XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino, |
| 713 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); |
| 714 | else |
| 715 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 716 | XFS_BMAP_MAGIC, 1, 1, ip->i_ino, |
| 717 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 718 | |
| 719 | /* |
| 720 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 721 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 722 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 723 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 724 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 725 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 726 | /* |
| 727 | * Convert to a btree with two levels, one record in root. |
| 728 | */ |
| 729 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 730 | memset(&args, 0, sizeof(args)); |
| 731 | args.tp = tp; |
| 732 | args.mp = mp; |
| 733 | args.firstblock = *firstblock; |
| 734 | if (*firstblock == NULLFSBLOCK) { |
| 735 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 736 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 737 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 738 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 739 | args.fsbno = *firstblock; |
| 740 | } else { |
| 741 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 742 | args.fsbno = *firstblock; |
| 743 | } |
| 744 | args.minlen = args.maxlen = args.prod = 1; |
| 745 | args.wasdel = wasdel; |
| 746 | *logflagsp = 0; |
| 747 | if ((error = xfs_alloc_vextent(&args))) { |
| 748 | xfs_iroot_realloc(ip, -1, whichfork); |
| 749 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 750 | return error; |
| 751 | } |
| 752 | /* |
| 753 | * Allocation can't fail, the space was reserved. |
| 754 | */ |
| 755 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 756 | ASSERT(*firstblock == NULLFSBLOCK || |
| 757 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 758 | (dfops->dop_low && |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 759 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 760 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 761 | cur->bc_private.b.allocated++; |
| 762 | ip->i_d.di_nblocks++; |
| 763 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 764 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 765 | /* |
| 766 | * Fill in the child block. |
| 767 | */ |
| 768 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 769 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 770 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 771 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 772 | XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino, |
| 773 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); |
| 774 | else |
| 775 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 776 | XFS_BMAP_MAGIC, 0, 0, ip->i_ino, |
| 777 | XFS_BTREE_LONG_PTRS); |
| 778 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 779 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 780 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 781 | for (cnt = i = 0; i < nextents; i++) { |
| 782 | ep = xfs_iext_get_ext(ifp, i); |
| 783 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
| 784 | arp->l0 = cpu_to_be64(ep->l0); |
| 785 | arp->l1 = cpu_to_be64(ep->l1); |
| 786 | arp++; cnt++; |
| 787 | } |
| 788 | } |
| 789 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 790 | xfs_btree_set_numrecs(ablock, cnt); |
| 791 | |
| 792 | /* |
| 793 | * Fill in the root key and pointer. |
| 794 | */ |
| 795 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 796 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 797 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 798 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 799 | be16_to_cpu(block->bb_level))); |
| 800 | *pp = cpu_to_be64(args.fsbno); |
| 801 | |
| 802 | /* |
| 803 | * Do all this logging at the end so that |
| 804 | * the root is at the right level. |
| 805 | */ |
| 806 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 807 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 808 | ASSERT(*curp == NULL); |
| 809 | *curp = cur; |
| 810 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * Convert a local file to an extents file. |
| 816 | * This code is out of bounds for data forks of regular files, |
| 817 | * since the file data needs to get logged so things will stay consistent. |
| 818 | * (The bmap-level manipulations are ok, though). |
| 819 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 820 | void |
| 821 | xfs_bmap_local_to_extents_empty( |
| 822 | struct xfs_inode *ip, |
| 823 | int whichfork) |
| 824 | { |
| 825 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 826 | |
| 827 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 828 | ASSERT(ifp->if_bytes == 0); |
| 829 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 830 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 831 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 832 | ifp->if_flags &= ~XFS_IFINLINE; |
| 833 | ifp->if_flags |= XFS_IFEXTENTS; |
| 834 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 835 | } |
| 836 | |
| 837 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 838 | STATIC int /* error */ |
| 839 | xfs_bmap_local_to_extents( |
| 840 | xfs_trans_t *tp, /* transaction pointer */ |
| 841 | xfs_inode_t *ip, /* incore inode pointer */ |
| 842 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 843 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 844 | int *logflagsp, /* inode logging flags */ |
| 845 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 846 | void (*init_fn)(struct xfs_trans *tp, |
| 847 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 848 | struct xfs_inode *ip, |
| 849 | struct xfs_ifork *ifp)) |
| 850 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 851 | int error = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 852 | int flags; /* logging flags returned */ |
| 853 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 854 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 855 | xfs_buf_t *bp; /* buffer for extent block */ |
| 856 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 857 | |
| 858 | /* |
| 859 | * We don't want to deal with the case of keeping inode data inline yet. |
| 860 | * So sending the data fork of a regular inode is invalid. |
| 861 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 862 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 863 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 864 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 865 | |
| 866 | if (!ifp->if_bytes) { |
| 867 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 868 | flags = XFS_ILOG_CORE; |
| 869 | goto done; |
| 870 | } |
| 871 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 872 | flags = 0; |
| 873 | error = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 874 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
| 875 | XFS_IFINLINE); |
| 876 | memset(&args, 0, sizeof(args)); |
| 877 | args.tp = tp; |
| 878 | args.mp = ip->i_mount; |
| 879 | args.firstblock = *firstblock; |
| 880 | /* |
| 881 | * Allocate a block. We know we need only one, since the |
| 882 | * file currently fits in an inode. |
| 883 | */ |
| 884 | if (*firstblock == NULLFSBLOCK) { |
| 885 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 886 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 887 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 888 | args.fsbno = *firstblock; |
| 889 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 890 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 891 | args.total = total; |
| 892 | args.minlen = args.maxlen = args.prod = 1; |
| 893 | error = xfs_alloc_vextent(&args); |
| 894 | if (error) |
| 895 | goto done; |
| 896 | |
| 897 | /* Can't fail, the space was reserved. */ |
| 898 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 899 | ASSERT(args.len == 1); |
| 900 | *firstblock = args.fsbno; |
| 901 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 902 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 903 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 904 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 905 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 906 | * The callout is responsible for logging because the remote format |
| 907 | * might differ from the local format and thus we don't know how much to |
| 908 | * log here. Note that init_fn must also set the buffer log item type |
| 909 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 910 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 911 | init_fn(tp, bp, ip, ifp); |
| 912 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 913 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 914 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 915 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 916 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 917 | |
| 918 | xfs_iext_add(ifp, 0, 1); |
| 919 | ep = xfs_iext_get_ext(ifp, 0); |
| 920 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
| 921 | trace_xfs_bmap_post_update(ip, 0, |
| 922 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 923 | _THIS_IP_); |
| 924 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 925 | ip->i_d.di_nblocks = 1; |
| 926 | xfs_trans_mod_dquot_byino(tp, ip, |
| 927 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 928 | flags |= xfs_ilog_fext(whichfork); |
| 929 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 930 | done: |
| 931 | *logflagsp = flags; |
| 932 | return error; |
| 933 | } |
| 934 | |
| 935 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 937 | */ |
| 938 | STATIC int /* error */ |
| 939 | xfs_bmap_add_attrfork_btree( |
| 940 | xfs_trans_t *tp, /* transaction pointer */ |
| 941 | xfs_inode_t *ip, /* incore inode pointer */ |
| 942 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 943 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | int *flags) /* inode logging flags */ |
| 945 | { |
| 946 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 947 | int error; /* error return value */ |
| 948 | xfs_mount_t *mp; /* file system mount struct */ |
| 949 | int stat; /* newroot status */ |
| 950 | |
| 951 | mp = ip->i_mount; |
| 952 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 953 | *flags |= XFS_ILOG_DBROOT; |
| 954 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 955 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 956 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | cur->bc_private.b.firstblock = *firstblock; |
| 958 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 959 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 960 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 961 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 962 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | goto error0; |
| 964 | if (stat == 0) { |
| 965 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 966 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } |
| 968 | *firstblock = cur->bc_private.b.firstblock; |
| 969 | cur->bc_private.b.allocated = 0; |
| 970 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 971 | } |
| 972 | return 0; |
| 973 | error0: |
| 974 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 975 | return error; |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 980 | */ |
| 981 | STATIC int /* error */ |
| 982 | xfs_bmap_add_attrfork_extents( |
| 983 | xfs_trans_t *tp, /* transaction pointer */ |
| 984 | xfs_inode_t *ip, /* incore inode pointer */ |
| 985 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 986 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | int *flags) /* inode logging flags */ |
| 988 | { |
| 989 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 990 | int error; /* error return value */ |
| 991 | |
| 992 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 993 | return 0; |
| 994 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 995 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | flags, XFS_DATA_FORK); |
| 997 | if (cur) { |
| 998 | cur->bc_private.b.allocated = 0; |
| 999 | xfs_btree_del_cursor(cur, |
| 1000 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 1001 | } |
| 1002 | return error; |
| 1003 | } |
| 1004 | |
| 1005 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1006 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 1007 | * different data fork content type needs a different callout to do the |
| 1008 | * conversion. Some are basic and only require special block initialisation |
| 1009 | * callouts for the data formating, others (directories) are so specialised they |
| 1010 | * handle everything themselves. |
| 1011 | * |
| 1012 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1013 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1014 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | */ |
| 1016 | STATIC int /* error */ |
| 1017 | xfs_bmap_add_attrfork_local( |
| 1018 | xfs_trans_t *tp, /* transaction pointer */ |
| 1019 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1020 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 1021 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | int *flags) /* inode logging flags */ |
| 1023 | { |
| 1024 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | |
| 1026 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1027 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1028 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1029 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1031 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | dargs.dp = ip; |
| 1033 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 1034 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1035 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | dargs.whichfork = XFS_DATA_FORK; |
| 1037 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1038 | return xfs_dir2_sf_to_block(&dargs); |
| 1039 | } |
| 1040 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1041 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1042 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1043 | flags, XFS_DATA_FORK, |
| 1044 | xfs_symlink_local_to_remote); |
| 1045 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1046 | /* should only be called for types that support local format data */ |
| 1047 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1048 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1052 | * Convert inode from non-attributed to attributed. |
| 1053 | * Must not be in a transaction, ip must not be locked. |
| 1054 | */ |
| 1055 | int /* error code */ |
| 1056 | xfs_bmap_add_attrfork( |
| 1057 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1058 | int size, /* space new attribute needs */ |
| 1059 | int rsvd) /* xact may use reserved blks */ |
| 1060 | { |
| 1061 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 1062 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1063 | xfs_mount_t *mp; /* mount structure */ |
| 1064 | xfs_trans_t *tp; /* transaction pointer */ |
| 1065 | int blks; /* space reservation */ |
| 1066 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1067 | int logflags; /* logging flags */ |
| 1068 | int error; /* error return value */ |
| 1069 | |
| 1070 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1071 | |
| 1072 | mp = ip->i_mount; |
| 1073 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1074 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1075 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1076 | |
| 1077 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1078 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1079 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1080 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1081 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1082 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1083 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1084 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1085 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1086 | if (error) |
| 1087 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1088 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1089 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1090 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1091 | /* |
| 1092 | * For inodes coming from pre-6.2 filesystems. |
| 1093 | */ |
| 1094 | ASSERT(ip->i_d.di_aformat == 0); |
| 1095 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1096 | } |
| 1097 | ASSERT(ip->i_d.di_anextents == 0); |
| 1098 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1099 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1100 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1101 | |
| 1102 | switch (ip->i_d.di_format) { |
| 1103 | case XFS_DINODE_FMT_DEV: |
| 1104 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1105 | break; |
| 1106 | case XFS_DINODE_FMT_UUID: |
| 1107 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 1108 | break; |
| 1109 | case XFS_DINODE_FMT_LOCAL: |
| 1110 | case XFS_DINODE_FMT_EXTENTS: |
| 1111 | case XFS_DINODE_FMT_BTREE: |
| 1112 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1113 | if (!ip->i_d.di_forkoff) |
| 1114 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1115 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1116 | version = 2; |
| 1117 | break; |
| 1118 | default: |
| 1119 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1120 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1121 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | ASSERT(ip->i_afp == NULL); |
| 1125 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1126 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1127 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 1128 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1129 | switch (ip->i_d.di_format) { |
| 1130 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 1131 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1132 | &logflags); |
| 1133 | break; |
| 1134 | case XFS_DINODE_FMT_EXTENTS: |
| 1135 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 1136 | &dfops, &logflags); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1137 | break; |
| 1138 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 1139 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1140 | &logflags); |
| 1141 | break; |
| 1142 | default: |
| 1143 | error = 0; |
| 1144 | break; |
| 1145 | } |
| 1146 | if (logflags) |
| 1147 | xfs_trans_log_inode(tp, ip, logflags); |
| 1148 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1149 | goto bmap_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1150 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1151 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1152 | bool log_sb = false; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1153 | |
| 1154 | spin_lock(&mp->m_sb_lock); |
| 1155 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1156 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1157 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1158 | } |
| 1159 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1160 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1161 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1162 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1163 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1164 | if (log_sb) |
| 1165 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1166 | } |
| 1167 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 1168 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1169 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1170 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1171 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1172 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1173 | return error; |
| 1174 | |
| 1175 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 1176 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1177 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1178 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1179 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1180 | return error; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * Internal and external extent tree search functions. |
| 1185 | */ |
| 1186 | |
| 1187 | /* |
| 1188 | * Read in the extents to if_extents. |
| 1189 | * All inode fields are set up by caller, we just traverse the btree |
| 1190 | * and copy the records in. If the file system cannot contain unwritten |
| 1191 | * extents, the records are checked for no "state" flags. |
| 1192 | */ |
| 1193 | int /* error */ |
| 1194 | xfs_bmap_read_extents( |
| 1195 | xfs_trans_t *tp, /* transaction pointer */ |
| 1196 | xfs_inode_t *ip, /* incore inode */ |
| 1197 | int whichfork) /* data or attr fork */ |
| 1198 | { |
| 1199 | struct xfs_btree_block *block; /* current btree block */ |
| 1200 | xfs_fsblock_t bno; /* block # of "block" */ |
| 1201 | xfs_buf_t *bp; /* buffer for "block" */ |
| 1202 | int error; /* error return value */ |
| 1203 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
| 1204 | xfs_extnum_t i, j; /* index into the extents list */ |
| 1205 | xfs_ifork_t *ifp; /* fork structure */ |
| 1206 | int level; /* btree level, for checking */ |
| 1207 | xfs_mount_t *mp; /* file system mount structure */ |
| 1208 | __be64 *pp; /* pointer to block address */ |
| 1209 | /* REFERENCED */ |
| 1210 | xfs_extnum_t room; /* number of entries there's room for */ |
| 1211 | |
| 1212 | bno = NULLFSBLOCK; |
| 1213 | mp = ip->i_mount; |
| 1214 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1215 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 1216 | XFS_EXTFMT_INODE(ip); |
| 1217 | block = ifp->if_broot; |
| 1218 | /* |
| 1219 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1220 | */ |
| 1221 | level = be16_to_cpu(block->bb_level); |
| 1222 | ASSERT(level > 0); |
| 1223 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1224 | bno = be64_to_cpu(*pp); |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 1225 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1226 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 1227 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 1228 | /* |
| 1229 | * Go down the tree until leaf level is reached, following the first |
| 1230 | * pointer (leftmost) at each level. |
| 1231 | */ |
| 1232 | while (level-- > 0) { |
| 1233 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1234 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1235 | if (error) |
| 1236 | return error; |
| 1237 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1238 | if (level == 0) |
| 1239 | break; |
| 1240 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1241 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1242 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 1243 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1244 | xfs_trans_brelse(tp, bp); |
| 1245 | } |
| 1246 | /* |
| 1247 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1248 | */ |
| 1249 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 1250 | i = 0; |
| 1251 | /* |
| 1252 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1253 | */ |
| 1254 | for (;;) { |
| 1255 | xfs_bmbt_rec_t *frp; |
| 1256 | xfs_fsblock_t nextbno; |
| 1257 | xfs_extnum_t num_recs; |
| 1258 | xfs_extnum_t start; |
| 1259 | |
| 1260 | num_recs = xfs_btree_get_numrecs(block); |
| 1261 | if (unlikely(i + num_recs > room)) { |
| 1262 | ASSERT(i + num_recs <= room); |
| 1263 | xfs_warn(ip->i_mount, |
| 1264 | "corrupt dinode %Lu, (btree extents).", |
| 1265 | (unsigned long long) ip->i_ino); |
| 1266 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 1267 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
| 1268 | goto error0; |
| 1269 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1270 | /* |
| 1271 | * Read-ahead the next leaf block, if any. |
| 1272 | */ |
| 1273 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1274 | if (nextbno != NULLFSBLOCK) |
| 1275 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1276 | &xfs_bmbt_buf_ops); |
| 1277 | /* |
| 1278 | * Copy records into the extent records. |
| 1279 | */ |
| 1280 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 1281 | start = i; |
| 1282 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 1283 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
| 1284 | trp->l0 = be64_to_cpu(frp->l0); |
| 1285 | trp->l1 = be64_to_cpu(frp->l1); |
| 1286 | } |
| 1287 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 1288 | /* |
| 1289 | * Check all attribute bmap btree records and |
| 1290 | * any "older" data bmap btree records for a |
| 1291 | * set bit in the "extent flag" position. |
| 1292 | */ |
| 1293 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 1294 | start, num_recs))) { |
| 1295 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 1296 | XFS_ERRLEVEL_LOW, |
| 1297 | ip->i_mount); |
| 1298 | goto error0; |
| 1299 | } |
| 1300 | } |
| 1301 | xfs_trans_brelse(tp, bp); |
| 1302 | bno = nextbno; |
| 1303 | /* |
| 1304 | * If we've reached the end, stop. |
| 1305 | */ |
| 1306 | if (bno == NULLFSBLOCK) |
| 1307 | break; |
| 1308 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1309 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1310 | if (error) |
| 1311 | return error; |
| 1312 | block = XFS_BUF_TO_BLOCK(bp); |
| 1313 | } |
| 1314 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
| 1315 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 1316 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
| 1317 | return 0; |
| 1318 | error0: |
| 1319 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1320 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | |
| 1324 | /* |
| 1325 | * Search the extent records for the entry containing block bno. |
| 1326 | * If bno lies in a hole, point to the next entry. If bno lies |
| 1327 | * past eof, *eofp will be set, and *prevp will contain the last |
| 1328 | * entry (null if none). Else, *lastxp will be set to the index |
| 1329 | * of the found entry; *gotp will contain the entry. |
| 1330 | */ |
| 1331 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
| 1332 | xfs_bmap_search_multi_extents( |
| 1333 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 1334 | xfs_fileoff_t bno, /* block number searched for */ |
| 1335 | int *eofp, /* out: end of file found */ |
| 1336 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 1337 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 1338 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 1339 | { |
| 1340 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 1341 | xfs_extnum_t lastx; /* last extent index */ |
| 1342 | |
| 1343 | /* |
| 1344 | * Initialize the extent entry structure to catch access to |
| 1345 | * uninitialized br_startblock field. |
| 1346 | */ |
| 1347 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 1348 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 1349 | gotp->br_state = XFS_EXT_INVALID; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1350 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1351 | prevp->br_startoff = NULLFILEOFF; |
| 1352 | |
| 1353 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 1354 | if (lastx > 0) { |
| 1355 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 1356 | } |
| 1357 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 1358 | xfs_bmbt_get_all(ep, gotp); |
| 1359 | *eofp = 0; |
| 1360 | } else { |
| 1361 | if (lastx > 0) { |
| 1362 | *gotp = *prevp; |
| 1363 | } |
| 1364 | *eofp = 1; |
| 1365 | ep = NULL; |
| 1366 | } |
| 1367 | *lastxp = lastx; |
| 1368 | return ep; |
| 1369 | } |
| 1370 | |
| 1371 | /* |
| 1372 | * Search the extents list for the inode, for the extent containing bno. |
| 1373 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 1374 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 1375 | * Else, *lastxp will be set to the index of the found |
| 1376 | * entry; *gotp will contain the entry. |
| 1377 | */ |
| 1378 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
| 1379 | xfs_bmap_search_extents( |
| 1380 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1381 | xfs_fileoff_t bno, /* block number searched for */ |
| 1382 | int fork, /* data or attr fork */ |
| 1383 | int *eofp, /* out: end of file found */ |
| 1384 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 1385 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 1386 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 1387 | { |
| 1388 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1389 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 1390 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1391 | XFS_STATS_INC(ip->i_mount, xs_look_exlist); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1392 | ifp = XFS_IFORK_PTR(ip, fork); |
| 1393 | |
| 1394 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 1395 | |
| 1396 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 1397 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
| 1398 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
| 1399 | "Access to block zero in inode %llu " |
| 1400 | "start_block: %llx start_off: %llx " |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 1401 | "blkcnt: %llx extent-state: %x lastx: %x", |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1402 | (unsigned long long)ip->i_ino, |
| 1403 | (unsigned long long)gotp->br_startblock, |
| 1404 | (unsigned long long)gotp->br_startoff, |
| 1405 | (unsigned long long)gotp->br_blockcount, |
| 1406 | gotp->br_state, *lastxp); |
| 1407 | *lastxp = NULLEXTNUM; |
| 1408 | *eofp = 1; |
| 1409 | return NULL; |
| 1410 | } |
| 1411 | return ep; |
| 1412 | } |
| 1413 | |
| 1414 | /* |
| 1415 | * Returns the file-relative block number of the first unused block(s) |
| 1416 | * in the file with at least "len" logically contiguous blocks free. |
| 1417 | * This is the lowest-address hole if the file has holes, else the first block |
| 1418 | * past the end of file. |
| 1419 | * Return 0 if the file is currently local (in-inode). |
| 1420 | */ |
| 1421 | int /* error */ |
| 1422 | xfs_bmap_first_unused( |
| 1423 | xfs_trans_t *tp, /* transaction pointer */ |
| 1424 | xfs_inode_t *ip, /* incore inode */ |
| 1425 | xfs_extlen_t len, /* size of hole to find */ |
| 1426 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1427 | int whichfork) /* data or attr fork */ |
| 1428 | { |
| 1429 | int error; /* error return value */ |
| 1430 | int idx; /* extent record index */ |
| 1431 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1432 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 1433 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 1434 | xfs_fileoff_t max; /* starting useful block */ |
| 1435 | xfs_fileoff_t off; /* offset for this block */ |
| 1436 | xfs_extnum_t nextents; /* number of extent entries */ |
| 1437 | |
| 1438 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1439 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1440 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 1441 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1442 | *first_unused = 0; |
| 1443 | return 0; |
| 1444 | } |
| 1445 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1446 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1447 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1448 | return error; |
| 1449 | lowest = *first_unused; |
| 1450 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 1451 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
| 1452 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
| 1453 | off = xfs_bmbt_get_startoff(ep); |
| 1454 | /* |
| 1455 | * See if the hole before this extent will work. |
| 1456 | */ |
| 1457 | if (off >= lowest + len && off - max >= len) { |
| 1458 | *first_unused = max; |
| 1459 | return 0; |
| 1460 | } |
| 1461 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 1462 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1463 | } |
| 1464 | *first_unused = max; |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1469 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1470 | * last_block (input value) in the file. |
| 1471 | * This is not based on i_size, it is based on the extent records. |
| 1472 | * Returns 0 for local files, as they do not have extent records. |
| 1473 | */ |
| 1474 | int /* error */ |
| 1475 | xfs_bmap_last_before( |
| 1476 | xfs_trans_t *tp, /* transaction pointer */ |
| 1477 | xfs_inode_t *ip, /* incore inode */ |
| 1478 | xfs_fileoff_t *last_block, /* last block */ |
| 1479 | int whichfork) /* data or attr fork */ |
| 1480 | { |
| 1481 | xfs_fileoff_t bno; /* input file offset */ |
| 1482 | int eof; /* hit end of file */ |
| 1483 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
| 1484 | int error; /* error return value */ |
| 1485 | xfs_bmbt_irec_t got; /* current extent value */ |
| 1486 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1487 | xfs_extnum_t lastx; /* last extent used */ |
| 1488 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 1489 | |
| 1490 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1491 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 1492 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1493 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1494 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1495 | *last_block = 0; |
| 1496 | return 0; |
| 1497 | } |
| 1498 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1499 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1500 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1501 | return error; |
| 1502 | bno = *last_block - 1; |
| 1503 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 1504 | &prev); |
| 1505 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 1506 | if (prev.br_startoff == NULLFILEOFF) |
| 1507 | *last_block = 0; |
| 1508 | else |
| 1509 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 1510 | } |
| 1511 | /* |
| 1512 | * Otherwise *last_block is already the right answer. |
| 1513 | */ |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1517 | int |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1518 | xfs_bmap_last_extent( |
| 1519 | struct xfs_trans *tp, |
| 1520 | struct xfs_inode *ip, |
| 1521 | int whichfork, |
| 1522 | struct xfs_bmbt_irec *rec, |
| 1523 | int *is_empty) |
| 1524 | { |
| 1525 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1526 | int error; |
| 1527 | int nextents; |
| 1528 | |
| 1529 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1530 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1531 | if (error) |
| 1532 | return error; |
| 1533 | } |
| 1534 | |
| 1535 | nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
| 1536 | if (nextents == 0) { |
| 1537 | *is_empty = 1; |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 1542 | *is_empty = 0; |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | * Check the last inode extent to determine whether this allocation will result |
| 1548 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1549 | * blocks at the end of the file which do not start at the previous data block, |
| 1550 | * we will try to align the new blocks at stripe unit boundaries. |
| 1551 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1552 | * 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] | 1553 | * at, or past the EOF. |
| 1554 | */ |
| 1555 | STATIC int |
| 1556 | xfs_bmap_isaeof( |
| 1557 | struct xfs_bmalloca *bma, |
| 1558 | int whichfork) |
| 1559 | { |
| 1560 | struct xfs_bmbt_irec rec; |
| 1561 | int is_empty; |
| 1562 | int error; |
| 1563 | |
| 1564 | bma->aeof = 0; |
| 1565 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1566 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1567 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1568 | return error; |
| 1569 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1570 | if (is_empty) { |
| 1571 | bma->aeof = 1; |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1575 | /* |
| 1576 | * Check if we are allocation or past the last extent, or at least into |
| 1577 | * the last delayed allocated extent. |
| 1578 | */ |
| 1579 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1580 | (bma->offset >= rec.br_startoff && |
| 1581 | isnullstartblock(rec.br_startblock)); |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1586 | * Returns the file-relative block number of the first block past eof in |
| 1587 | * the file. This is not based on i_size, it is based on the extent records. |
| 1588 | * Returns 0 for local files, as they do not have extent records. |
| 1589 | */ |
| 1590 | int |
| 1591 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1592 | struct xfs_inode *ip, |
| 1593 | xfs_fileoff_t *last_block, |
| 1594 | int whichfork) |
| 1595 | { |
| 1596 | struct xfs_bmbt_irec rec; |
| 1597 | int is_empty; |
| 1598 | int error; |
| 1599 | |
| 1600 | *last_block = 0; |
| 1601 | |
| 1602 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1603 | return 0; |
| 1604 | |
| 1605 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1606 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1607 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1608 | |
| 1609 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1610 | if (error || is_empty) |
| 1611 | return error; |
| 1612 | |
| 1613 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | /* |
| 1618 | * Returns whether the selected fork of the inode has exactly one |
| 1619 | * block or not. For the data fork we check this matches di_size, |
| 1620 | * implying the file's range is 0..bsize-1. |
| 1621 | */ |
| 1622 | int /* 1=>1 block, 0=>otherwise */ |
| 1623 | xfs_bmap_one_block( |
| 1624 | xfs_inode_t *ip, /* incore inode */ |
| 1625 | int whichfork) /* data or attr fork */ |
| 1626 | { |
| 1627 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
| 1628 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1629 | int rval; /* return value */ |
| 1630 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 1631 | |
| 1632 | #ifndef DEBUG |
| 1633 | if (whichfork == XFS_DATA_FORK) |
| 1634 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1635 | #endif /* !DEBUG */ |
| 1636 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1637 | return 0; |
| 1638 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1639 | return 0; |
| 1640 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1641 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 1642 | ep = xfs_iext_get_ext(ifp, 0); |
| 1643 | xfs_bmbt_get_all(ep, &s); |
| 1644 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1645 | if (rval && whichfork == XFS_DATA_FORK) |
| 1646 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1647 | return rval; |
| 1648 | } |
| 1649 | |
| 1650 | /* |
| 1651 | * Extent tree manipulation functions used during allocation. |
| 1652 | */ |
| 1653 | |
| 1654 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1655 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | */ |
| 1657 | STATIC int /* error */ |
| 1658 | xfs_bmap_add_extent_delay_real( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1659 | struct xfs_bmalloca *bma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1661 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1663 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1666 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1668 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1669 | /* left is 0, right is 1, prev is 2 */ |
| 1670 | int rval=0; /* return value (logging flags) */ |
| 1671 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1672 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1673 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1674 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 1675 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | int tmp_rval; /* partial logging flags */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1677 | int whichfork = XFS_DATA_FORK; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1678 | struct xfs_mount *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1680 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1681 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1682 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1683 | ASSERT(bma->idx >= 0); |
| 1684 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1685 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1686 | ASSERT(!bma->cur || |
| 1687 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1688 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1689 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | #define LEFT r[0] |
| 1692 | #define RIGHT r[1] |
| 1693 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | |
| 1695 | /* |
| 1696 | * Set up a bunch of variables to make the tests simpler. |
| 1697 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1698 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | xfs_bmbt_get_all(ep, &PREV); |
| 1700 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1701 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1702 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1703 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1704 | da_old = startblockval(PREV.br_startblock); |
| 1705 | da_new = 0; |
| 1706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | /* |
| 1708 | * Set flags determining what part of the previous delayed allocation |
| 1709 | * extent is being replaced by a real allocation. |
| 1710 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1711 | if (PREV.br_startoff == new->br_startoff) |
| 1712 | state |= BMAP_LEFT_FILLING; |
| 1713 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1714 | state |= BMAP_RIGHT_FILLING; |
| 1715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | /* |
| 1717 | * Check and set flags if this segment has a left neighbor. |
| 1718 | * Don't set contiguous if the combined extent would be too large. |
| 1719 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1720 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1721 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1722 | 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] | 1723 | |
| 1724 | if (isnullstartblock(LEFT.br_startblock)) |
| 1725 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1727 | |
| 1728 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1729 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1730 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1731 | LEFT.br_state == new->br_state && |
| 1732 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1733 | state |= BMAP_LEFT_CONTIG; |
| 1734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | /* |
| 1736 | * Check and set flags if this segment has a right neighbor. |
| 1737 | * Don't set contiguous if the combined extent would be too large. |
| 1738 | * Also check for all-three-contiguous being too large. |
| 1739 | */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1740 | 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] | 1741 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1742 | 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] | 1743 | |
| 1744 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1745 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1747 | |
| 1748 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1749 | new_endoff == RIGHT.br_startoff && |
| 1750 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1751 | new->br_state == RIGHT.br_state && |
| 1752 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1753 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1754 | BMAP_RIGHT_FILLING)) != |
| 1755 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1756 | BMAP_RIGHT_FILLING) || |
| 1757 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1758 | <= MAXEXTLEN)) |
| 1759 | state |= BMAP_RIGHT_CONTIG; |
| 1760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | error = 0; |
| 1762 | /* |
| 1763 | * Switch out based on the FILLING and CONTIG state bits. |
| 1764 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1765 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1766 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1767 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1768 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | /* |
| 1770 | * Filling in all of a previously delayed allocation extent. |
| 1771 | * The left and right neighbors are both contiguous with new. |
| 1772 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1773 | bma->idx--; |
| 1774 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1775 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1777 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1778 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1779 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1780 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
| 1781 | bma->ip->i_d.di_nextents--; |
| 1782 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1784 | else { |
| 1785 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1786 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1788 | RIGHT.br_blockcount, &i); |
| 1789 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1791 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1792 | error = xfs_btree_delete(bma->cur, &i); |
| 1793 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1795 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1796 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1797 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1799 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1800 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | LEFT.br_startblock, |
| 1802 | LEFT.br_blockcount + |
| 1803 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1804 | RIGHT.br_blockcount, LEFT.br_state); |
| 1805 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | goto done; |
| 1807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | break; |
| 1809 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1810 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | /* |
| 1812 | * Filling in all of a previously delayed allocation extent. |
| 1813 | * The left neighbor is contiguous, the right is not. |
| 1814 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1815 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1816 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1817 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1818 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1819 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1820 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1821 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1822 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1823 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | rval = XFS_ILOG_DEXT; |
| 1825 | else { |
| 1826 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1827 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1829 | &i); |
| 1830 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1832 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1833 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | LEFT.br_startblock, |
| 1835 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1836 | PREV.br_blockcount, LEFT.br_state); |
| 1837 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | goto done; |
| 1839 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | break; |
| 1841 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1842 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | /* |
| 1844 | * Filling in all of a previously delayed allocation extent. |
| 1845 | * The right neighbor is contiguous, the left is not. |
| 1846 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1847 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 1849 | xfs_bmbt_set_blockcount(ep, |
| 1850 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1851 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1852 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1853 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1854 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | rval = XFS_ILOG_DEXT; |
| 1856 | else { |
| 1857 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1858 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1860 | RIGHT.br_blockcount, &i); |
| 1861 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1863 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1864 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | new->br_startblock, |
| 1866 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1867 | RIGHT.br_blockcount, PREV.br_state); |
| 1868 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | goto done; |
| 1870 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | break; |
| 1872 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1873 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | /* |
| 1875 | * Filling in all of a previously delayed allocation extent. |
| 1876 | * Neither the left nor right neighbors are contiguous with |
| 1877 | * the new one. |
| 1878 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1879 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1881 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1882 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1883 | bma->ip->i_d.di_nextents++; |
| 1884 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1886 | else { |
| 1887 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1888 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1890 | &i); |
| 1891 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1893 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1894 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1895 | error = xfs_btree_insert(bma->cur, &i); |
| 1896 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1898 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | break; |
| 1901 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1902 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | /* |
| 1904 | * Filling in the first part of a previous delayed allocation. |
| 1905 | * The left neighbor is contiguous. |
| 1906 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1907 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 1908 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | LEFT.br_blockcount + new->br_blockcount); |
| 1910 | xfs_bmbt_set_startoff(ep, |
| 1911 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1912 | 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] | 1913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1915 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1917 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | rval = XFS_ILOG_DEXT; |
| 1919 | else { |
| 1920 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1921 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1923 | &i); |
| 1924 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1926 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1927 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | LEFT.br_startblock, |
| 1929 | LEFT.br_blockcount + |
| 1930 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1931 | LEFT.br_state); |
| 1932 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | goto done; |
| 1934 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1935 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1936 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1937 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1938 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1939 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1940 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | break; |
| 1942 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1943 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | /* |
| 1945 | * Filling in the first part of a previous delayed allocation. |
| 1946 | * The left neighbor is not contiguous. |
| 1947 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1948 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1950 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1951 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1952 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 1953 | bma->ip->i_d.di_nextents++; |
| 1954 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1956 | else { |
| 1957 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1958 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1960 | &i); |
| 1961 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1963 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1964 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1965 | error = xfs_btree_insert(bma->cur, &i); |
| 1966 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1968 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1970 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1971 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1972 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 1973 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1974 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | rval |= tmp_rval; |
| 1976 | if (error) |
| 1977 | goto done; |
| 1978 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1979 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1980 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1981 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 1982 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1983 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1984 | 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] | 1985 | break; |
| 1986 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1987 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | /* |
| 1989 | * Filling in the last part of a previous delayed allocation. |
| 1990 | * The right neighbor is contiguous with the new allocation. |
| 1991 | */ |
| 1992 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1993 | 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] | 1994 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1995 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1996 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | new->br_blockcount + RIGHT.br_blockcount, |
| 1998 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1999 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 2000 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | rval = XFS_ILOG_DEXT; |
| 2002 | else { |
| 2003 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2004 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2006 | RIGHT.br_blockcount, &i); |
| 2007 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2009 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2010 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | new->br_startblock, |
| 2012 | new->br_blockcount + |
| 2013 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2014 | RIGHT.br_state); |
| 2015 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | goto done; |
| 2017 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2018 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2019 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2020 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2021 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2022 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2023 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2024 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2025 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | break; |
| 2027 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2028 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | /* |
| 2030 | * Filling in the last part of a previous delayed allocation. |
| 2031 | * The right neighbor is not contiguous. |
| 2032 | */ |
| 2033 | temp = PREV.br_blockcount - new->br_blockcount; |
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_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2036 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
| 2037 | bma->ip->i_d.di_nextents++; |
| 2038 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2040 | else { |
| 2041 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2042 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2044 | &i); |
| 2045 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2047 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2048 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2049 | error = xfs_btree_insert(bma->cur, &i); |
| 2050 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2052 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2054 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2055 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2056 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 2057 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2058 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | rval |= tmp_rval; |
| 2060 | if (error) |
| 2061 | goto done; |
| 2062 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2063 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2064 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2065 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 2066 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2067 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2068 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2069 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2070 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | break; |
| 2072 | |
| 2073 | case 0: |
| 2074 | /* |
| 2075 | * Filling in the middle part of a previous delayed allocation. |
| 2076 | * Contiguity is impossible here. |
| 2077 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2078 | * |
| 2079 | * We start with a delayed allocation: |
| 2080 | * |
| 2081 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 2082 | * PREV @ idx |
| 2083 | * |
| 2084 | * and we are allocating: |
| 2085 | * +rrrrrrrrrrrrrrrrr+ |
| 2086 | * new |
| 2087 | * |
| 2088 | * and we set it up for insertion as: |
| 2089 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 2090 | * new |
| 2091 | * PREV @ idx LEFT RIGHT |
| 2092 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | */ |
| 2094 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2096 | 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] | 2097 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 2098 | LEFT = *new; |
| 2099 | RIGHT.br_state = PREV.br_state; |
| 2100 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2101 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2102 | RIGHT.br_startoff = new_endoff; |
| 2103 | RIGHT.br_blockcount = temp2; |
| 2104 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2105 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
| 2106 | bma->ip->i_d.di_nextents++; |
| 2107 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2109 | else { |
| 2110 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2111 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2113 | &i); |
| 2114 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2116 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2117 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2118 | error = xfs_btree_insert(bma->cur, &i); |
| 2119 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2121 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2123 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2124 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2125 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 2126 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2127 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | rval |= tmp_rval; |
| 2129 | if (error) |
| 2130 | goto done; |
| 2131 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2132 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 2133 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2134 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2135 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2136 | if (diff > 0) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2137 | error = xfs_mod_fdblocks(bma->ip->i_mount, |
| 2138 | -((int64_t)diff), false); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2139 | ASSERT(!error); |
| 2140 | if (error) |
| 2141 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2143 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2144 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2145 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2146 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2147 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 2148 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2149 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2150 | 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] | 2151 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2152 | bma->idx++; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2153 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | break; |
| 2155 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2156 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2157 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2158 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2159 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2160 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2161 | case BMAP_LEFT_CONTIG: |
| 2162 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | /* |
| 2164 | * These cases are all impossible. |
| 2165 | */ |
| 2166 | ASSERT(0); |
| 2167 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2168 | |
| 2169 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2170 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2171 | int tmp_logflags; /* partial log flag return val */ |
| 2172 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2173 | ASSERT(bma->cur == NULL); |
| 2174 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 2175 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2176 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2177 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2178 | if (error) |
| 2179 | goto done; |
| 2180 | } |
| 2181 | |
| 2182 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2183 | if (da_old || da_new) { |
| 2184 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2185 | if (bma->cur) |
| 2186 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2187 | ASSERT(temp <= da_old); |
| 2188 | if (temp < da_old) |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2189 | xfs_mod_fdblocks(bma->ip->i_mount, |
| 2190 | (int64_t)(da_old - temp), false); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2194 | if (bma->cur) |
| 2195 | bma->cur->bc_private.b.allocated = 0; |
| 2196 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2197 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | done: |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2199 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | return error; |
| 2201 | #undef LEFT |
| 2202 | #undef RIGHT |
| 2203 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2207 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | */ |
| 2209 | STATIC int /* error */ |
| 2210 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2211 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2213 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2215 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2216 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 2217 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2218 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2221 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2224 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 2226 | xfs_exntst_t newext; /* new extent state */ |
| 2227 | xfs_exntst_t oldext; /* old extent state */ |
| 2228 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2229 | /* left is 0, right is 1, prev is 2 */ |
| 2230 | int rval=0; /* return value (logging flags) */ |
| 2231 | int state = 0;/* state bits, accessed thru macros */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2232 | struct xfs_mount *mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2234 | *logflagsp = 0; |
| 2235 | |
| 2236 | cur = *curp; |
| 2237 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 2238 | |
| 2239 | ASSERT(*idx >= 0); |
| 2240 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
| 2241 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2242 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2243 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | #define LEFT r[0] |
| 2246 | #define RIGHT r[1] |
| 2247 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | /* |
| 2250 | * Set up a bunch of variables to make the tests simpler. |
| 2251 | */ |
| 2252 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2253 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | xfs_bmbt_get_all(ep, &PREV); |
| 2255 | newext = new->br_state; |
| 2256 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 2257 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 2258 | ASSERT(PREV.br_state == oldext); |
| 2259 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2260 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2261 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | /* |
| 2264 | * Set flags determining what part of the previous oldext allocation |
| 2265 | * extent is being replaced by a newext allocation. |
| 2266 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2267 | if (PREV.br_startoff == new->br_startoff) |
| 2268 | state |= BMAP_LEFT_FILLING; |
| 2269 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2270 | state |= BMAP_RIGHT_FILLING; |
| 2271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | /* |
| 2273 | * Check and set flags if this segment has a left neighbor. |
| 2274 | * Don't set contiguous if the combined extent would be too large. |
| 2275 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2276 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2277 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2278 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2279 | |
| 2280 | if (isnullstartblock(LEFT.br_startblock)) |
| 2281 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2283 | |
| 2284 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2285 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2286 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 2287 | LEFT.br_state == newext && |
| 2288 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2289 | state |= BMAP_LEFT_CONTIG; |
| 2290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | /* |
| 2292 | * Check and set flags if this segment has a right neighbor. |
| 2293 | * Don't set contiguous if the combined extent would be too large. |
| 2294 | * Also check for all-three-contiguous being too large. |
| 2295 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2296 | 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] | 2297 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2298 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2299 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2300 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2302 | |
| 2303 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2304 | new_endoff == RIGHT.br_startoff && |
| 2305 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 2306 | newext == RIGHT.br_state && |
| 2307 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2308 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2309 | BMAP_RIGHT_FILLING)) != |
| 2310 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2311 | BMAP_RIGHT_FILLING) || |
| 2312 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2313 | <= MAXEXTLEN)) |
| 2314 | state |= BMAP_RIGHT_CONTIG; |
| 2315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | /* |
| 2317 | * Switch out based on the FILLING and CONTIG state bits. |
| 2318 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2319 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2320 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2321 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2322 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | /* |
| 2324 | * Setting all of a previous oldext extent to newext. |
| 2325 | * The left and right neighbors are both contiguous with new. |
| 2326 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2327 | --*idx; |
| 2328 | |
| 2329 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2330 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2332 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2333 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2334 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2335 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | ip->i_d.di_nextents -= 2; |
| 2337 | if (cur == NULL) |
| 2338 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2339 | else { |
| 2340 | rval = XFS_ILOG_CORE; |
| 2341 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2342 | RIGHT.br_startblock, |
| 2343 | RIGHT.br_blockcount, &i))) |
| 2344 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2345 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2346 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2348 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2349 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2351 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2352 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2354 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2355 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2357 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2359 | LEFT.br_startblock, |
| 2360 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2361 | RIGHT.br_blockcount, LEFT.br_state))) |
| 2362 | goto done; |
| 2363 | } |
| 2364 | break; |
| 2365 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2366 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | /* |
| 2368 | * Setting all of a previous oldext extent to newext. |
| 2369 | * The left neighbor is contiguous, the right is not. |
| 2370 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2371 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2372 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2373 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2374 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 2375 | LEFT.br_blockcount + PREV.br_blockcount); |
| 2376 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2377 | |
| 2378 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | ip->i_d.di_nextents--; |
| 2380 | if (cur == NULL) |
| 2381 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2382 | else { |
| 2383 | rval = XFS_ILOG_CORE; |
| 2384 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2385 | PREV.br_startblock, PREV.br_blockcount, |
| 2386 | &i))) |
| 2387 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2388 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2389 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2391 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2392 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2394 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2396 | LEFT.br_startblock, |
| 2397 | LEFT.br_blockcount + PREV.br_blockcount, |
| 2398 | LEFT.br_state))) |
| 2399 | goto done; |
| 2400 | } |
| 2401 | break; |
| 2402 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2403 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | /* |
| 2405 | * Setting all of a previous oldext extent to newext. |
| 2406 | * The right neighbor is contiguous, the left is not. |
| 2407 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2408 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | xfs_bmbt_set_blockcount(ep, |
| 2410 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 2411 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2412 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2413 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | ip->i_d.di_nextents--; |
| 2415 | if (cur == NULL) |
| 2416 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2417 | else { |
| 2418 | rval = XFS_ILOG_CORE; |
| 2419 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2420 | RIGHT.br_startblock, |
| 2421 | RIGHT.br_blockcount, &i))) |
| 2422 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2423 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2424 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2426 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2427 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2429 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2431 | new->br_startblock, |
| 2432 | new->br_blockcount + RIGHT.br_blockcount, |
| 2433 | newext))) |
| 2434 | goto done; |
| 2435 | } |
| 2436 | break; |
| 2437 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2438 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | /* |
| 2440 | * Setting all of a previous oldext extent to newext. |
| 2441 | * Neither the left nor right neighbors are contiguous with |
| 2442 | * the new one. |
| 2443 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2444 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2446 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | if (cur == NULL) |
| 2449 | rval = XFS_ILOG_DEXT; |
| 2450 | else { |
| 2451 | rval = 0; |
| 2452 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2453 | new->br_startblock, new->br_blockcount, |
| 2454 | &i))) |
| 2455 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2456 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2458 | new->br_startblock, new->br_blockcount, |
| 2459 | newext))) |
| 2460 | goto done; |
| 2461 | } |
| 2462 | break; |
| 2463 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2464 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | /* |
| 2466 | * Setting the first part of a previous oldext extent to newext. |
| 2467 | * The left neighbor is contiguous. |
| 2468 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2469 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 2470 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | LEFT.br_blockcount + new->br_blockcount); |
| 2472 | xfs_bmbt_set_startoff(ep, |
| 2473 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2474 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2475 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2476 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | xfs_bmbt_set_startblock(ep, |
| 2478 | new->br_startblock + new->br_blockcount); |
| 2479 | xfs_bmbt_set_blockcount(ep, |
| 2480 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2481 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2482 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2483 | --*idx; |
| 2484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | if (cur == NULL) |
| 2486 | rval = XFS_ILOG_DEXT; |
| 2487 | else { |
| 2488 | rval = 0; |
| 2489 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2490 | PREV.br_startblock, PREV.br_blockcount, |
| 2491 | &i))) |
| 2492 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2493 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | if ((error = xfs_bmbt_update(cur, |
| 2495 | PREV.br_startoff + new->br_blockcount, |
| 2496 | PREV.br_startblock + new->br_blockcount, |
| 2497 | PREV.br_blockcount - new->br_blockcount, |
| 2498 | oldext))) |
| 2499 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2500 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2502 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | LEFT.br_startblock, |
| 2504 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2505 | LEFT.br_state); |
| 2506 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | goto done; |
| 2508 | } |
| 2509 | break; |
| 2510 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2511 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | /* |
| 2513 | * Setting the first part of a previous oldext extent to newext. |
| 2514 | * The left neighbor is not contiguous. |
| 2515 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2516 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 2518 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 2519 | xfs_bmbt_set_blockcount(ep, |
| 2520 | PREV.br_blockcount - new->br_blockcount); |
| 2521 | xfs_bmbt_set_startblock(ep, |
| 2522 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2523 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2524 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2525 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | ip->i_d.di_nextents++; |
| 2527 | if (cur == NULL) |
| 2528 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2529 | else { |
| 2530 | rval = XFS_ILOG_CORE; |
| 2531 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2532 | PREV.br_startblock, PREV.br_blockcount, |
| 2533 | &i))) |
| 2534 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2535 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | if ((error = xfs_bmbt_update(cur, |
| 2537 | PREV.br_startoff + new->br_blockcount, |
| 2538 | PREV.br_startblock + new->br_blockcount, |
| 2539 | PREV.br_blockcount - new->br_blockcount, |
| 2540 | oldext))) |
| 2541 | goto done; |
| 2542 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2543 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2545 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | } |
| 2547 | break; |
| 2548 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2549 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | /* |
| 2551 | * Setting the last part of a previous oldext extent to newext. |
| 2552 | * The right neighbor is contiguous with the new allocation. |
| 2553 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2554 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | xfs_bmbt_set_blockcount(ep, |
| 2556 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2557 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2558 | |
| 2559 | ++*idx; |
| 2560 | |
| 2561 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2562 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2563 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2565 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | if (cur == NULL) |
| 2568 | rval = XFS_ILOG_DEXT; |
| 2569 | else { |
| 2570 | rval = 0; |
| 2571 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2572 | PREV.br_startblock, |
| 2573 | PREV.br_blockcount, &i))) |
| 2574 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2575 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2577 | PREV.br_startblock, |
| 2578 | PREV.br_blockcount - new->br_blockcount, |
| 2579 | oldext))) |
| 2580 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2581 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | goto done; |
| 2583 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2584 | new->br_startblock, |
| 2585 | new->br_blockcount + RIGHT.br_blockcount, |
| 2586 | newext))) |
| 2587 | goto done; |
| 2588 | } |
| 2589 | break; |
| 2590 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2591 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | /* |
| 2593 | * Setting the last part of a previous oldext extent to newext. |
| 2594 | * The right neighbor is not contiguous. |
| 2595 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2596 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | xfs_bmbt_set_blockcount(ep, |
| 2598 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2599 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2600 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2601 | ++*idx; |
| 2602 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | ip->i_d.di_nextents++; |
| 2605 | if (cur == NULL) |
| 2606 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2607 | else { |
| 2608 | rval = XFS_ILOG_CORE; |
| 2609 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2610 | PREV.br_startblock, PREV.br_blockcount, |
| 2611 | &i))) |
| 2612 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2613 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2615 | PREV.br_startblock, |
| 2616 | PREV.br_blockcount - new->br_blockcount, |
| 2617 | oldext))) |
| 2618 | goto done; |
| 2619 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2620 | new->br_startblock, new->br_blockcount, |
| 2621 | &i))) |
| 2622 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2623 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2625 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2627 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | } |
| 2629 | break; |
| 2630 | |
| 2631 | case 0: |
| 2632 | /* |
| 2633 | * Setting the middle part of a previous oldext extent to |
| 2634 | * newext. Contiguity is impossible here. |
| 2635 | * One extent becomes three extents. |
| 2636 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2637 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | xfs_bmbt_set_blockcount(ep, |
| 2639 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2640 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | r[0] = *new; |
| 2643 | r[1].br_startoff = new_endoff; |
| 2644 | r[1].br_blockcount = |
| 2645 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 2646 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 2647 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2648 | |
| 2649 | ++*idx; |
| 2650 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 2651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | ip->i_d.di_nextents += 2; |
| 2653 | if (cur == NULL) |
| 2654 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2655 | else { |
| 2656 | rval = XFS_ILOG_CORE; |
| 2657 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2658 | PREV.br_startblock, PREV.br_blockcount, |
| 2659 | &i))) |
| 2660 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2661 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | /* new right extent - oldext */ |
| 2663 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 2664 | r[1].br_startblock, r[1].br_blockcount, |
| 2665 | r[1].br_state))) |
| 2666 | goto done; |
| 2667 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 2669 | cur->bc_rec.b.br_blockcount = |
| 2670 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2671 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2673 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2674 | /* |
| 2675 | * Reset the cursor to the position of the new extent |
| 2676 | * we are about to insert as we can't trust it after |
| 2677 | * the previous insert. |
| 2678 | */ |
| 2679 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2680 | new->br_startblock, new->br_blockcount, |
| 2681 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2683 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2685 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2686 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2688 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | } |
| 2690 | break; |
| 2691 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2692 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2693 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2694 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2695 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2696 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2697 | case BMAP_LEFT_CONTIG: |
| 2698 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | /* |
| 2700 | * These cases are all impossible. |
| 2701 | */ |
| 2702 | ASSERT(0); |
| 2703 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2704 | |
| 2705 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2706 | if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2707 | int tmp_logflags; /* partial log flag return val */ |
| 2708 | |
| 2709 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 2710 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2711 | 0, &tmp_logflags, XFS_DATA_FORK); |
| 2712 | *logflagsp |= tmp_logflags; |
| 2713 | if (error) |
| 2714 | goto done; |
| 2715 | } |
| 2716 | |
| 2717 | /* clear out the allocated field, done with it now in any case. */ |
| 2718 | if (cur) { |
| 2719 | cur->bc_private.b.allocated = 0; |
| 2720 | *curp = cur; |
| 2721 | } |
| 2722 | |
| 2723 | xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2725 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | return error; |
| 2727 | #undef LEFT |
| 2728 | #undef RIGHT |
| 2729 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2733 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2735 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | xfs_bmap_add_extent_hole_delay( |
| 2737 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2738 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2739 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2741 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2743 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2744 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2745 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 2746 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2747 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2749 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2751 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | /* |
| 2754 | * Check and set flags if this segment has a left neighbor |
| 2755 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2756 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2757 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2758 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2759 | |
| 2760 | if (isnullstartblock(left.br_startblock)) |
| 2761 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | /* |
| 2765 | * Check and set flags if the current (right) segment exists. |
| 2766 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2767 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2768 | 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] | 2769 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2770 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2771 | |
| 2772 | if (isnullstartblock(right.br_startblock)) |
| 2773 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | /* |
| 2777 | * Set contiguity flags on the left and right neighbors. |
| 2778 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2779 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2780 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2781 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2782 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2783 | state |= BMAP_LEFT_CONTIG; |
| 2784 | |
| 2785 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2786 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2787 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2788 | (!(state & BMAP_LEFT_CONTIG) || |
| 2789 | (left.br_blockcount + new->br_blockcount + |
| 2790 | right.br_blockcount <= MAXEXTLEN))) |
| 2791 | state |= BMAP_RIGHT_CONTIG; |
| 2792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | /* |
| 2794 | * Switch out based on the contiguity flags. |
| 2795 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2796 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2797 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | /* |
| 2799 | * New allocation is contiguous with delayed allocations |
| 2800 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2801 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2803 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | temp = left.br_blockcount + new->br_blockcount + |
| 2805 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2806 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2807 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2808 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2809 | oldlen = startblockval(left.br_startblock) + |
| 2810 | startblockval(new->br_startblock) + |
| 2811 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2813 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2814 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2815 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2816 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2817 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | break; |
| 2819 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2820 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | /* |
| 2822 | * New allocation is contiguous with a delayed allocation |
| 2823 | * on the left. |
| 2824 | * Merge the new allocation with the left neighbor. |
| 2825 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2826 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2828 | |
| 2829 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2830 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2831 | oldlen = startblockval(left.br_startblock) + |
| 2832 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2834 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2835 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2836 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | break; |
| 2838 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2839 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | /* |
| 2841 | * New allocation is contiguous with a delayed allocation |
| 2842 | * on the right. |
| 2843 | * Merge the new allocation with the right neighbor. |
| 2844 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2845 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2847 | oldlen = startblockval(new->br_startblock) + |
| 2848 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2850 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 2851 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2852 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2853 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | break; |
| 2855 | |
| 2856 | case 0: |
| 2857 | /* |
| 2858 | * New allocation is not contiguous with another |
| 2859 | * delayed allocation. |
| 2860 | * Insert a new entry. |
| 2861 | */ |
| 2862 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2863 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | break; |
| 2865 | } |
| 2866 | if (oldlen != newlen) { |
| 2867 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2868 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2869 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | /* |
| 2871 | * Nothing to do for disk quota accounting here. |
| 2872 | */ |
| 2873 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2877 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | */ |
| 2879 | STATIC int /* error */ |
| 2880 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2881 | struct xfs_bmalloca *bma, |
| 2882 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | { |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2884 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | int i; /* temp state */ |
| 2887 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2888 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2889 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2890 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | int state; /* state bits, accessed thru macros */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2892 | struct xfs_mount *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 2894 | mp = bma->ip->i_mount; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2895 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2896 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2897 | ASSERT(bma->idx >= 0); |
| 2898 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2899 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2900 | ASSERT(!bma->cur || |
| 2901 | !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2902 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2903 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2904 | |
| 2905 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2906 | if (whichfork == XFS_ATTR_FORK) |
| 2907 | state |= BMAP_ATTRFORK; |
| 2908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | /* |
| 2910 | * Check and set flags if this segment has a left neighbor. |
| 2911 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2912 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2913 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2914 | 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] | 2915 | if (isnullstartblock(left.br_startblock)) |
| 2916 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | /* |
| 2920 | * Check and set flags if this segment has a current value. |
| 2921 | * Not true if we're inserting into the "hole" at eof. |
| 2922 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2923 | if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2924 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2925 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2926 | if (isnullstartblock(right.br_startblock)) |
| 2927 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | /* |
| 2931 | * We're inserting a real allocation between "left" and "right". |
| 2932 | * Set the contiguity flags. Don't let extents get too large. |
| 2933 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2934 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2935 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2936 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2937 | left.br_state == new->br_state && |
| 2938 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2939 | state |= BMAP_LEFT_CONTIG; |
| 2940 | |
| 2941 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2942 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2943 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2944 | new->br_state == right.br_state && |
| 2945 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2946 | (!(state & BMAP_LEFT_CONTIG) || |
| 2947 | left.br_blockcount + new->br_blockcount + |
| 2948 | right.br_blockcount <= MAXEXTLEN)) |
| 2949 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2951 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | /* |
| 2953 | * Select which case we're in here, and implement it. |
| 2954 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2955 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2956 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | /* |
| 2958 | * New allocation is contiguous with real allocations on the |
| 2959 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2960 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2962 | --bma->idx; |
| 2963 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2964 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | left.br_blockcount + new->br_blockcount + |
| 2966 | right.br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2967 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2968 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2969 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2970 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2971 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 2972 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1); |
| 2973 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2974 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2975 | } else { |
| 2976 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2977 | error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff, |
| 2978 | right.br_startblock, right.br_blockcount, |
| 2979 | &i); |
| 2980 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2981 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2982 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2983 | error = xfs_btree_delete(bma->cur, &i); |
| 2984 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2985 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2986 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2987 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 2988 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2989 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2990 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2991 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2992 | left.br_startblock, |
| 2993 | left.br_blockcount + |
| 2994 | new->br_blockcount + |
| 2995 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2996 | left.br_state); |
| 2997 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2998 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3000 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3002 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | /* |
| 3004 | * New allocation is contiguous with a real allocation |
| 3005 | * on the left. |
| 3006 | * Merge the new allocation with the left neighbor. |
| 3007 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3008 | --bma->idx; |
| 3009 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3010 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3012 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3013 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3014 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3015 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3016 | } else { |
| 3017 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3018 | error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff, |
| 3019 | left.br_startblock, left.br_blockcount, |
| 3020 | &i); |
| 3021 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3022 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3023 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3024 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3025 | left.br_startblock, |
| 3026 | left.br_blockcount + |
| 3027 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3028 | left.br_state); |
| 3029 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3030 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3032 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3034 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | /* |
| 3036 | * New allocation is contiguous with a real allocation |
| 3037 | * on the right. |
| 3038 | * Merge the new allocation with the right neighbor. |
| 3039 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3040 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3041 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 3042 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | new->br_blockcount + right.br_blockcount, |
| 3044 | right.br_state); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3045 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3046 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3047 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3048 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3049 | } else { |
| 3050 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3051 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3052 | right.br_startoff, |
| 3053 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3054 | right.br_blockcount, &i); |
| 3055 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3056 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3057 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3058 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3059 | new->br_startblock, |
| 3060 | new->br_blockcount + |
| 3061 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3062 | right.br_state); |
| 3063 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3064 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3066 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | |
| 3068 | case 0: |
| 3069 | /* |
| 3070 | * New allocation is not contiguous with another |
| 3071 | * real allocation. |
| 3072 | * Insert a new entry. |
| 3073 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3074 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 3075 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 3076 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1); |
| 3077 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3078 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3079 | } else { |
| 3080 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3081 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3082 | new->br_startoff, |
| 3083 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3084 | new->br_blockcount, &i); |
| 3085 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3086 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3087 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3088 | bma->cur->bc_rec.b.br_state = new->br_state; |
| 3089 | error = xfs_btree_insert(bma->cur, &i); |
| 3090 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3091 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3092 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3094 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3096 | |
| 3097 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3098 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3099 | int tmp_logflags; /* partial log flag return val */ |
| 3100 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3101 | ASSERT(bma->cur == NULL); |
| 3102 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 3103 | bma->firstblock, bma->dfops, &bma->cur, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3104 | 0, &tmp_logflags, whichfork); |
| 3105 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3106 | if (error) |
| 3107 | goto done; |
| 3108 | } |
| 3109 | |
| 3110 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3111 | if (bma->cur) |
| 3112 | bma->cur->bc_private.b.allocated = 0; |
| 3113 | |
| 3114 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3115 | done: |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3116 | bma->logflags |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3117 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | } |
| 3119 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3120 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 3121 | * Functions used in the extent read, allocate and remove paths |
| 3122 | */ |
| 3123 | |
| 3124 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3125 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 3126 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3127 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3128 | xfs_bmap_extsize_align( |
| 3129 | xfs_mount_t *mp, |
| 3130 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 3131 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 3132 | xfs_extlen_t extsz, /* align to this extent size */ |
| 3133 | int rt, /* is this a realtime inode? */ |
| 3134 | int eof, /* is extent at end-of-file? */ |
| 3135 | int delay, /* creating delalloc extent? */ |
| 3136 | int convert, /* overwriting unwritten extent? */ |
| 3137 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 3138 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 3139 | { |
| 3140 | xfs_fileoff_t orig_off; /* original offset */ |
| 3141 | xfs_extlen_t orig_alen; /* original length */ |
| 3142 | xfs_fileoff_t orig_end; /* original off+len */ |
| 3143 | xfs_fileoff_t nexto; /* next file offset */ |
| 3144 | xfs_fileoff_t prevo; /* previous file offset */ |
| 3145 | xfs_fileoff_t align_off; /* temp for offset */ |
| 3146 | xfs_extlen_t align_alen; /* temp for length */ |
| 3147 | xfs_extlen_t temp; /* temp for calculations */ |
| 3148 | |
| 3149 | if (convert) |
| 3150 | return 0; |
| 3151 | |
| 3152 | orig_off = align_off = *offp; |
| 3153 | orig_alen = align_alen = *lenp; |
| 3154 | orig_end = orig_off + orig_alen; |
| 3155 | |
| 3156 | /* |
| 3157 | * If this request overlaps an existing extent, then don't |
| 3158 | * attempt to perform any additional alignment. |
| 3159 | */ |
| 3160 | if (!delay && !eof && |
| 3161 | (orig_off >= gotp->br_startoff) && |
| 3162 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 3163 | return 0; |
| 3164 | } |
| 3165 | |
| 3166 | /* |
| 3167 | * If the file offset is unaligned vs. the extent size |
| 3168 | * we need to align it. This will be possible unless |
| 3169 | * the file was previously written with a kernel that didn't |
| 3170 | * perform this alignment, or if a truncate shot us in the |
| 3171 | * foot. |
| 3172 | */ |
| 3173 | temp = do_mod(orig_off, extsz); |
| 3174 | if (temp) { |
| 3175 | align_alen += temp; |
| 3176 | align_off -= temp; |
| 3177 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3178 | |
| 3179 | /* Same adjustment for the end of the requested area. */ |
| 3180 | temp = (align_alen % extsz); |
| 3181 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3182 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3183 | |
| 3184 | /* |
| 3185 | * For large extent hint sizes, the aligned extent might be larger than |
| 3186 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 3187 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 3188 | * short allocation just fine, so it is safe to do this. We only want to |
| 3189 | * do it when we are forced to, though, because it means more allocation |
| 3190 | * operations are required. |
| 3191 | */ |
| 3192 | while (align_alen > MAXEXTLEN) |
| 3193 | align_alen -= extsz; |
| 3194 | ASSERT(align_alen <= MAXEXTLEN); |
| 3195 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3196 | /* |
| 3197 | * If the previous block overlaps with this proposed allocation |
| 3198 | * then move the start forward without adjusting the length. |
| 3199 | */ |
| 3200 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3201 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3202 | prevo = prevp->br_startoff; |
| 3203 | else |
| 3204 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3205 | } else |
| 3206 | prevo = 0; |
| 3207 | if (align_off != orig_off && align_off < prevo) |
| 3208 | align_off = prevo; |
| 3209 | /* |
| 3210 | * If the next block overlaps with this proposed allocation |
| 3211 | * then move the start back without adjusting the length, |
| 3212 | * but not before offset 0. |
| 3213 | * This may of course make the start overlap previous block, |
| 3214 | * and if we hit the offset 0 limit then the next block |
| 3215 | * can still overlap too. |
| 3216 | */ |
| 3217 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3218 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3219 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3220 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3221 | else |
| 3222 | nexto = gotp->br_startoff; |
| 3223 | } else |
| 3224 | nexto = NULLFILEOFF; |
| 3225 | if (!eof && |
| 3226 | align_off + align_alen != orig_end && |
| 3227 | align_off + align_alen > nexto) |
| 3228 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3229 | /* |
| 3230 | * If we're now overlapping the next or previous extent that |
| 3231 | * means we can't fit an extsz piece in this hole. Just move |
| 3232 | * the start forward to the first valid spot and set |
| 3233 | * the length so we hit the end. |
| 3234 | */ |
| 3235 | if (align_off != orig_off && align_off < prevo) |
| 3236 | align_off = prevo; |
| 3237 | if (align_off + align_alen != orig_end && |
| 3238 | align_off + align_alen > nexto && |
| 3239 | nexto != NULLFILEOFF) { |
| 3240 | ASSERT(nexto > prevo); |
| 3241 | align_alen = nexto - align_off; |
| 3242 | } |
| 3243 | |
| 3244 | /* |
| 3245 | * If realtime, and the result isn't a multiple of the realtime |
| 3246 | * extent size we need to remove blocks until it is. |
| 3247 | */ |
| 3248 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3249 | /* |
| 3250 | * We're not covering the original request, or |
| 3251 | * we won't be able to once we fix the length. |
| 3252 | */ |
| 3253 | if (orig_off < align_off || |
| 3254 | orig_end > align_off + align_alen || |
| 3255 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3256 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3257 | /* |
| 3258 | * Try to fix it by moving the start up. |
| 3259 | */ |
| 3260 | if (align_off + temp <= orig_off) { |
| 3261 | align_alen -= temp; |
| 3262 | align_off += temp; |
| 3263 | } |
| 3264 | /* |
| 3265 | * Try to fix it by moving the end in. |
| 3266 | */ |
| 3267 | else if (align_off + align_alen - temp >= orig_end) |
| 3268 | align_alen -= temp; |
| 3269 | /* |
| 3270 | * Set the start to the minimum then trim the length. |
| 3271 | */ |
| 3272 | else { |
| 3273 | align_alen -= orig_off - align_off; |
| 3274 | align_off = orig_off; |
| 3275 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3276 | } |
| 3277 | /* |
| 3278 | * Result doesn't cover the request, fail it. |
| 3279 | */ |
| 3280 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3281 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3282 | } else { |
| 3283 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3284 | /* see MAXEXTLEN handling above */ |
| 3285 | ASSERT(orig_end <= align_off + align_alen || |
| 3286 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | #ifdef DEBUG |
| 3290 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3291 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3292 | if (prevp->br_startoff != NULLFILEOFF) |
| 3293 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3294 | #endif |
| 3295 | |
| 3296 | *lenp = align_alen; |
| 3297 | *offp = align_off; |
| 3298 | return 0; |
| 3299 | } |
| 3300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3302 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3303 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3304 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3305 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | { |
| 3307 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3309 | xfs_mount_t *mp; /* mount point structure */ |
| 3310 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3311 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | |
| 3313 | #define ISVALID(x,y) \ |
| 3314 | (rt ? \ |
| 3315 | (x) < mp->m_sb.sb_rblocks : \ |
| 3316 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3317 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3318 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3321 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3323 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | /* |
| 3325 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3326 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3327 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3328 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3329 | !isnullstartblock(ap->prev.br_startblock) && |
| 3330 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3331 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3332 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | /* |
| 3334 | * Adjust for the gap between prevp and us. |
| 3335 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3336 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3337 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3339 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3340 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3341 | } |
| 3342 | /* |
| 3343 | * If not at eof, then compare the two neighbor blocks. |
| 3344 | * Figure out whether either one gives us a good starting point, |
| 3345 | * and pick the better one. |
| 3346 | */ |
| 3347 | else if (!ap->eof) { |
| 3348 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3349 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3350 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3351 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3352 | |
| 3353 | /* |
| 3354 | * If there's a previous (left) block, select a requested |
| 3355 | * start block based on it. |
| 3356 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3357 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3358 | !isnullstartblock(ap->prev.br_startblock) && |
| 3359 | (prevbno = ap->prev.br_startblock + |
| 3360 | ap->prev.br_blockcount) && |
| 3361 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | /* |
| 3363 | * Calculate gap to end of previous block. |
| 3364 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3365 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3366 | (ap->prev.br_startoff + |
| 3367 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | /* |
| 3369 | * Figure the startblock based on the previous block's |
| 3370 | * end and the gap size. |
| 3371 | * Heuristic! |
| 3372 | * If the gap is large relative to the piece we're |
| 3373 | * allocating, or using it gives us an invalid block |
| 3374 | * number, then just use the end of the previous block. |
| 3375 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3376 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3378 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | prevbno += adjust; |
| 3380 | else |
| 3381 | prevdiff += adjust; |
| 3382 | /* |
| 3383 | * If the firstblock forbids it, can't use it, |
| 3384 | * must use default. |
| 3385 | */ |
| 3386 | if (!rt && !nullfb && |
| 3387 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3388 | prevbno = NULLFSBLOCK; |
| 3389 | } |
| 3390 | /* |
| 3391 | * No previous block or can't follow it, just default. |
| 3392 | */ |
| 3393 | else |
| 3394 | prevbno = NULLFSBLOCK; |
| 3395 | /* |
| 3396 | * If there's a following (right) block, select a requested |
| 3397 | * start block based on it. |
| 3398 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3399 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | /* |
| 3401 | * Calculate gap to start of next block. |
| 3402 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3403 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | /* |
| 3405 | * Figure the startblock based on the next block's |
| 3406 | * start and the gap size. |
| 3407 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3408 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | /* |
| 3410 | * Heuristic! |
| 3411 | * If the gap is large relative to the piece we're |
| 3412 | * allocating, or using it gives us an invalid block |
| 3413 | * number, then just use the start of the next block |
| 3414 | * offset by our length. |
| 3415 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3416 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3418 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3419 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3420 | gotbno -= ap->length; |
| 3421 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | } else |
| 3423 | gotdiff += adjust; |
| 3424 | /* |
| 3425 | * If the firstblock forbids it, can't use it, |
| 3426 | * must use default. |
| 3427 | */ |
| 3428 | if (!rt && !nullfb && |
| 3429 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3430 | gotbno = NULLFSBLOCK; |
| 3431 | } |
| 3432 | /* |
| 3433 | * No next block, just default. |
| 3434 | */ |
| 3435 | else |
| 3436 | gotbno = NULLFSBLOCK; |
| 3437 | /* |
| 3438 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3439 | * 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] | 3440 | */ |
| 3441 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3442 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3444 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3446 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3448 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3449 | } |
| 3450 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3451 | static int |
| 3452 | xfs_bmap_longest_free_extent( |
| 3453 | struct xfs_trans *tp, |
| 3454 | xfs_agnumber_t ag, |
| 3455 | xfs_extlen_t *blen, |
| 3456 | int *notinit) |
| 3457 | { |
| 3458 | struct xfs_mount *mp = tp->t_mountp; |
| 3459 | struct xfs_perag *pag; |
| 3460 | xfs_extlen_t longest; |
| 3461 | int error = 0; |
| 3462 | |
| 3463 | pag = xfs_perag_get(mp, ag); |
| 3464 | if (!pag->pagf_init) { |
| 3465 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3466 | if (error) |
| 3467 | goto out; |
| 3468 | |
| 3469 | if (!pag->pagf_init) { |
| 3470 | *notinit = 1; |
| 3471 | goto out; |
| 3472 | } |
| 3473 | } |
| 3474 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 3475 | longest = xfs_alloc_longest_free_extent(mp, pag, |
Dave Chinner | 496817b | 2015-06-22 10:13:30 +1000 | [diff] [blame] | 3476 | xfs_alloc_min_freelist(mp, pag)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3477 | if (*blen < longest) |
| 3478 | *blen = longest; |
| 3479 | |
| 3480 | out: |
| 3481 | xfs_perag_put(pag); |
| 3482 | return error; |
| 3483 | } |
| 3484 | |
| 3485 | static void |
| 3486 | xfs_bmap_select_minlen( |
| 3487 | struct xfs_bmalloca *ap, |
| 3488 | struct xfs_alloc_arg *args, |
| 3489 | xfs_extlen_t *blen, |
| 3490 | int notinit) |
| 3491 | { |
| 3492 | if (notinit || *blen < ap->minlen) { |
| 3493 | /* |
| 3494 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3495 | * there is space for this request. |
| 3496 | */ |
| 3497 | args->minlen = ap->minlen; |
| 3498 | } else if (*blen < args->maxlen) { |
| 3499 | /* |
| 3500 | * If the best seen length is less than the request length, |
| 3501 | * use the best as the minimum. |
| 3502 | */ |
| 3503 | args->minlen = *blen; |
| 3504 | } else { |
| 3505 | /* |
| 3506 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3507 | * as the minimum. |
| 3508 | */ |
| 3509 | args->minlen = args->maxlen; |
| 3510 | } |
| 3511 | } |
| 3512 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3513 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3514 | xfs_bmap_btalloc_nullfb( |
| 3515 | struct xfs_bmalloca *ap, |
| 3516 | struct xfs_alloc_arg *args, |
| 3517 | xfs_extlen_t *blen) |
| 3518 | { |
| 3519 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3520 | xfs_agnumber_t ag, startag; |
| 3521 | int notinit = 0; |
| 3522 | int error; |
| 3523 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3524 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3525 | args->total = ap->total; |
| 3526 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3527 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3528 | if (startag == NULLAGNUMBER) |
| 3529 | startag = ag = 0; |
| 3530 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3531 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3532 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3533 | ¬init); |
| 3534 | if (error) |
| 3535 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3536 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3537 | if (++ag == mp->m_sb.sb_agcount) |
| 3538 | ag = 0; |
| 3539 | if (ag == startag) |
| 3540 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3541 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3542 | |
| 3543 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3544 | return 0; |
| 3545 | } |
| 3546 | |
| 3547 | STATIC int |
| 3548 | xfs_bmap_btalloc_filestreams( |
| 3549 | struct xfs_bmalloca *ap, |
| 3550 | struct xfs_alloc_arg *args, |
| 3551 | xfs_extlen_t *blen) |
| 3552 | { |
| 3553 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3554 | xfs_agnumber_t ag; |
| 3555 | int notinit = 0; |
| 3556 | int error; |
| 3557 | |
| 3558 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3559 | args->total = ap->total; |
| 3560 | |
| 3561 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3562 | if (ag == NULLAGNUMBER) |
| 3563 | ag = 0; |
| 3564 | |
| 3565 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3566 | if (error) |
| 3567 | return error; |
| 3568 | |
| 3569 | if (*blen < args->maxlen) { |
| 3570 | error = xfs_filestream_new_ag(ap, &ag); |
| 3571 | if (error) |
| 3572 | return error; |
| 3573 | |
| 3574 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3575 | ¬init); |
| 3576 | if (error) |
| 3577 | return error; |
| 3578 | |
| 3579 | } |
| 3580 | |
| 3581 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3582 | |
| 3583 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3584 | * Set the failure fallback case to look in the selected AG as stream |
| 3585 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3586 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3587 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3588 | return 0; |
| 3589 | } |
| 3590 | |
| 3591 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3592 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3593 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3594 | { |
| 3595 | xfs_mount_t *mp; /* mount point structure */ |
| 3596 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 3597 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3598 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3599 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3600 | xfs_alloc_arg_t args; |
| 3601 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3602 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3603 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3604 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3605 | int tryagain; |
| 3606 | int error; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3607 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3608 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3609 | ASSERT(ap->length); |
| 3610 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3611 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3612 | |
| 3613 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3614 | stripe_align = 0; |
| 3615 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3616 | stripe_align = mp->m_swidth; |
| 3617 | else if (mp->m_dalign) |
| 3618 | stripe_align = mp->m_dalign; |
| 3619 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3620 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3621 | if (unlikely(align)) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3622 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3623 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3624 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3625 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3626 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3627 | } |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3628 | |
| 3629 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3630 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3631 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3632 | if (nullfb) { |
| 3633 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 3634 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3635 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3636 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3637 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3638 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3639 | } |
| 3640 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3641 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3642 | |
| 3643 | xfs_bmap_adjacent(ap); |
| 3644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3646 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | * it's in the right allocation group. |
| 3648 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3649 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | ; |
| 3651 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3652 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | * Normal allocation, done through xfs_alloc_vextent. |
| 3655 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3656 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3657 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3658 | args.tp = ap->tp; |
| 3659 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3660 | args.fsbno = ap->blkno; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3661 | |
| 3662 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3663 | args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3664 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3665 | blen = 0; |
| 3666 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3667 | /* |
| 3668 | * Search for an allocation group with a single extent large |
| 3669 | * enough for the request. If one isn't found, then adjust |
| 3670 | * the minimum allocation size to the largest space found. |
| 3671 | */ |
| 3672 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 3673 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3674 | else |
| 3675 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3676 | if (error) |
| 3677 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 3678 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3679 | if (xfs_inode_is_filestream(ap->ip)) |
| 3680 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3681 | else |
| 3682 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3683 | args.total = args.minlen = ap->minlen; |
| 3684 | } else { |
| 3685 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3686 | args.total = ap->total; |
| 3687 | args.minlen = ap->minlen; |
| 3688 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3689 | /* apply extent size hints if obtained earlier */ |
| 3690 | if (unlikely(align)) { |
| 3691 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3692 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3693 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3694 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3695 | args.prod = 1; |
| 3696 | args.mod = 0; |
| 3697 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3698 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3699 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3700 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3701 | } |
| 3702 | /* |
| 3703 | * If we are not low on available data blocks, and the |
| 3704 | * underlying logical volume manager is a stripe, and |
| 3705 | * the file offset is zero then try to allocate data |
| 3706 | * blocks on stripe unit boundary. |
| 3707 | * NOTE: ap->aeof is only set if the allocation length |
| 3708 | * is >= the stripe unit and the allocation offset is |
| 3709 | * at the end of file. |
| 3710 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 3711 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3712 | if (!ap->offset) { |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3713 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3714 | atype = args.type; |
| 3715 | isaligned = 1; |
| 3716 | /* |
| 3717 | * Adjust for alignment |
| 3718 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3719 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3720 | args.minlen = blen - args.alignment; |
| 3721 | args.minalignslop = 0; |
| 3722 | } else { |
| 3723 | /* |
| 3724 | * First try an exact bno allocation. |
| 3725 | * If it fails then do a near or start bno |
| 3726 | * allocation with alignment turned on. |
| 3727 | */ |
| 3728 | atype = args.type; |
| 3729 | tryagain = 1; |
| 3730 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3731 | args.alignment = 1; |
| 3732 | /* |
| 3733 | * Compute the minlen+alignment for the |
| 3734 | * next case. Set slop so that the value |
| 3735 | * of minlen+alignment+slop doesn't go up |
| 3736 | * between the calls. |
| 3737 | */ |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3738 | if (blen > stripe_align && blen <= args.maxlen) |
| 3739 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3740 | else |
| 3741 | nextminlen = args.minlen; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3742 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3743 | args.minalignslop = |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3744 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3745 | args.minlen - 1; |
| 3746 | else |
| 3747 | args.minalignslop = 0; |
| 3748 | } |
| 3749 | } else { |
| 3750 | args.alignment = 1; |
| 3751 | args.minalignslop = 0; |
| 3752 | } |
| 3753 | args.minleft = ap->minleft; |
| 3754 | args.wasdel = ap->wasdel; |
| 3755 | args.isfl = 0; |
| 3756 | args.userdata = ap->userdata; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3757 | if (ap->userdata & XFS_ALLOC_USERDATA_ZERO) |
| 3758 | args.ip = ap->ip; |
| 3759 | |
| 3760 | error = xfs_alloc_vextent(&args); |
| 3761 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3762 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3763 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3764 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3765 | /* |
| 3766 | * Exact allocation failed. Now try with alignment |
| 3767 | * turned on. |
| 3768 | */ |
| 3769 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3770 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3771 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3772 | args.minlen = nextminlen; |
| 3773 | args.minalignslop = 0; |
| 3774 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | if ((error = xfs_alloc_vextent(&args))) |
| 3776 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3777 | } |
| 3778 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3779 | /* |
| 3780 | * allocation failed, so turn off alignment and |
| 3781 | * try again. |
| 3782 | */ |
| 3783 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3784 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3785 | args.alignment = 0; |
| 3786 | if ((error = xfs_alloc_vextent(&args))) |
| 3787 | return error; |
| 3788 | } |
| 3789 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3790 | args.minlen > ap->minlen) { |
| 3791 | args.minlen = ap->minlen; |
| 3792 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3793 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3794 | if ((error = xfs_alloc_vextent(&args))) |
| 3795 | return error; |
| 3796 | } |
| 3797 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3798 | args.fsbno = 0; |
| 3799 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3800 | args.total = ap->minlen; |
| 3801 | args.minleft = 0; |
| 3802 | if ((error = xfs_alloc_vextent(&args))) |
| 3803 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 3804 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3805 | } |
| 3806 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3807 | /* |
| 3808 | * check the allocation happened at the same or higher AG than |
| 3809 | * the first block that was allocated. |
| 3810 | */ |
| 3811 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
| 3812 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == |
| 3813 | XFS_FSB_TO_AGNO(mp, args.fsbno) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 3814 | (ap->dfops->dop_low && |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3815 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < |
| 3816 | XFS_FSB_TO_AGNO(mp, args.fsbno))); |
| 3817 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3818 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3819 | if (*ap->firstblock == NULLFSBLOCK) |
| 3820 | *ap->firstblock = args.fsbno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3821 | ASSERT(nullfb || fb_agno == args.agno || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 3822 | (ap->dfops->dop_low && fb_agno < args.agno)); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3823 | ap->length = args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3824 | ap->ip->i_d.di_nblocks += args.len; |
| 3825 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3826 | if (ap->wasdel) |
| 3827 | ap->ip->i_delayed_blks -= args.len; |
| 3828 | /* |
| 3829 | * Adjust the disk quota also. This was reserved |
| 3830 | * earlier. |
| 3831 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3832 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3833 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3834 | XFS_TRANS_DQ_BCOUNT, |
| 3835 | (long) args.len); |
| 3836 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3837 | ap->blkno = NULLFSBLOCK; |
| 3838 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | } |
| 3840 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | /* |
| 3844 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3845 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3846 | */ |
| 3847 | STATIC int |
| 3848 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3849 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3850 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3851 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3852 | return xfs_bmap_rtalloc(ap); |
| 3853 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3857 | * Trim the returned map to the required bounds |
| 3858 | */ |
| 3859 | STATIC void |
| 3860 | xfs_bmapi_trim_map( |
| 3861 | struct xfs_bmbt_irec *mval, |
| 3862 | struct xfs_bmbt_irec *got, |
| 3863 | xfs_fileoff_t *bno, |
| 3864 | xfs_filblks_t len, |
| 3865 | xfs_fileoff_t obno, |
| 3866 | xfs_fileoff_t end, |
| 3867 | int n, |
| 3868 | int flags) |
| 3869 | { |
| 3870 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3871 | got->br_startoff + got->br_blockcount <= obno) { |
| 3872 | *mval = *got; |
| 3873 | if (isnullstartblock(got->br_startblock)) |
| 3874 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3875 | return; |
| 3876 | } |
| 3877 | |
| 3878 | if (obno > *bno) |
| 3879 | *bno = obno; |
| 3880 | ASSERT((*bno >= obno) || (n == 0)); |
| 3881 | ASSERT(*bno < end); |
| 3882 | mval->br_startoff = *bno; |
| 3883 | if (isnullstartblock(got->br_startblock)) |
| 3884 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3885 | else |
| 3886 | mval->br_startblock = got->br_startblock + |
| 3887 | (*bno - got->br_startoff); |
| 3888 | /* |
| 3889 | * Return the minimum of what we got and what we asked for for |
| 3890 | * the length. We can use the len variable here because it is |
| 3891 | * modified below and we could have been there before coming |
| 3892 | * here if the first part of the allocation didn't overlap what |
| 3893 | * was asked for. |
| 3894 | */ |
| 3895 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3896 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3897 | mval->br_state = got->br_state; |
| 3898 | ASSERT(mval->br_blockcount <= len); |
| 3899 | return; |
| 3900 | } |
| 3901 | |
| 3902 | /* |
| 3903 | * Update and validate the extent map to return |
| 3904 | */ |
| 3905 | STATIC void |
| 3906 | xfs_bmapi_update_map( |
| 3907 | struct xfs_bmbt_irec **map, |
| 3908 | xfs_fileoff_t *bno, |
| 3909 | xfs_filblks_t *len, |
| 3910 | xfs_fileoff_t obno, |
| 3911 | xfs_fileoff_t end, |
| 3912 | int *n, |
| 3913 | int flags) |
| 3914 | { |
| 3915 | xfs_bmbt_irec_t *mval = *map; |
| 3916 | |
| 3917 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3918 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3919 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3920 | (mval->br_startoff < obno)); |
| 3921 | |
| 3922 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3923 | *len = end - *bno; |
| 3924 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3925 | /* update previous map with new information */ |
| 3926 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3927 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3928 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3929 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3930 | mval[-1].br_state = mval->br_state; |
| 3931 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3932 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3933 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3934 | mval->br_startblock == mval[-1].br_startblock + |
| 3935 | mval[-1].br_blockcount && |
| 3936 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3937 | mval[-1].br_state == mval->br_state)) { |
| 3938 | ASSERT(mval->br_startoff == |
| 3939 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3940 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3941 | } else if (*n > 0 && |
| 3942 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3943 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3944 | mval->br_startoff == |
| 3945 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3946 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3947 | mval[-1].br_state = mval->br_state; |
| 3948 | } else if (!((*n == 0) && |
| 3949 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3950 | obno))) { |
| 3951 | mval++; |
| 3952 | (*n)++; |
| 3953 | } |
| 3954 | *map = mval; |
| 3955 | } |
| 3956 | |
| 3957 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3958 | * Map file blocks to filesystem blocks without allocation. |
| 3959 | */ |
| 3960 | int |
| 3961 | xfs_bmapi_read( |
| 3962 | struct xfs_inode *ip, |
| 3963 | xfs_fileoff_t bno, |
| 3964 | xfs_filblks_t len, |
| 3965 | struct xfs_bmbt_irec *mval, |
| 3966 | int *nmap, |
| 3967 | int flags) |
| 3968 | { |
| 3969 | struct xfs_mount *mp = ip->i_mount; |
| 3970 | struct xfs_ifork *ifp; |
| 3971 | struct xfs_bmbt_irec got; |
| 3972 | struct xfs_bmbt_irec prev; |
| 3973 | xfs_fileoff_t obno; |
| 3974 | xfs_fileoff_t end; |
| 3975 | xfs_extnum_t lastx; |
| 3976 | int error; |
| 3977 | int eof; |
| 3978 | int n = 0; |
| 3979 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 3980 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 3981 | |
| 3982 | ASSERT(*nmap >= 1); |
| 3983 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
| 3984 | XFS_BMAPI_IGSTATE))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3985 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3986 | |
| 3987 | if (unlikely(XFS_TEST_ERROR( |
| 3988 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3989 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 3990 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 3991 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3992 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3996 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3997 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3998 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3999 | |
| 4000 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4001 | |
| 4002 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4003 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4004 | if (error) |
| 4005 | return error; |
| 4006 | } |
| 4007 | |
| 4008 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev); |
| 4009 | end = bno + len; |
| 4010 | obno = bno; |
| 4011 | |
| 4012 | while (bno < end && n < *nmap) { |
| 4013 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4014 | if (eof) |
| 4015 | got.br_startoff = end; |
| 4016 | if (got.br_startoff > bno) { |
| 4017 | /* Reading in a hole. */ |
| 4018 | mval->br_startoff = bno; |
| 4019 | mval->br_startblock = HOLESTARTBLOCK; |
| 4020 | mval->br_blockcount = |
| 4021 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4022 | mval->br_state = XFS_EXT_NORM; |
| 4023 | bno += mval->br_blockcount; |
| 4024 | len -= mval->br_blockcount; |
| 4025 | mval++; |
| 4026 | n++; |
| 4027 | continue; |
| 4028 | } |
| 4029 | |
| 4030 | /* set up the extent map to return. */ |
| 4031 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4032 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4033 | |
| 4034 | /* If we're done, stop now. */ |
| 4035 | if (bno >= end || n >= *nmap) |
| 4036 | break; |
| 4037 | |
| 4038 | /* Else go on to the next record. */ |
| 4039 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4040 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4041 | else |
| 4042 | eof = 1; |
| 4043 | } |
| 4044 | *nmap = n; |
| 4045 | return 0; |
| 4046 | } |
| 4047 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4048 | STATIC int |
| 4049 | xfs_bmapi_reserve_delalloc( |
| 4050 | struct xfs_inode *ip, |
| 4051 | xfs_fileoff_t aoff, |
| 4052 | xfs_filblks_t len, |
| 4053 | struct xfs_bmbt_irec *got, |
| 4054 | struct xfs_bmbt_irec *prev, |
| 4055 | xfs_extnum_t *lastx, |
| 4056 | int eof) |
| 4057 | { |
| 4058 | struct xfs_mount *mp = ip->i_mount; |
| 4059 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4060 | xfs_extlen_t alen; |
| 4061 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4062 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4063 | xfs_extlen_t extsz; |
| 4064 | int error; |
| 4065 | |
| 4066 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4067 | if (!eof) |
| 4068 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
| 4069 | |
| 4070 | /* Figure out the extent size, adjust alen */ |
| 4071 | extsz = xfs_get_extsz_hint(ip); |
| 4072 | if (extsz) { |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4073 | error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof, |
| 4074 | 1, 0, &aoff, &alen); |
| 4075 | ASSERT(!error); |
| 4076 | } |
| 4077 | |
| 4078 | if (rt) |
| 4079 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4080 | |
| 4081 | /* |
| 4082 | * Make a transaction-less quota reservation for delayed allocation |
| 4083 | * blocks. This number gets adjusted later. We return if we haven't |
| 4084 | * allocated blocks already inside this loop. |
| 4085 | */ |
| 4086 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4087 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4088 | if (error) |
| 4089 | return error; |
| 4090 | |
| 4091 | /* |
| 4092 | * Split changing sb for alen and indlen since they could be coming |
| 4093 | * from different places. |
| 4094 | */ |
| 4095 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4096 | ASSERT(indlen > 0); |
| 4097 | |
| 4098 | if (rt) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4099 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4100 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4101 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | if (error) |
| 4105 | goto out_unreserve_quota; |
| 4106 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4107 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4108 | if (error) |
| 4109 | goto out_unreserve_blocks; |
| 4110 | |
| 4111 | |
| 4112 | ip->i_delayed_blks += alen; |
| 4113 | |
| 4114 | got->br_startoff = aoff; |
| 4115 | got->br_startblock = nullstartblock(indlen); |
| 4116 | got->br_blockcount = alen; |
| 4117 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4118 | xfs_bmap_add_extent_hole_delay(ip, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4119 | |
| 4120 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4121 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay |
| 4122 | * might have merged it into one of the neighbouring ones. |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4123 | */ |
| 4124 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); |
| 4125 | |
| 4126 | ASSERT(got->br_startoff <= aoff); |
| 4127 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); |
| 4128 | ASSERT(isnullstartblock(got->br_startblock)); |
| 4129 | ASSERT(got->br_state == XFS_EXT_NORM); |
| 4130 | return 0; |
| 4131 | |
| 4132 | out_unreserve_blocks: |
| 4133 | if (rt) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4134 | xfs_mod_frextents(mp, extsz); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4135 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4136 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4137 | out_unreserve_quota: |
| 4138 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 4139 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4140 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4141 | return error; |
| 4142 | } |
| 4143 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4144 | /* |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4145 | * Map file blocks to filesystem blocks, adding delayed allocations as needed. |
| 4146 | */ |
| 4147 | int |
| 4148 | xfs_bmapi_delay( |
| 4149 | struct xfs_inode *ip, /* incore inode */ |
| 4150 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4151 | xfs_filblks_t len, /* length to map in file */ |
| 4152 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4153 | int *nmap, /* i/o: mval size/count */ |
| 4154 | int flags) /* XFS_BMAPI_... */ |
| 4155 | { |
| 4156 | struct xfs_mount *mp = ip->i_mount; |
| 4157 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4158 | struct xfs_bmbt_irec got; /* current file extent record */ |
| 4159 | struct xfs_bmbt_irec prev; /* previous file extent record */ |
| 4160 | xfs_fileoff_t obno; /* old block number (offset) */ |
| 4161 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4162 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4163 | int eof; /* we've hit the end of extents */ |
| 4164 | int n = 0; /* current extent index */ |
| 4165 | int error = 0; |
| 4166 | |
| 4167 | ASSERT(*nmap >= 1); |
| 4168 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4169 | ASSERT(!(flags & ~XFS_BMAPI_ENTIRE)); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4170 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4171 | |
| 4172 | if (unlikely(XFS_TEST_ERROR( |
| 4173 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4174 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 4175 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4176 | XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4177 | return -EFSCORRUPTED; |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4181 | return -EIO; |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4182 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4183 | XFS_STATS_INC(mp, xs_blk_mapw); |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4184 | |
| 4185 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4186 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4187 | if (error) |
| 4188 | return error; |
| 4189 | } |
| 4190 | |
| 4191 | xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev); |
| 4192 | end = bno + len; |
| 4193 | obno = bno; |
| 4194 | |
| 4195 | while (bno < end && n < *nmap) { |
| 4196 | if (eof || got.br_startoff > bno) { |
| 4197 | error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got, |
| 4198 | &prev, &lastx, eof); |
| 4199 | if (error) { |
| 4200 | if (n == 0) { |
| 4201 | *nmap = 0; |
| 4202 | return error; |
| 4203 | } |
| 4204 | break; |
| 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | /* set up the extent map to return. */ |
| 4209 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4210 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4211 | |
| 4212 | /* If we're done, stop now. */ |
| 4213 | if (bno >= end || n >= *nmap) |
| 4214 | break; |
| 4215 | |
| 4216 | /* Else go on to the next record. */ |
| 4217 | prev = got; |
| 4218 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4219 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4220 | else |
| 4221 | eof = 1; |
| 4222 | } |
| 4223 | |
| 4224 | *nmap = n; |
| 4225 | return 0; |
| 4226 | } |
| 4227 | |
| 4228 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4229 | static int |
| 4230 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4231 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4232 | { |
| 4233 | struct xfs_mount *mp = bma->ip->i_mount; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4234 | int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4235 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4236 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4237 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4238 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4239 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4240 | ASSERT(bma->length > 0); |
| 4241 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4242 | /* |
| 4243 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4244 | * for in this bmap call but that wouldn't be as good. |
| 4245 | */ |
| 4246 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4247 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4248 | bma->offset = bma->got.br_startoff; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4249 | if (bma->idx != NULLEXTNUM && bma->idx) { |
| 4250 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4251 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4252 | } |
| 4253 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4254 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4255 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4256 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4257 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | /* |
| 4261 | * 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] | 4262 | * user data. And if it is userdata, indicate whether it needs to |
| 4263 | * be initialised to zero during allocation. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4264 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4265 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4266 | bma->userdata = (bma->offset == 0) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4267 | XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4268 | if (bma->flags & XFS_BMAPI_ZERO) |
| 4269 | bma->userdata |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4272 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4273 | |
| 4274 | /* |
| 4275 | * Only want to do the alignment at the eof if it is userdata and |
| 4276 | * allocation length is larger than a stripe unit. |
| 4277 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4278 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4279 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4280 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4281 | if (error) |
| 4282 | return error; |
| 4283 | } |
| 4284 | |
| 4285 | error = xfs_bmap_alloc(bma); |
| 4286 | if (error) |
| 4287 | return error; |
| 4288 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 4289 | if (bma->dfops->dop_low) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4290 | bma->minleft = 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4291 | if (bma->cur) |
| 4292 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4293 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4294 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4295 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4296 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4297 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 4298 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4299 | } |
| 4300 | /* |
| 4301 | * Bump the number of extents we've allocated |
| 4302 | * in this call. |
| 4303 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4304 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4305 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4306 | if (bma->cur) |
| 4307 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4308 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4309 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4310 | bma->got.br_startoff = bma->offset; |
| 4311 | bma->got.br_startblock = bma->blkno; |
| 4312 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4313 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4314 | |
| 4315 | /* |
| 4316 | * A wasdelay extent has been initialized, so shouldn't be flagged |
| 4317 | * as unwritten. |
| 4318 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4319 | if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4320 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4321 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4322 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4323 | if (bma->wasdel) |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 4324 | error = xfs_bmap_add_extent_delay_real(bma); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4325 | else |
| 4326 | error = xfs_bmap_add_extent_hole_real(bma, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4327 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4328 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4329 | if (error) |
| 4330 | return error; |
| 4331 | |
| 4332 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4333 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4334 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4335 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4336 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4337 | 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] | 4338 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4339 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4340 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4341 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4342 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4343 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4344 | return 0; |
| 4345 | } |
| 4346 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4347 | STATIC int |
| 4348 | xfs_bmapi_convert_unwritten( |
| 4349 | struct xfs_bmalloca *bma, |
| 4350 | struct xfs_bmbt_irec *mval, |
| 4351 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4352 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4353 | { |
| 4354 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4355 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4356 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4357 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4358 | int error; |
| 4359 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4360 | /* check if we need to do unwritten->real conversion */ |
| 4361 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4362 | (flags & XFS_BMAPI_PREALLOC)) |
| 4363 | return 0; |
| 4364 | |
| 4365 | /* check if we need to do real->unwritten conversion */ |
| 4366 | if (mval->br_state == XFS_EXT_NORM && |
| 4367 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4368 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4369 | return 0; |
| 4370 | |
| 4371 | /* |
| 4372 | * Modify (by adding) the state flag, if writing. |
| 4373 | */ |
| 4374 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4375 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4376 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4377 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4378 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 4379 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4380 | } |
| 4381 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4382 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4383 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4384 | /* |
| 4385 | * Before insertion into the bmbt, zero the range being converted |
| 4386 | * if required. |
| 4387 | */ |
| 4388 | if (flags & XFS_BMAPI_ZERO) { |
| 4389 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4390 | mval->br_blockcount); |
| 4391 | if (error) |
| 4392 | return error; |
| 4393 | } |
| 4394 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4395 | 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^] | 4396 | &bma->cur, mval, bma->firstblock, bma->dfops, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4397 | &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4398 | /* |
| 4399 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4400 | * path because the conversion might not have done so (e.g., if the |
| 4401 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4402 | * in the transaction for the sake of fsync(), even if nothing has |
| 4403 | * changed, because fsync() will not force the log for this transaction |
| 4404 | * unless it sees the inode pinned. |
| 4405 | */ |
| 4406 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4407 | if (error) |
| 4408 | return error; |
| 4409 | |
| 4410 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4411 | * Update our extent pointer, given that |
| 4412 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4413 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4414 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4415 | 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] | 4416 | |
| 4417 | /* |
| 4418 | * We may have combined previously unwritten space with written space, |
| 4419 | * so generate another request. |
| 4420 | */ |
| 4421 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4422 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4423 | return 0; |
| 4424 | } |
| 4425 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4426 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4427 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4428 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4429 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4430 | * locking problems. |
| 4431 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | * The returned value in "firstblock" from the first call in a transaction |
| 4433 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4434 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4435 | * the first call in "total"; if no allocation group has that many free |
| 4436 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4437 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4438 | int |
| 4439 | xfs_bmapi_write( |
| 4440 | struct xfs_trans *tp, /* transaction pointer */ |
| 4441 | struct xfs_inode *ip, /* incore inode */ |
| 4442 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4443 | xfs_filblks_t len, /* length to map in file */ |
| 4444 | int flags, /* XFS_BMAPI_... */ |
| 4445 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4446 | controls a.g. for allocs */ |
| 4447 | xfs_extlen_t total, /* total blocks needed */ |
| 4448 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4449 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 4450 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4452 | struct xfs_mount *mp = ip->i_mount; |
| 4453 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4454 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4455 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4456 | int eof; /* after the end of extents */ |
| 4457 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4458 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4459 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4460 | int whichfork; /* data or attr fork */ |
| 4461 | char inhole; /* current location is hole in file */ |
| 4462 | char wasdelay; /* old extent was delayed */ |
| 4463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4465 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4466 | int orig_flags; /* original flags arg value */ |
| 4467 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4468 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4469 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | |
| 4471 | orig_bno = bno; |
| 4472 | orig_len = len; |
| 4473 | orig_flags = flags; |
| 4474 | orig_mval = mval; |
| 4475 | orig_nmap = *nmap; |
| 4476 | #endif |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4477 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4478 | XFS_ATTR_FORK : XFS_DATA_FORK; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4481 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4482 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
| 4483 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4484 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4485 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4486 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4487 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4488 | /* zeroing is for currently only for data extents, not metadata */ |
| 4489 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4490 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4491 | /* |
| 4492 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4493 | * but it makes no sense to do both at once. This would result in |
| 4494 | * zeroing the unwritten extent twice, but it still being an |
| 4495 | * unwritten extent.... |
| 4496 | */ |
| 4497 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4498 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | if (unlikely(XFS_TEST_ERROR( |
| 4501 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4502 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4504 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4505 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4509 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4512 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4513 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4514 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4515 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4517 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4519 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4520 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4521 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
| 4524 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4525 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4526 | if (error) |
| 4527 | goto error0; |
| 4528 | } |
| 4529 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4530 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4531 | &bma.prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | n = 0; |
| 4533 | end = bno + len; |
| 4534 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4535 | |
| 4536 | bma.tp = tp; |
| 4537 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4538 | bma.total = total; |
| 4539 | bma.userdata = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 4540 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4541 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | while (bno < end && n < *nmap) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4544 | inhole = eof || bma.got.br_startoff > bno; |
| 4545 | wasdelay = !inhole && isnullstartblock(bma.got.br_startblock); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | /* |
| 4548 | * First, deal with the hole before the allocated space |
| 4549 | * that we found, if any. |
| 4550 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4551 | if (inhole || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4552 | bma.eof = eof; |
| 4553 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4554 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4555 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4556 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4557 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4558 | /* |
| 4559 | * There's a 32/64 bit type mismatch between the |
| 4560 | * allocation length request (which can be 64 bits in |
| 4561 | * length) and the bma length request, which is |
| 4562 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4563 | * check for 32-bit overflows and handle them here. |
| 4564 | */ |
| 4565 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4566 | bma.length = MAXEXTLEN; |
| 4567 | else |
| 4568 | bma.length = len; |
| 4569 | |
| 4570 | ASSERT(len > 0); |
| 4571 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4572 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | if (error) |
| 4574 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4575 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4576 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4578 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4579 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4580 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4581 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4583 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4584 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4585 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4586 | continue; |
| 4587 | if (error) |
| 4588 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4590 | /* update the extent map to return */ |
| 4591 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | /* |
| 4594 | * If we're done, stop now. Stop when we've allocated |
| 4595 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4596 | * the transaction may get too big. |
| 4597 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4598 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4600 | |
| 4601 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4602 | bma.prev = bma.got; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4603 | if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) { |
| 4604 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx), |
| 4605 | &bma.got); |
| 4606 | } else |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4607 | eof = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | /* |
| 4612 | * Transform from btree to extents, give it cur. |
| 4613 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4614 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4615 | int tmp_logflags = 0; |
| 4616 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4617 | ASSERT(bma.cur); |
| 4618 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4620 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | if (error) |
| 4622 | goto error0; |
| 4623 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4626 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4627 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | error0: |
| 4630 | /* |
| 4631 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4632 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4634 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4636 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4637 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4639 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | /* |
| 4641 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4642 | * detecting a case where the data is changed, there's an error, |
| 4643 | * and it's not logged so we don't shutdown when we should. |
| 4644 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4645 | if (bma.logflags) |
| 4646 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4647 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4648 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | if (!error) { |
| 4650 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4651 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4652 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4653 | bma.cur->bc_private.b.firstblock) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 4654 | (dfops->dop_low && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4656 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4657 | bma.cur->bc_private.b.firstblock))); |
| 4658 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4660 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4662 | } |
| 4663 | if (!error) |
| 4664 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4665 | orig_nmap, *nmap); |
| 4666 | return error; |
| 4667 | } |
| 4668 | |
| 4669 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4670 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4671 | * indlen reservation must be shared across the two new extents that are left |
| 4672 | * behind. |
| 4673 | * |
| 4674 | * Given the original reservation and the worst case indlen for the two new |
| 4675 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4676 | * reservation fairly across the two new extents. If necessary, steal available |
| 4677 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4678 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4679 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4680 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4681 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4682 | xfs_bmap_split_indlen( |
| 4683 | xfs_filblks_t ores, /* original res. */ |
| 4684 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4685 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4686 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4687 | { |
| 4688 | xfs_filblks_t len1 = *indlen1; |
| 4689 | xfs_filblks_t len2 = *indlen2; |
| 4690 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4691 | xfs_filblks_t stolen = 0; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4692 | |
| 4693 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4694 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4695 | * indlen for both new extents. |
| 4696 | */ |
| 4697 | while (nres > ores && avail) { |
| 4698 | nres--; |
| 4699 | avail--; |
| 4700 | stolen++; |
| 4701 | } |
| 4702 | |
| 4703 | /* |
| 4704 | * The only blocks available are those reserved for the original |
| 4705 | * extent and what we can steal from the extent being removed. |
| 4706 | * If this still isn't enough to satisfy the combined |
| 4707 | * requirements for the two new extents, skim blocks off of each |
| 4708 | * of the new reservations until they match what is available. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4709 | */ |
| 4710 | while (nres > ores) { |
| 4711 | if (len1) { |
| 4712 | len1--; |
| 4713 | nres--; |
| 4714 | } |
| 4715 | if (nres == ores) |
| 4716 | break; |
| 4717 | if (len2) { |
| 4718 | len2--; |
| 4719 | nres--; |
| 4720 | } |
| 4721 | } |
| 4722 | |
| 4723 | *indlen1 = len1; |
| 4724 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4725 | |
| 4726 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4730 | * Called by xfs_bmapi to update file extent records and the btree |
| 4731 | * after removing space (or undoing a delayed allocation). |
| 4732 | */ |
| 4733 | STATIC int /* error */ |
| 4734 | xfs_bmap_del_extent( |
| 4735 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4736 | xfs_trans_t *tp, /* current transaction pointer */ |
| 4737 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 4738 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4739 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4740 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4741 | int *logflagsp, /* inode logging flags */ |
| 4742 | int whichfork) /* data or attr fork */ |
| 4743 | { |
| 4744 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 4745 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 4746 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4747 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 4748 | int delay; /* current block is delayed allocated */ |
| 4749 | int do_fx; /* free extent at end of routine */ |
| 4750 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
| 4751 | int error; /* error return value */ |
| 4752 | int flags; /* inode logging flags */ |
| 4753 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 4754 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4755 | int i; /* temp state */ |
| 4756 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4757 | xfs_mount_t *mp; /* mount structure */ |
| 4758 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4759 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4760 | /* REFERENCED */ |
| 4761 | uint qfield; /* quota field to update */ |
| 4762 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 4763 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 4764 | int state = 0; |
| 4765 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4766 | mp = ip->i_mount; |
| 4767 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4768 | |
| 4769 | if (whichfork == XFS_ATTR_FORK) |
| 4770 | state |= BMAP_ATTRFORK; |
| 4771 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4772 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4773 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / |
| 4774 | (uint)sizeof(xfs_bmbt_rec_t))); |
| 4775 | ASSERT(del->br_blockcount > 0); |
| 4776 | ep = xfs_iext_get_ext(ifp, *idx); |
| 4777 | xfs_bmbt_get_all(ep, &got); |
| 4778 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4779 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4780 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4781 | ASSERT(got_endoff >= del_endoff); |
| 4782 | delay = isnullstartblock(got.br_startblock); |
| 4783 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
| 4784 | flags = 0; |
| 4785 | qfield = 0; |
| 4786 | error = 0; |
| 4787 | /* |
| 4788 | * If deleting a real allocation, must free up the disk space. |
| 4789 | */ |
| 4790 | if (!delay) { |
| 4791 | flags = XFS_ILOG_CORE; |
| 4792 | /* |
| 4793 | * Realtime allocation. Free it and record di_nblocks update. |
| 4794 | */ |
| 4795 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4796 | xfs_fsblock_t bno; |
| 4797 | xfs_filblks_t len; |
| 4798 | |
| 4799 | ASSERT(do_mod(del->br_blockcount, |
| 4800 | mp->m_sb.sb_rextsize) == 0); |
| 4801 | ASSERT(do_mod(del->br_startblock, |
| 4802 | mp->m_sb.sb_rextsize) == 0); |
| 4803 | bno = del->br_startblock; |
| 4804 | len = del->br_blockcount; |
| 4805 | do_div(bno, mp->m_sb.sb_rextsize); |
| 4806 | do_div(len, mp->m_sb.sb_rextsize); |
| 4807 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4808 | if (error) |
| 4809 | goto done; |
| 4810 | do_fx = 0; |
| 4811 | nblks = len * mp->m_sb.sb_rextsize; |
| 4812 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4813 | } |
| 4814 | /* |
| 4815 | * Ordinary allocation. |
| 4816 | */ |
| 4817 | else { |
| 4818 | do_fx = 1; |
| 4819 | nblks = del->br_blockcount; |
| 4820 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4821 | } |
| 4822 | /* |
| 4823 | * Set up del_endblock and cur for later. |
| 4824 | */ |
| 4825 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4826 | if (cur) { |
| 4827 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 4828 | got.br_startblock, got.br_blockcount, |
| 4829 | &i))) |
| 4830 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4831 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4832 | } |
| 4833 | da_old = da_new = 0; |
| 4834 | } else { |
| 4835 | da_old = startblockval(got.br_startblock); |
| 4836 | da_new = 0; |
| 4837 | nblks = 0; |
| 4838 | do_fx = 0; |
| 4839 | } |
| 4840 | /* |
| 4841 | * Set flag value to use in switch statement. |
| 4842 | * Left-contig is 2, right-contig is 1. |
| 4843 | */ |
| 4844 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 4845 | (got_endoff == del_endoff)) { |
| 4846 | case 3: |
| 4847 | /* |
| 4848 | * Matches the whole extent. Delete the entry. |
| 4849 | */ |
| 4850 | xfs_iext_remove(ip, *idx, 1, |
| 4851 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 4852 | --*idx; |
| 4853 | if (delay) |
| 4854 | break; |
| 4855 | |
| 4856 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4857 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 4858 | flags |= XFS_ILOG_CORE; |
| 4859 | if (!cur) { |
| 4860 | flags |= xfs_ilog_fext(whichfork); |
| 4861 | break; |
| 4862 | } |
| 4863 | if ((error = xfs_btree_delete(cur, &i))) |
| 4864 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4865 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4866 | break; |
| 4867 | |
| 4868 | case 2: |
| 4869 | /* |
| 4870 | * Deleting the first part of the extent. |
| 4871 | */ |
| 4872 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4873 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 4874 | temp = got.br_blockcount - del->br_blockcount; |
| 4875 | xfs_bmbt_set_blockcount(ep, temp); |
| 4876 | if (delay) { |
| 4877 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 4878 | da_old); |
| 4879 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 4880 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4881 | da_new = temp; |
| 4882 | break; |
| 4883 | } |
| 4884 | xfs_bmbt_set_startblock(ep, del_endblock); |
| 4885 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4886 | if (!cur) { |
| 4887 | flags |= xfs_ilog_fext(whichfork); |
| 4888 | break; |
| 4889 | } |
| 4890 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 4891 | got.br_blockcount - del->br_blockcount, |
| 4892 | got.br_state))) |
| 4893 | goto done; |
| 4894 | break; |
| 4895 | |
| 4896 | case 1: |
| 4897 | /* |
| 4898 | * Deleting the last part of the extent. |
| 4899 | */ |
| 4900 | temp = got.br_blockcount - del->br_blockcount; |
| 4901 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4902 | xfs_bmbt_set_blockcount(ep, temp); |
| 4903 | if (delay) { |
| 4904 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 4905 | da_old); |
| 4906 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 4907 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4908 | da_new = temp; |
| 4909 | break; |
| 4910 | } |
| 4911 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4912 | if (!cur) { |
| 4913 | flags |= xfs_ilog_fext(whichfork); |
| 4914 | break; |
| 4915 | } |
| 4916 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 4917 | got.br_startblock, |
| 4918 | got.br_blockcount - del->br_blockcount, |
| 4919 | got.br_state))) |
| 4920 | goto done; |
| 4921 | break; |
| 4922 | |
| 4923 | case 0: |
| 4924 | /* |
| 4925 | * Deleting the middle of the extent. |
| 4926 | */ |
| 4927 | temp = del->br_startoff - got.br_startoff; |
| 4928 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4929 | xfs_bmbt_set_blockcount(ep, temp); |
| 4930 | new.br_startoff = del_endoff; |
| 4931 | temp2 = got_endoff - del_endoff; |
| 4932 | new.br_blockcount = temp2; |
| 4933 | new.br_state = got.br_state; |
| 4934 | if (!delay) { |
| 4935 | new.br_startblock = del_endblock; |
| 4936 | flags |= XFS_ILOG_CORE; |
| 4937 | if (cur) { |
| 4938 | if ((error = xfs_bmbt_update(cur, |
| 4939 | got.br_startoff, |
| 4940 | got.br_startblock, temp, |
| 4941 | got.br_state))) |
| 4942 | goto done; |
| 4943 | if ((error = xfs_btree_increment(cur, 0, &i))) |
| 4944 | goto done; |
| 4945 | cur->bc_rec.b = new; |
| 4946 | error = xfs_btree_insert(cur, &i); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4947 | if (error && error != -ENOSPC) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4948 | goto done; |
| 4949 | /* |
| 4950 | * If get no-space back from btree insert, |
| 4951 | * it tried a split, and we have a zero |
| 4952 | * block reservation. |
| 4953 | * Fix up our state and return the error. |
| 4954 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4955 | if (error == -ENOSPC) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4956 | /* |
| 4957 | * Reset the cursor, don't trust |
| 4958 | * it after any insert operation. |
| 4959 | */ |
| 4960 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 4961 | got.br_startoff, |
| 4962 | got.br_startblock, |
| 4963 | temp, &i))) |
| 4964 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4965 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 4966 | i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4967 | /* |
| 4968 | * Update the btree record back |
| 4969 | * to the original value. |
| 4970 | */ |
| 4971 | if ((error = xfs_bmbt_update(cur, |
| 4972 | got.br_startoff, |
| 4973 | got.br_startblock, |
| 4974 | got.br_blockcount, |
| 4975 | got.br_state))) |
| 4976 | goto done; |
| 4977 | /* |
| 4978 | * Reset the extent record back |
| 4979 | * to the original value. |
| 4980 | */ |
| 4981 | xfs_bmbt_set_blockcount(ep, |
| 4982 | got.br_blockcount); |
| 4983 | flags = 0; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4984 | error = -ENOSPC; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4985 | goto done; |
| 4986 | } |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4987 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4988 | } else |
| 4989 | flags |= xfs_ilog_fext(whichfork); |
| 4990 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4991 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 4992 | } else { |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4993 | xfs_filblks_t stolen; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4994 | ASSERT(whichfork == XFS_DATA_FORK); |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4995 | |
| 4996 | /* |
| 4997 | * Distribute the original indlen reservation across the |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4998 | * two new extents. Steal blocks from the deleted extent |
| 4999 | * if necessary. Stealing blocks simply fudges the |
| 5000 | * fdblocks accounting in xfs_bunmapi(). |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5001 | */ |
| 5002 | temp = xfs_bmap_worst_indlen(ip, got.br_blockcount); |
| 5003 | temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5004 | stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2, |
| 5005 | del->br_blockcount); |
| 5006 | da_new = temp + temp2 - stolen; |
| 5007 | del->br_blockcount -= stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5008 | |
| 5009 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5010 | * Set the reservation for each extent. Warn if either |
| 5011 | * is zero as this can lead to delalloc problems. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5012 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5013 | WARN_ON_ONCE(!temp || !temp2); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5014 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5015 | new.br_startblock = nullstartblock((int)temp2); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5016 | } |
| 5017 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5018 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 5019 | ++*idx; |
| 5020 | break; |
| 5021 | } |
| 5022 | /* |
| 5023 | * If we need to, add to list of extents to delete. |
| 5024 | */ |
| 5025 | if (do_fx) |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5026 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
Darrick J. Wong | 59bad07 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 5027 | del->br_blockcount); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5028 | /* |
| 5029 | * Adjust inode # blocks in the file. |
| 5030 | */ |
| 5031 | if (nblks) |
| 5032 | ip->i_d.di_nblocks -= nblks; |
| 5033 | /* |
| 5034 | * Adjust quota data. |
| 5035 | */ |
| 5036 | if (qfield) |
| 5037 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5038 | |
| 5039 | /* |
| 5040 | * Account for change in delayed indirect blocks. |
| 5041 | * Nothing to do for disk quota accounting here. |
| 5042 | */ |
| 5043 | ASSERT(da_old >= da_new); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 5044 | if (da_old > da_new) |
| 5045 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5046 | done: |
| 5047 | *logflagsp = flags; |
| 5048 | return error; |
| 5049 | } |
| 5050 | |
| 5051 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5052 | * Unmap (remove) blocks from a file. |
| 5053 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5054 | * that value. If not all extents in the block range can be removed then |
| 5055 | * *done is set. |
| 5056 | */ |
| 5057 | int /* error */ |
| 5058 | xfs_bunmapi( |
| 5059 | xfs_trans_t *tp, /* transaction pointer */ |
| 5060 | struct xfs_inode *ip, /* incore inode */ |
| 5061 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5062 | xfs_filblks_t len, /* length to unmap in file */ |
| 5063 | int flags, /* misc flags */ |
| 5064 | xfs_extnum_t nexts, /* number of extents max */ |
| 5065 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5066 | controls a.g. for allocs */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5067 | struct xfs_defer_ops *dfops, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | int *done) /* set if not done yet */ |
| 5069 | { |
| 5070 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5071 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5072 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5073 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | int error; /* error return value */ |
| 5075 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5076 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5078 | int isrt; /* freeing in rt area */ |
| 5079 | xfs_extnum_t lastx; /* last extent index used */ |
| 5080 | int logflags; /* transaction logging flags */ |
| 5081 | xfs_extlen_t mod; /* rt extent offset */ |
| 5082 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5083 | xfs_extnum_t nextents; /* number of file extents */ |
| 5084 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5086 | int tmp_logflags; /* partial logging flags */ |
| 5087 | int wasdel; /* was a delayed alloc extent */ |
| 5088 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | xfs_fsblock_t sum; |
| 5090 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5091 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5094 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5095 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5096 | if (unlikely( |
| 5097 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5098 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5099 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5100 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5101 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | } |
| 5103 | mp = ip->i_mount; |
| 5104 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5105 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5106 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5107 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | ASSERT(len > 0); |
| 5109 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5112 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5113 | return error; |
| 5114 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5115 | if (nextents == 0) { |
| 5116 | *done = 1; |
| 5117 | return 0; |
| 5118 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5119 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5120 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | start = bno; |
| 5122 | bno = start + len - 1; |
| 5123 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5124 | &prev); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | /* |
| 5127 | * Check to see if the given block number is past the end of the |
| 5128 | * file, back up to the last block if so... |
| 5129 | */ |
| 5130 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5131 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | xfs_bmbt_get_all(ep, &got); |
| 5133 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5134 | } |
| 5135 | logflags = 0; |
| 5136 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5137 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5138 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5140 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | cur->bc_private.b.flags = 0; |
| 5142 | } else |
| 5143 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5144 | |
| 5145 | if (isrt) { |
| 5146 | /* |
| 5147 | * Synchronize by locking the bitmap inode. |
| 5148 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5149 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5150 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5151 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5152 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | extno = 0; |
| 5156 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5157 | (nexts == 0 || extno < nexts)) { |
| 5158 | /* |
| 5159 | * Is the found extent after a hole in which bno lives? |
| 5160 | * Just back up to the previous extent, if so. |
| 5161 | */ |
| 5162 | if (got.br_startoff > bno) { |
| 5163 | if (--lastx < 0) |
| 5164 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5165 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | xfs_bmbt_get_all(ep, &got); |
| 5167 | } |
| 5168 | /* |
| 5169 | * Is the last block of this extent before the range |
| 5170 | * we're supposed to delete? If so, we're done. |
| 5171 | */ |
| 5172 | bno = XFS_FILEOFF_MIN(bno, |
| 5173 | got.br_startoff + got.br_blockcount - 1); |
| 5174 | if (bno < start) |
| 5175 | break; |
| 5176 | /* |
| 5177 | * Then deal with the (possibly delayed) allocated space |
| 5178 | * we found. |
| 5179 | */ |
| 5180 | ASSERT(ep != NULL); |
| 5181 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5182 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | if (got.br_startoff < start) { |
| 5184 | del.br_startoff = start; |
| 5185 | del.br_blockcount -= start - got.br_startoff; |
| 5186 | if (!wasdel) |
| 5187 | del.br_startblock += start - got.br_startoff; |
| 5188 | } |
| 5189 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5190 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5191 | sum = del.br_startblock + del.br_blockcount; |
| 5192 | if (isrt && |
| 5193 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5194 | /* |
| 5195 | * Realtime extent not lined up at the end. |
| 5196 | * The extent could have been split into written |
| 5197 | * and unwritten pieces, or we could just be |
| 5198 | * unmapping part of it. But we can't really |
| 5199 | * get rid of part of a realtime extent. |
| 5200 | */ |
| 5201 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5202 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | /* |
| 5204 | * This piece is unwritten, or we're not |
| 5205 | * using unwritten extents. Skip over it. |
| 5206 | */ |
| 5207 | ASSERT(bno >= mod); |
| 5208 | bno -= mod > del.br_blockcount ? |
| 5209 | del.br_blockcount : mod; |
| 5210 | if (bno < got.br_startoff) { |
| 5211 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5212 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5213 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | } |
| 5215 | continue; |
| 5216 | } |
| 5217 | /* |
| 5218 | * It's written, turn it unwritten. |
| 5219 | * This is better than zeroing it. |
| 5220 | */ |
| 5221 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5222 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | /* |
| 5224 | * If this spans a realtime extent boundary, |
| 5225 | * chop it back to the start of the one we end at. |
| 5226 | */ |
| 5227 | if (del.br_blockcount > mod) { |
| 5228 | del.br_startoff += del.br_blockcount - mod; |
| 5229 | del.br_startblock += del.br_blockcount - mod; |
| 5230 | del.br_blockcount = mod; |
| 5231 | } |
| 5232 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5233 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5234 | &lastx, &cur, &del, firstblock, dfops, |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5235 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | if (error) |
| 5237 | goto error0; |
| 5238 | goto nodelete; |
| 5239 | } |
| 5240 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5241 | /* |
| 5242 | * Realtime extent is lined up at the end but not |
| 5243 | * at the front. We'll get rid of full extents if |
| 5244 | * we can. |
| 5245 | */ |
| 5246 | mod = mp->m_sb.sb_rextsize - mod; |
| 5247 | if (del.br_blockcount > mod) { |
| 5248 | del.br_blockcount -= mod; |
| 5249 | del.br_startoff += mod; |
| 5250 | del.br_startblock += mod; |
| 5251 | } else if ((del.br_startoff == start && |
| 5252 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5253 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5254 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | /* |
| 5256 | * Can't make it unwritten. There isn't |
| 5257 | * a full extent here so just skip it. |
| 5258 | */ |
| 5259 | ASSERT(bno >= del.br_blockcount); |
| 5260 | bno -= del.br_blockcount; |
Christoph Hellwig | f1c63b7 | 2011-05-11 15:04:09 +0000 | [diff] [blame] | 5261 | if (got.br_startoff > bno) { |
| 5262 | if (--lastx >= 0) { |
| 5263 | ep = xfs_iext_get_ext(ifp, |
| 5264 | lastx); |
| 5265 | xfs_bmbt_get_all(ep, &got); |
| 5266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | } |
| 5268 | continue; |
| 5269 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5270 | /* |
| 5271 | * This one is already unwritten. |
| 5272 | * It must have a written left neighbor. |
| 5273 | * Unwrite the killed part of that one and |
| 5274 | * try again. |
| 5275 | */ |
| 5276 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5277 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5278 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5280 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | ASSERT(del.br_startblock == |
| 5282 | prev.br_startblock + prev.br_blockcount); |
| 5283 | if (prev.br_startoff < start) { |
| 5284 | mod = start - prev.br_startoff; |
| 5285 | prev.br_blockcount -= mod; |
| 5286 | prev.br_startblock += mod; |
| 5287 | prev.br_startoff = start; |
| 5288 | } |
| 5289 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5290 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5291 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5292 | ip, &lastx, &cur, &prev, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5293 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | if (error) |
| 5295 | goto error0; |
| 5296 | goto nodelete; |
| 5297 | } else { |
| 5298 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5299 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5300 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5301 | ip, &lastx, &cur, &del, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5302 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5303 | if (error) |
| 5304 | goto error0; |
| 5305 | goto nodelete; |
| 5306 | } |
| 5307 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | /* |
| 5310 | * If it's the case where the directory code is running |
| 5311 | * with no block reservation, and the deleted block is in |
| 5312 | * the middle of its extent, and the resulting insert |
| 5313 | * of an extent would cause transformation to btree format, |
| 5314 | * then reject it. The calling code will then swap |
| 5315 | * blocks around instead. |
| 5316 | * We have to do this now, rather than waiting for the |
| 5317 | * conversion to btree format, since the transaction |
| 5318 | * will be dirty. |
| 5319 | */ |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5320 | if (!wasdel && tp->t_blk_res == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5322 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ |
| 5323 | XFS_IFORK_MAXEXT(ip, whichfork) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | del.br_startoff > got.br_startoff && |
| 5325 | del.br_startoff + del.br_blockcount < |
| 5326 | got.br_startoff + got.br_blockcount) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5327 | error = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | goto error0; |
| 5329 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5330 | |
| 5331 | /* |
| 5332 | * Unreserve quota and update realtime free space, if |
| 5333 | * appropriate. If delayed allocation, update the inode delalloc |
| 5334 | * counter now and wait to update the sb counters as |
| 5335 | * xfs_bmap_del_extent() might need to borrow some blocks. |
| 5336 | */ |
| 5337 | if (wasdel) { |
| 5338 | ASSERT(startblockval(del.br_startblock) > 0); |
| 5339 | if (isrt) { |
| 5340 | xfs_filblks_t rtexts; |
| 5341 | |
| 5342 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5343 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5344 | xfs_mod_frextents(mp, (int64_t)rtexts); |
| 5345 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5346 | ip, -((long)del.br_blockcount), 0, |
| 5347 | XFS_QMOPT_RES_RTBLKS); |
| 5348 | } else { |
| 5349 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5350 | ip, -((long)del.br_blockcount), 0, |
| 5351 | XFS_QMOPT_RES_REGBLKS); |
| 5352 | } |
| 5353 | ip->i_delayed_blks -= del.br_blockcount; |
| 5354 | if (cur) |
| 5355 | cur->bc_private.b.flags |= |
| 5356 | XFS_BTCUR_BPRV_WASDEL; |
| 5357 | } else if (cur) |
| 5358 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5359 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5360 | error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5361 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | logflags |= tmp_logflags; |
| 5363 | if (error) |
| 5364 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5365 | |
| 5366 | if (!isrt && wasdel) |
| 5367 | xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false); |
| 5368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | bno = del.br_startoff - 1; |
| 5370 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | /* |
| 5372 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5375 | if (lastx >= 0) { |
| 5376 | ep = xfs_iext_get_ext(ifp, lastx); |
| 5377 | if (xfs_bmbt_get_startoff(ep) > bno) { |
| 5378 | if (--lastx >= 0) |
| 5379 | ep = xfs_iext_get_ext(ifp, |
| 5380 | lastx); |
| 5381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | extno++; |
| 5385 | } |
| 5386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5389 | /* |
| 5390 | * Convert to a btree if necessary. |
| 5391 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5392 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5394 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | &cur, 0, &tmp_logflags, whichfork); |
| 5396 | logflags |= tmp_logflags; |
| 5397 | if (error) |
| 5398 | goto error0; |
| 5399 | } |
| 5400 | /* |
| 5401 | * transform from btree to extents, give it cur |
| 5402 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5403 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | ASSERT(cur != NULL); |
| 5405 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5406 | whichfork); |
| 5407 | logflags |= tmp_logflags; |
| 5408 | if (error) |
| 5409 | goto error0; |
| 5410 | } |
| 5411 | /* |
| 5412 | * transform from extents to local? |
| 5413 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | error = 0; |
| 5415 | error0: |
| 5416 | /* |
| 5417 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5418 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5420 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5422 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5423 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5425 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | /* |
| 5427 | * Log inode even in the error case, if the transaction |
| 5428 | * is dirty we'll need to shut down the filesystem. |
| 5429 | */ |
| 5430 | if (logflags) |
| 5431 | xfs_trans_log_inode(tp, ip, logflags); |
| 5432 | if (cur) { |
| 5433 | if (!error) { |
| 5434 | *firstblock = cur->bc_private.b.firstblock; |
| 5435 | cur->bc_private.b.allocated = 0; |
| 5436 | } |
| 5437 | xfs_btree_del_cursor(cur, |
| 5438 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5439 | } |
| 5440 | return error; |
| 5441 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5442 | |
| 5443 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5444 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5445 | * extent that precedes the target hole of the shift. |
| 5446 | */ |
| 5447 | STATIC bool |
| 5448 | xfs_bmse_can_merge( |
| 5449 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5450 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5451 | xfs_fileoff_t shift) /* shift fsb */ |
| 5452 | { |
| 5453 | xfs_fileoff_t startoff; |
| 5454 | |
| 5455 | startoff = got->br_startoff - shift; |
| 5456 | |
| 5457 | /* |
| 5458 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5459 | * the preceding extent. |
| 5460 | */ |
| 5461 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5462 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5463 | (left->br_state != got->br_state) || |
| 5464 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5465 | return false; |
| 5466 | |
| 5467 | return true; |
| 5468 | } |
| 5469 | |
| 5470 | /* |
| 5471 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5472 | * hole in the file. If an extent shift would result in the extent being fully |
| 5473 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5474 | * the preceding extent rather than do the shift. |
| 5475 | * |
| 5476 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5477 | * with the provided extents via xfs_bmse_can_merge(). |
| 5478 | */ |
| 5479 | STATIC int |
| 5480 | xfs_bmse_merge( |
| 5481 | struct xfs_inode *ip, |
| 5482 | int whichfork, |
| 5483 | xfs_fileoff_t shift, /* shift fsb */ |
| 5484 | int current_ext, /* idx of gotp */ |
| 5485 | struct xfs_bmbt_rec_host *gotp, /* extent to shift */ |
| 5486 | struct xfs_bmbt_rec_host *leftp, /* preceding extent */ |
| 5487 | struct xfs_btree_cur *cur, |
| 5488 | int *logflags) /* output */ |
| 5489 | { |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5490 | struct xfs_bmbt_irec got; |
| 5491 | struct xfs_bmbt_irec left; |
| 5492 | xfs_filblks_t blockcount; |
| 5493 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5494 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5495 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5496 | xfs_bmbt_get_all(gotp, &got); |
| 5497 | xfs_bmbt_get_all(leftp, &left); |
| 5498 | blockcount = left.br_blockcount + got.br_blockcount; |
| 5499 | |
| 5500 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5501 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 5502 | ASSERT(xfs_bmse_can_merge(&left, &got, shift)); |
| 5503 | |
| 5504 | /* |
| 5505 | * Merge the in-core extents. Note that the host record pointers and |
| 5506 | * current_ext index are invalid once the extent has been removed via |
| 5507 | * xfs_iext_remove(). |
| 5508 | */ |
| 5509 | xfs_bmbt_set_blockcount(leftp, blockcount); |
| 5510 | xfs_iext_remove(ip, current_ext, 1, 0); |
| 5511 | |
| 5512 | /* |
| 5513 | * Update the on-disk extent count, the btree if necessary and log the |
| 5514 | * inode. |
| 5515 | */ |
| 5516 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5517 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5518 | *logflags |= XFS_ILOG_CORE; |
| 5519 | if (!cur) { |
| 5520 | *logflags |= XFS_ILOG_DEXT; |
| 5521 | return 0; |
| 5522 | } |
| 5523 | |
| 5524 | /* lookup and remove the extent to merge */ |
| 5525 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5526 | got.br_blockcount, &i); |
| 5527 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5528 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5529 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5530 | |
| 5531 | error = xfs_btree_delete(cur, &i); |
| 5532 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5533 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5534 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5535 | |
| 5536 | /* lookup and update size of the previous extent */ |
| 5537 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock, |
| 5538 | left.br_blockcount, &i); |
| 5539 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5540 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5541 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5542 | |
| 5543 | left.br_blockcount = blockcount; |
| 5544 | |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5545 | return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock, |
| 5546 | left.br_blockcount, left.br_state); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5547 | } |
| 5548 | |
| 5549 | /* |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5550 | * Shift a single extent. |
| 5551 | */ |
| 5552 | STATIC int |
| 5553 | xfs_bmse_shift_one( |
| 5554 | struct xfs_inode *ip, |
| 5555 | int whichfork, |
| 5556 | xfs_fileoff_t offset_shift_fsb, |
| 5557 | int *current_ext, |
| 5558 | struct xfs_bmbt_rec_host *gotp, |
| 5559 | struct xfs_btree_cur *cur, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5560 | int *logflags, |
| 5561 | enum shift_direction direction) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5562 | { |
| 5563 | struct xfs_ifork *ifp; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5564 | struct xfs_mount *mp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5565 | xfs_fileoff_t startoff; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5566 | struct xfs_bmbt_rec_host *adj_irecp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5567 | struct xfs_bmbt_irec got; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5568 | struct xfs_bmbt_irec adj_irec; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5569 | int error; |
| 5570 | int i; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5571 | int total_extents; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5572 | |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5573 | mp = ip->i_mount; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5574 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5575 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5576 | |
| 5577 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5578 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5579 | /* delalloc extents should be prevented by caller */ |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5580 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5581 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5582 | if (direction == SHIFT_LEFT) { |
| 5583 | startoff = got.br_startoff - offset_shift_fsb; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5584 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5585 | /* |
| 5586 | * Check for merge if we've got an extent to the left, |
| 5587 | * otherwise make sure there's enough room at the start |
| 5588 | * of the file for the shift. |
| 5589 | */ |
| 5590 | if (!*current_ext) { |
| 5591 | if (got.br_startoff < offset_shift_fsb) |
| 5592 | return -EINVAL; |
| 5593 | goto update_current_ext; |
| 5594 | } |
| 5595 | /* |
| 5596 | * grab the left extent and check for a large |
| 5597 | * enough hole. |
| 5598 | */ |
| 5599 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1); |
| 5600 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5601 | |
| 5602 | if (startoff < |
| 5603 | adj_irec.br_startoff + adj_irec.br_blockcount) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5604 | return -EINVAL; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5605 | |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5606 | /* check whether to merge the extent or shift it down */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5607 | if (xfs_bmse_can_merge(&adj_irec, &got, |
| 5608 | offset_shift_fsb)) { |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5609 | return xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5610 | *current_ext, gotp, adj_irecp, |
| 5611 | cur, logflags); |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5612 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5613 | } else { |
| 5614 | startoff = got.br_startoff + offset_shift_fsb; |
| 5615 | /* nothing to move if this is the last extent */ |
| 5616 | if (*current_ext >= (total_extents - 1)) |
| 5617 | goto update_current_ext; |
| 5618 | /* |
| 5619 | * If this is not the last extent in the file, make sure there |
| 5620 | * is enough room between current extent and next extent for |
| 5621 | * accommodating the shift. |
| 5622 | */ |
| 5623 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1); |
| 5624 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5625 | if (startoff + got.br_blockcount > adj_irec.br_startoff) |
| 5626 | return -EINVAL; |
| 5627 | /* |
| 5628 | * Unlike a left shift (which involves a hole punch), |
| 5629 | * a right shift does not modify extent neighbors |
| 5630 | * in any way. We should never find mergeable extents |
| 5631 | * in this scenario. Check anyways and warn if we |
| 5632 | * encounter two extents that could be one. |
| 5633 | */ |
| 5634 | if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb)) |
| 5635 | WARN_ON_ONCE(1); |
| 5636 | } |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5637 | /* |
| 5638 | * Increment the extent index for the next iteration, update the start |
| 5639 | * offset of the in-core extent and update the btree if applicable. |
| 5640 | */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5641 | update_current_ext: |
| 5642 | if (direction == SHIFT_LEFT) |
| 5643 | (*current_ext)++; |
| 5644 | else |
| 5645 | (*current_ext)--; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5646 | xfs_bmbt_set_startoff(gotp, startoff); |
| 5647 | *logflags |= XFS_ILOG_CORE; |
| 5648 | if (!cur) { |
| 5649 | *logflags |= XFS_ILOG_DEXT; |
| 5650 | return 0; |
| 5651 | } |
| 5652 | |
| 5653 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5654 | got.br_blockcount, &i); |
| 5655 | if (error) |
| 5656 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5657 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5658 | |
| 5659 | got.br_startoff = startoff; |
kbuild test robot | d254aae | 2014-12-01 08:42:52 +1100 | [diff] [blame] | 5660 | return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5661 | got.br_blockcount, got.br_state); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5662 | } |
| 5663 | |
| 5664 | /* |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5665 | * Shift extent records to the left/right to cover/create a hole. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5666 | * |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5667 | * 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] | 5668 | * @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] | 5669 | * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb |
| 5670 | * is the length by which each extent is shifted. If there is no hole to shift |
| 5671 | * the extents into, this will be considered invalid operation and we abort |
| 5672 | * immediately. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5673 | */ |
| 5674 | int |
| 5675 | xfs_bmap_shift_extents( |
| 5676 | struct xfs_trans *tp, |
| 5677 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5678 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5679 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5680 | int *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5681 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5682 | xfs_fsblock_t *firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5683 | struct xfs_defer_ops *dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5684 | enum shift_direction direction, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5685 | int num_exts) |
| 5686 | { |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5687 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5688 | struct xfs_bmbt_rec_host *gotp; |
| 5689 | struct xfs_bmbt_irec got; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5690 | struct xfs_mount *mp = ip->i_mount; |
| 5691 | struct xfs_ifork *ifp; |
| 5692 | xfs_extnum_t nexts = 0; |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5693 | xfs_extnum_t current_ext; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5694 | xfs_extnum_t total_extents; |
| 5695 | xfs_extnum_t stop_extent; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5696 | int error = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5697 | int whichfork = XFS_DATA_FORK; |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5698 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5699 | |
| 5700 | if (unlikely(XFS_TEST_ERROR( |
| 5701 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5702 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5703 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 5704 | XFS_ERROR_REPORT("xfs_bmap_shift_extents", |
| 5705 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5706 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5707 | } |
| 5708 | |
| 5709 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5710 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5711 | |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5712 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5713 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5714 | ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT); |
| 5715 | ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5716 | |
| 5717 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5718 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5719 | /* Read in all the extents */ |
| 5720 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5721 | if (error) |
| 5722 | return error; |
| 5723 | } |
| 5724 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5725 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5726 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5727 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5728 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5729 | cur->bc_private.b.flags = 0; |
| 5730 | } |
| 5731 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5732 | /* |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 5733 | * 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] | 5734 | * are collapsing out, so we cannot use the count of real extents here. |
| 5735 | * Instead we have to calculate it from the incore fork. |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 5736 | */ |
| 5737 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5738 | if (total_extents == 0) { |
| 5739 | *done = 1; |
| 5740 | goto del_cursor; |
| 5741 | } |
| 5742 | |
| 5743 | /* |
| 5744 | * In case of first right shift, we need to initialize next_fsb |
| 5745 | */ |
| 5746 | if (*next_fsb == NULLFSBLOCK) { |
| 5747 | gotp = xfs_iext_get_ext(ifp, total_extents - 1); |
| 5748 | xfs_bmbt_get_all(gotp, &got); |
| 5749 | *next_fsb = got.br_startoff; |
| 5750 | if (stop_fsb > *next_fsb) { |
| 5751 | *done = 1; |
| 5752 | goto del_cursor; |
| 5753 | } |
| 5754 | } |
| 5755 | |
| 5756 | /* Lookup the extent index at which we have to stop */ |
| 5757 | if (direction == SHIFT_RIGHT) { |
| 5758 | gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent); |
| 5759 | /* Make stop_extent exclusive of shift range */ |
| 5760 | stop_extent--; |
| 5761 | } else |
| 5762 | stop_extent = total_extents; |
| 5763 | |
| 5764 | /* |
| 5765 | * Look up the extent index for the fsb where we start shifting. We can |
| 5766 | * henceforth iterate with current_ext as extent list changes are locked |
| 5767 | * out via ilock. |
| 5768 | * |
| 5769 | * gotp can be null in 2 cases: 1) if there are no extents or 2) |
| 5770 | * *next_fsb lies in a hole beyond which there are no extents. Either |
| 5771 | * way, we are done. |
| 5772 | */ |
| 5773 | gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext); |
| 5774 | if (!gotp) { |
| 5775 | *done = 1; |
| 5776 | goto del_cursor; |
| 5777 | } |
| 5778 | |
| 5779 | /* some sanity checking before we finally start shifting extents */ |
| 5780 | if ((direction == SHIFT_LEFT && current_ext >= stop_extent) || |
| 5781 | (direction == SHIFT_RIGHT && current_ext <= stop_extent)) { |
| 5782 | error = -EIO; |
| 5783 | goto del_cursor; |
| 5784 | } |
| 5785 | |
| 5786 | while (nexts++ < num_exts) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5787 | error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5788 | ¤t_ext, gotp, cur, &logflags, |
| 5789 | direction); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5790 | if (error) |
| 5791 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5792 | /* |
| 5793 | * If there was an extent merge during the shift, the extent |
| 5794 | * count can change. Update the total and grade the next record. |
| 5795 | */ |
| 5796 | if (direction == SHIFT_LEFT) { |
| 5797 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
| 5798 | stop_extent = total_extents; |
| 5799 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5800 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5801 | if (current_ext == stop_extent) { |
| 5802 | *done = 1; |
| 5803 | *next_fsb = NULLFSBLOCK; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5804 | break; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5805 | } |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5806 | gotp = xfs_iext_get_ext(ifp, current_ext); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5807 | } |
| 5808 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5809 | if (!*done) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5810 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5811 | *next_fsb = got.br_startoff; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5812 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5813 | |
| 5814 | del_cursor: |
| 5815 | if (cur) |
| 5816 | xfs_btree_del_cursor(cur, |
| 5817 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5818 | |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5819 | if (logflags) |
| 5820 | xfs_trans_log_inode(tp, ip, logflags); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5821 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5822 | return error; |
| 5823 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5824 | |
| 5825 | /* |
| 5826 | * Splits an extent into two extents at split_fsb block such that it is |
| 5827 | * the first block of the current_ext. @current_ext is a target extent |
| 5828 | * to be split. @split_fsb is a block where the extents is split. |
| 5829 | * If split_fsb lies in a hole or the first block of extents, just return 0. |
| 5830 | */ |
| 5831 | STATIC int |
| 5832 | xfs_bmap_split_extent_at( |
| 5833 | struct xfs_trans *tp, |
| 5834 | struct xfs_inode *ip, |
| 5835 | xfs_fileoff_t split_fsb, |
| 5836 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5837 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5838 | { |
| 5839 | int whichfork = XFS_DATA_FORK; |
| 5840 | struct xfs_btree_cur *cur = NULL; |
| 5841 | struct xfs_bmbt_rec_host *gotp; |
| 5842 | struct xfs_bmbt_irec got; |
| 5843 | struct xfs_bmbt_irec new; /* split extent */ |
| 5844 | struct xfs_mount *mp = ip->i_mount; |
| 5845 | struct xfs_ifork *ifp; |
| 5846 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
| 5847 | xfs_extnum_t current_ext; |
| 5848 | int error = 0; |
| 5849 | int logflags = 0; |
| 5850 | int i = 0; |
| 5851 | |
| 5852 | if (unlikely(XFS_TEST_ERROR( |
| 5853 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5854 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5855 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 5856 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5857 | XFS_ERRLEVEL_LOW, mp); |
| 5858 | return -EFSCORRUPTED; |
| 5859 | } |
| 5860 | |
| 5861 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5862 | return -EIO; |
| 5863 | |
| 5864 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5865 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5866 | /* Read in all the extents */ |
| 5867 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5868 | if (error) |
| 5869 | return error; |
| 5870 | } |
| 5871 | |
| 5872 | /* |
| 5873 | * gotp can be null in 2 cases: 1) if there are no extents |
| 5874 | * or 2) split_fsb lies in a hole beyond which there are |
| 5875 | * no extents. Either way, we are done. |
| 5876 | */ |
| 5877 | gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext); |
| 5878 | if (!gotp) |
| 5879 | return 0; |
| 5880 | |
| 5881 | xfs_bmbt_get_all(gotp, &got); |
| 5882 | |
| 5883 | /* |
| 5884 | * Check split_fsb lies in a hole or the start boundary offset |
| 5885 | * of the extent. |
| 5886 | */ |
| 5887 | if (got.br_startoff >= split_fsb) |
| 5888 | return 0; |
| 5889 | |
| 5890 | gotblkcnt = split_fsb - got.br_startoff; |
| 5891 | new.br_startoff = split_fsb; |
| 5892 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5893 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5894 | new.br_state = got.br_state; |
| 5895 | |
| 5896 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5897 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5898 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5899 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5900 | cur->bc_private.b.flags = 0; |
| 5901 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 5902 | got.br_startblock, |
| 5903 | got.br_blockcount, |
| 5904 | &i); |
| 5905 | if (error) |
| 5906 | goto del_cursor; |
| 5907 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5908 | } |
| 5909 | |
| 5910 | xfs_bmbt_set_blockcount(gotp, gotblkcnt); |
| 5911 | got.br_blockcount = gotblkcnt; |
| 5912 | |
| 5913 | logflags = XFS_ILOG_CORE; |
| 5914 | if (cur) { |
| 5915 | error = xfs_bmbt_update(cur, got.br_startoff, |
| 5916 | got.br_startblock, |
| 5917 | got.br_blockcount, |
| 5918 | got.br_state); |
| 5919 | if (error) |
| 5920 | goto del_cursor; |
| 5921 | } else |
| 5922 | logflags |= XFS_ILOG_DEXT; |
| 5923 | |
| 5924 | /* Add new extent */ |
| 5925 | current_ext++; |
| 5926 | xfs_iext_insert(ip, current_ext, 1, &new, 0); |
| 5927 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5928 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5929 | |
| 5930 | if (cur) { |
| 5931 | error = xfs_bmbt_lookup_eq(cur, new.br_startoff, |
| 5932 | new.br_startblock, new.br_blockcount, |
| 5933 | &i); |
| 5934 | if (error) |
| 5935 | goto del_cursor; |
| 5936 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
| 5937 | cur->bc_rec.b.br_state = new.br_state; |
| 5938 | |
| 5939 | error = xfs_btree_insert(cur, &i); |
| 5940 | if (error) |
| 5941 | goto del_cursor; |
| 5942 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5943 | } |
| 5944 | |
| 5945 | /* |
| 5946 | * Convert to a btree if necessary. |
| 5947 | */ |
| 5948 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 5949 | int tmp_logflags; /* partial log flag return val */ |
| 5950 | |
| 5951 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5952 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5953 | &cur, 0, &tmp_logflags, whichfork); |
| 5954 | logflags |= tmp_logflags; |
| 5955 | } |
| 5956 | |
| 5957 | del_cursor: |
| 5958 | if (cur) { |
| 5959 | cur->bc_private.b.allocated = 0; |
| 5960 | xfs_btree_del_cursor(cur, |
| 5961 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5962 | } |
| 5963 | |
| 5964 | if (logflags) |
| 5965 | xfs_trans_log_inode(tp, ip, logflags); |
| 5966 | return error; |
| 5967 | } |
| 5968 | |
| 5969 | int |
| 5970 | xfs_bmap_split_extent( |
| 5971 | struct xfs_inode *ip, |
| 5972 | xfs_fileoff_t split_fsb) |
| 5973 | { |
| 5974 | struct xfs_mount *mp = ip->i_mount; |
| 5975 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5976 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5977 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5978 | int error; |
| 5979 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 5980 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 5981 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 5982 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5983 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5984 | |
| 5985 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 5986 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 5987 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5988 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5989 | |
| 5990 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5991 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5992 | if (error) |
| 5993 | goto out; |
| 5994 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 5995 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5996 | if (error) |
| 5997 | goto out; |
| 5998 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 5999 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6000 | |
| 6001 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame^] | 6002 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6003 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6004 | return error; |
| 6005 | } |