Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
| 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_sb.h" |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 27 | #include "xfs_defer.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 28 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_da_btree.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 30 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_btree.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 33 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_extfree_item.h" |
| 36 | #include "xfs_alloc.h" |
| 37 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 38 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 39 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_rtalloc.h" |
| 41 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_quota.h" |
| 43 | #include "xfs_trans_space.h" |
| 44 | #include "xfs_buf_item.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 45 | #include "xfs_trace.h" |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 46 | #include "xfs_symlink.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 47 | #include "xfs_attr_leaf.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 48 | #include "xfs_filestream.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 49 | #include "xfs_rmap.h" |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 50 | #include "xfs_ag_resv.h" |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 51 | #include "xfs_refcount.h" |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 52 | #include "xfs_rmap_btree.h" |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 53 | #include "xfs_icache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 57 | |
| 58 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 59 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 63 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 64 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 66 | void |
| 67 | xfs_bmap_compute_maxlevels( |
| 68 | xfs_mount_t *mp, /* file system mount structure */ |
| 69 | int whichfork) /* data or attr fork */ |
| 70 | { |
| 71 | int level; /* btree level */ |
| 72 | uint maxblocks; /* max blocks at this level */ |
| 73 | uint maxleafents; /* max leaf entries possible */ |
| 74 | int maxrootrecs; /* max records in root block */ |
| 75 | int minleafrecs; /* min records in leaf block */ |
| 76 | int minnoderecs; /* min records in node block */ |
| 77 | int sz; /* root block size */ |
| 78 | |
| 79 | /* |
| 80 | * The maximum number of extents in a file, hence the maximum |
| 81 | * number of leaf entries, is controlled by the type of di_nextents |
| 82 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 83 | * (a signed 16-bit number, xfs_aextnum_t). |
| 84 | * |
| 85 | * Note that we can no longer assume that if we are in ATTR1 that |
| 86 | * the fork offset of all the inodes will be |
| 87 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 88 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 89 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 90 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 91 | * of a minimum size available. |
| 92 | */ |
| 93 | if (whichfork == XFS_DATA_FORK) { |
| 94 | maxleafents = MAXEXTNUM; |
| 95 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 96 | } else { |
| 97 | maxleafents = MAXAEXTNUM; |
| 98 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 99 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 100 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 101 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 102 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 103 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 104 | for (level = 1; maxblocks > 1; level++) { |
| 105 | if (maxblocks <= maxrootrecs) |
| 106 | maxblocks = 1; |
| 107 | else |
| 108 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 109 | } |
| 110 | mp->m_bm_maxlevels[whichfork] = level; |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 113 | STATIC int /* error */ |
| 114 | xfs_bmbt_lookup_eq( |
| 115 | struct xfs_btree_cur *cur, |
| 116 | xfs_fileoff_t off, |
| 117 | xfs_fsblock_t bno, |
| 118 | xfs_filblks_t len, |
| 119 | int *stat) /* success/failure */ |
| 120 | { |
| 121 | cur->bc_rec.b.br_startoff = off; |
| 122 | cur->bc_rec.b.br_startblock = bno; |
| 123 | cur->bc_rec.b.br_blockcount = len; |
| 124 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 125 | } |
| 126 | |
| 127 | STATIC int /* error */ |
| 128 | xfs_bmbt_lookup_ge( |
| 129 | struct xfs_btree_cur *cur, |
| 130 | xfs_fileoff_t off, |
| 131 | xfs_fsblock_t bno, |
| 132 | xfs_filblks_t len, |
| 133 | int *stat) /* success/failure */ |
| 134 | { |
| 135 | cur->bc_rec.b.br_startoff = off; |
| 136 | cur->bc_rec.b.br_startblock = bno; |
| 137 | cur->bc_rec.b.br_blockcount = len; |
| 138 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 139 | } |
| 140 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 141 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 142 | * Check if the inode needs to be converted to btree format. |
| 143 | */ |
| 144 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 145 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 146 | return whichfork != XFS_COW_FORK && |
| 147 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 148 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 149 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Check if the inode should be converted to extent format. |
| 154 | */ |
| 155 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 156 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 157 | return whichfork != XFS_COW_FORK && |
| 158 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 159 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 160 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Update the record referred to by cur to the value given |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 165 | * by [off, bno, len, state]. |
| 166 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 167 | */ |
| 168 | STATIC int |
| 169 | xfs_bmbt_update( |
| 170 | struct xfs_btree_cur *cur, |
| 171 | xfs_fileoff_t off, |
| 172 | xfs_fsblock_t bno, |
| 173 | xfs_filblks_t len, |
| 174 | xfs_exntst_t state) |
| 175 | { |
| 176 | union xfs_btree_rec rec; |
| 177 | |
| 178 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 179 | return xfs_btree_update(cur, &rec); |
| 180 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 183 | * Compute the worst-case number of indirect blocks that will be used |
| 184 | * for ip's delayed extent of length "len". |
| 185 | */ |
| 186 | STATIC xfs_filblks_t |
| 187 | xfs_bmap_worst_indlen( |
| 188 | xfs_inode_t *ip, /* incore inode pointer */ |
| 189 | xfs_filblks_t len) /* delayed extent length */ |
| 190 | { |
| 191 | int level; /* btree level number */ |
| 192 | int maxrecs; /* maximum record count at this level */ |
| 193 | xfs_mount_t *mp; /* mount structure */ |
| 194 | xfs_filblks_t rval; /* return value */ |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 195 | xfs_filblks_t orig_len; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 196 | |
| 197 | mp = ip->i_mount; |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 198 | |
| 199 | /* Calculate the worst-case size of the bmbt. */ |
| 200 | orig_len = len; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 201 | maxrecs = mp->m_bmap_dmxr[0]; |
| 202 | for (level = 0, rval = 0; |
| 203 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 204 | level++) { |
| 205 | len += maxrecs - 1; |
| 206 | do_div(len, maxrecs); |
| 207 | rval += len; |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 208 | if (len == 1) { |
| 209 | rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 210 | level - 1; |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 211 | break; |
| 212 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 213 | if (level == 0) |
| 214 | maxrecs = mp->m_bmap_dmxr[1]; |
| 215 | } |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 216 | |
| 217 | /* Calculate the worst-case size of the rmapbt. */ |
| 218 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 219 | rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) + |
| 220 | mp->m_rmap_maxlevels; |
| 221 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 222 | return rval; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Calculate the default attribute fork offset for newly created inodes. |
| 227 | */ |
| 228 | uint |
| 229 | xfs_default_attroffset( |
| 230 | struct xfs_inode *ip) |
| 231 | { |
| 232 | struct xfs_mount *mp = ip->i_mount; |
| 233 | uint offset; |
| 234 | |
| 235 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 236 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 237 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 238 | } else { |
| 239 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 240 | } |
| 241 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 242 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 243 | return offset; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Helper routine to reset inode di_forkoff field when switching |
| 248 | * attribute fork from local to extent format - we reset it where |
| 249 | * possible to make space available for inline data fork extents. |
| 250 | */ |
| 251 | STATIC void |
| 252 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 253 | xfs_inode_t *ip, |
| 254 | int whichfork) |
| 255 | { |
| 256 | if (whichfork == XFS_ATTR_FORK && |
| 257 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 258 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 259 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 260 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 261 | |
| 262 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 263 | ip->i_d.di_forkoff = dfl_forkoff; |
| 264 | } |
| 265 | } |
| 266 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 267 | #ifdef DEBUG |
| 268 | STATIC struct xfs_buf * |
| 269 | xfs_bmap_get_bp( |
| 270 | struct xfs_btree_cur *cur, |
| 271 | xfs_fsblock_t bno) |
| 272 | { |
| 273 | struct xfs_log_item_desc *lidp; |
| 274 | int i; |
| 275 | |
| 276 | if (!cur) |
| 277 | return NULL; |
| 278 | |
| 279 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 280 | if (!cur->bc_bufs[i]) |
| 281 | break; |
| 282 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 283 | return cur->bc_bufs[i]; |
| 284 | } |
| 285 | |
| 286 | /* Chase down all the log items to see if the bp is there */ |
| 287 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 288 | struct xfs_buf_log_item *bip; |
| 289 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 290 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 291 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 292 | return bip->bli_buf; |
| 293 | } |
| 294 | |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | STATIC void |
| 299 | xfs_check_block( |
| 300 | struct xfs_btree_block *block, |
| 301 | xfs_mount_t *mp, |
| 302 | int root, |
| 303 | short sz) |
| 304 | { |
| 305 | int i, j, dmxr; |
| 306 | __be64 *pp, *thispa; /* pointer to block address */ |
| 307 | xfs_bmbt_key_t *prevp, *keyp; |
| 308 | |
| 309 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 310 | |
| 311 | prevp = NULL; |
| 312 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 313 | dmxr = mp->m_bmap_dmxr[0]; |
| 314 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 315 | |
| 316 | if (prevp) { |
| 317 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 318 | be64_to_cpu(keyp->br_startoff)); |
| 319 | } |
| 320 | prevp = keyp; |
| 321 | |
| 322 | /* |
| 323 | * Compare the block numbers to see if there are dups. |
| 324 | */ |
| 325 | if (root) |
| 326 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 327 | else |
| 328 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 329 | |
| 330 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 331 | if (root) |
| 332 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 333 | else |
| 334 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 335 | if (*thispa == *pp) { |
| 336 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 337 | __func__, j, i, |
| 338 | (unsigned long long)be64_to_cpu(*thispa)); |
| 339 | panic("%s: ptrs are equal in node\n", |
| 340 | __func__); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * 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] | 348 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 349 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 350 | * them. The btree record ordering checks will still be done, so for such large |
| 351 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 352 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 353 | STATIC void |
| 354 | xfs_bmap_check_leaf_extents( |
| 355 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 356 | xfs_inode_t *ip, /* incore inode pointer */ |
| 357 | int whichfork) /* data or attr fork */ |
| 358 | { |
| 359 | struct xfs_btree_block *block; /* current btree block */ |
| 360 | xfs_fsblock_t bno; /* block # of "block" */ |
| 361 | xfs_buf_t *bp; /* buffer for "block" */ |
| 362 | int error; /* error return value */ |
| 363 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 364 | xfs_ifork_t *ifp; /* fork structure */ |
| 365 | int level; /* btree level, for checking */ |
| 366 | xfs_mount_t *mp; /* file system mount structure */ |
| 367 | __be64 *pp; /* pointer to block address */ |
| 368 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 369 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 370 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 371 | int bp_release = 0; |
| 372 | |
| 373 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 374 | return; |
| 375 | } |
| 376 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 377 | /* skip large extent count inodes */ |
| 378 | if (ip->i_d.di_nextents > 10000) |
| 379 | return; |
| 380 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 381 | bno = NULLFSBLOCK; |
| 382 | mp = ip->i_mount; |
| 383 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 384 | block = ifp->if_broot; |
| 385 | /* |
| 386 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 387 | */ |
| 388 | level = be16_to_cpu(block->bb_level); |
| 389 | ASSERT(level > 0); |
| 390 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 391 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 392 | bno = be64_to_cpu(*pp); |
| 393 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 394 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 395 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 396 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 397 | |
| 398 | /* |
| 399 | * Go down the tree until leaf level is reached, following the first |
| 400 | * pointer (leftmost) at each level. |
| 401 | */ |
| 402 | while (level-- > 0) { |
| 403 | /* See if buf is in cur first */ |
| 404 | bp_release = 0; |
| 405 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 406 | if (!bp) { |
| 407 | bp_release = 1; |
| 408 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 409 | XFS_BMAP_BTREE_REF, |
| 410 | &xfs_bmbt_buf_ops); |
| 411 | if (error) |
| 412 | goto error_norelse; |
| 413 | } |
| 414 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 415 | if (level == 0) |
| 416 | break; |
| 417 | |
| 418 | /* |
| 419 | * Check this block for basic sanity (increasing keys and |
| 420 | * no duplicate blocks). |
| 421 | */ |
| 422 | |
| 423 | xfs_check_block(block, mp, 0, 0); |
| 424 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 425 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 426 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 427 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 428 | if (bp_release) { |
| 429 | bp_release = 0; |
| 430 | xfs_trans_brelse(NULL, bp); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 436 | */ |
| 437 | i = 0; |
| 438 | |
| 439 | /* |
| 440 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 441 | */ |
| 442 | for (;;) { |
| 443 | xfs_fsblock_t nextbno; |
| 444 | xfs_extnum_t num_recs; |
| 445 | |
| 446 | |
| 447 | num_recs = xfs_btree_get_numrecs(block); |
| 448 | |
| 449 | /* |
| 450 | * Read-ahead the next leaf block, if any. |
| 451 | */ |
| 452 | |
| 453 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 454 | |
| 455 | /* |
| 456 | * Check all the extents to make sure they are OK. |
| 457 | * If we had a previous block, the last entry should |
| 458 | * conform with the first entry in this one. |
| 459 | */ |
| 460 | |
| 461 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 462 | if (i) { |
| 463 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 464 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 465 | xfs_bmbt_disk_get_startoff(ep)); |
| 466 | } |
| 467 | for (j = 1; j < num_recs; j++) { |
| 468 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 469 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 470 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 471 | xfs_bmbt_disk_get_startoff(nextp)); |
| 472 | ep = nextp; |
| 473 | } |
| 474 | |
| 475 | last = *ep; |
| 476 | i += num_recs; |
| 477 | if (bp_release) { |
| 478 | bp_release = 0; |
| 479 | xfs_trans_brelse(NULL, bp); |
| 480 | } |
| 481 | bno = nextbno; |
| 482 | /* |
| 483 | * If we've reached the end, stop. |
| 484 | */ |
| 485 | if (bno == NULLFSBLOCK) |
| 486 | break; |
| 487 | |
| 488 | bp_release = 0; |
| 489 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 490 | if (!bp) { |
| 491 | bp_release = 1; |
| 492 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 493 | XFS_BMAP_BTREE_REF, |
| 494 | &xfs_bmbt_buf_ops); |
| 495 | if (error) |
| 496 | goto error_norelse; |
| 497 | } |
| 498 | block = XFS_BUF_TO_BLOCK(bp); |
| 499 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 500 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 501 | return; |
| 502 | |
| 503 | error0: |
| 504 | xfs_warn(mp, "%s: at error0", __func__); |
| 505 | if (bp_release) |
| 506 | xfs_trans_brelse(NULL, bp); |
| 507 | error_norelse: |
| 508 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 509 | __func__, i); |
| 510 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 511 | return; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Add bmap trace insert entries for all the contents of the extent records. |
| 516 | */ |
| 517 | void |
| 518 | xfs_bmap_trace_exlist( |
| 519 | xfs_inode_t *ip, /* incore inode pointer */ |
| 520 | xfs_extnum_t cnt, /* count of entries in the list */ |
Eric Sandeen | c44a1f2 | 2016-12-05 12:32:00 +1100 | [diff] [blame] | 521 | int whichfork, /* data or attr or cow fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 522 | unsigned long caller_ip) |
| 523 | { |
| 524 | xfs_extnum_t idx; /* extent record index */ |
| 525 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 526 | int state = 0; |
| 527 | |
| 528 | if (whichfork == XFS_ATTR_FORK) |
| 529 | state |= BMAP_ATTRFORK; |
Eric Sandeen | c44a1f2 | 2016-12-05 12:32:00 +1100 | [diff] [blame] | 530 | else if (whichfork == XFS_COW_FORK) |
| 531 | state |= BMAP_COWFORK; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 532 | |
| 533 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 534 | ASSERT(cnt == xfs_iext_count(ifp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 535 | for (idx = 0; idx < cnt; idx++) |
Eric Sandeen | 7710517 | 2016-12-05 12:31:50 +1100 | [diff] [blame] | 536 | trace_xfs_extlist(ip, idx, state, caller_ip); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 541 | * given the caller's original parameters. Specifically check the |
| 542 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 543 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 544 | */ |
| 545 | STATIC void |
| 546 | xfs_bmap_validate_ret( |
| 547 | xfs_fileoff_t bno, |
| 548 | xfs_filblks_t len, |
| 549 | int flags, |
| 550 | xfs_bmbt_irec_t *mval, |
| 551 | int nmap, |
| 552 | int ret_nmap) |
| 553 | { |
| 554 | int i; /* index to map values */ |
| 555 | |
| 556 | ASSERT(ret_nmap <= nmap); |
| 557 | |
| 558 | for (i = 0; i < ret_nmap; i++) { |
| 559 | ASSERT(mval[i].br_blockcount > 0); |
| 560 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 561 | ASSERT(mval[i].br_startoff >= bno); |
| 562 | ASSERT(mval[i].br_blockcount <= len); |
| 563 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 564 | bno + len); |
| 565 | } else { |
| 566 | ASSERT(mval[i].br_startoff < bno + len); |
| 567 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 568 | bno); |
| 569 | } |
| 570 | ASSERT(i == 0 || |
| 571 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 572 | mval[i].br_startoff); |
| 573 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 574 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 575 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 576 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | #else |
| 581 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
| 582 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 583 | #endif /* DEBUG */ |
| 584 | |
| 585 | /* |
| 586 | * bmap free list manipulation functions |
| 587 | */ |
| 588 | |
| 589 | /* |
| 590 | * Add the extent to the list of extents to be free at transaction end. |
| 591 | * The list is maintained sorted (by block number). |
| 592 | */ |
| 593 | void |
| 594 | xfs_bmap_add_free( |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 595 | struct xfs_mount *mp, |
| 596 | struct xfs_defer_ops *dfops, |
| 597 | xfs_fsblock_t bno, |
| 598 | xfs_filblks_t len, |
| 599 | struct xfs_owner_info *oinfo) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 600 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 601 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 602 | #ifdef DEBUG |
| 603 | xfs_agnumber_t agno; |
| 604 | xfs_agblock_t agbno; |
| 605 | |
| 606 | ASSERT(bno != NULLFSBLOCK); |
| 607 | ASSERT(len > 0); |
| 608 | ASSERT(len <= MAXEXTLEN); |
| 609 | ASSERT(!isnullstartblock(bno)); |
| 610 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 611 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 612 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 613 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 614 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 615 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 616 | #endif |
| 617 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 618 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 619 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 620 | new->xefi_startblock = bno; |
| 621 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 622 | if (oinfo) |
| 623 | new->xefi_oinfo = *oinfo; |
| 624 | else |
| 625 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 626 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
| 627 | XFS_FSB_TO_AGBNO(mp, bno), len); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 628 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Inode fork format manipulation functions |
| 633 | */ |
| 634 | |
| 635 | /* |
| 636 | * Transform a btree format file with only one leaf node, where the |
| 637 | * extents list will fit in the inode, into an extents format file. |
| 638 | * Since the file extents are already in-core, all we have to do is |
| 639 | * give up the space for the btree root and pitch the leaf block. |
| 640 | */ |
| 641 | STATIC int /* error */ |
| 642 | xfs_bmap_btree_to_extents( |
| 643 | xfs_trans_t *tp, /* transaction pointer */ |
| 644 | xfs_inode_t *ip, /* incore inode pointer */ |
| 645 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 646 | int *logflagsp, /* inode logging flags */ |
| 647 | int whichfork) /* data or attr fork */ |
| 648 | { |
| 649 | /* REFERENCED */ |
| 650 | struct xfs_btree_block *cblock;/* child btree block */ |
| 651 | xfs_fsblock_t cbno; /* child block number */ |
| 652 | xfs_buf_t *cbp; /* child block's buffer */ |
| 653 | int error; /* error return value */ |
| 654 | xfs_ifork_t *ifp; /* inode fork data */ |
| 655 | xfs_mount_t *mp; /* mount point structure */ |
| 656 | __be64 *pp; /* ptr to block address */ |
| 657 | struct xfs_btree_block *rblock;/* root btree block */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 658 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 659 | |
| 660 | mp = ip->i_mount; |
| 661 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 662 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 663 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 664 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 665 | rblock = ifp->if_broot; |
| 666 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 667 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 668 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 669 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 670 | cbno = be64_to_cpu(*pp); |
| 671 | *logflagsp = 0; |
| 672 | #ifdef DEBUG |
| 673 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
| 674 | return error; |
| 675 | #endif |
| 676 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 677 | &xfs_bmbt_buf_ops); |
| 678 | if (error) |
| 679 | return error; |
| 680 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 681 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 682 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 683 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
| 684 | xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 685 | ip->i_d.di_nblocks--; |
| 686 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 687 | xfs_trans_binval(tp, cbp); |
| 688 | if (cur->bc_bufs[0] == cbp) |
| 689 | cur->bc_bufs[0] = NULL; |
| 690 | xfs_iroot_realloc(ip, -1, whichfork); |
| 691 | ASSERT(ifp->if_broot == NULL); |
| 692 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 693 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 694 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Convert an extents-format file into a btree-format file. |
| 700 | * The new file will have a root block (in the inode) and a single child block. |
| 701 | */ |
| 702 | STATIC int /* error */ |
| 703 | xfs_bmap_extents_to_btree( |
| 704 | xfs_trans_t *tp, /* transaction pointer */ |
| 705 | xfs_inode_t *ip, /* incore inode pointer */ |
| 706 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 707 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 708 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 709 | int wasdel, /* converting a delayed alloc */ |
| 710 | int *logflagsp, /* inode logging flags */ |
| 711 | int whichfork) /* data or attr fork */ |
| 712 | { |
| 713 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 714 | xfs_buf_t *abp; /* buffer for ablock */ |
| 715 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 716 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 717 | struct xfs_btree_block *block; /* btree root block */ |
| 718 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 719 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 720 | int error; /* error return value */ |
| 721 | xfs_extnum_t i, cnt; /* extent record index */ |
| 722 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 723 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 724 | xfs_mount_t *mp; /* mount structure */ |
| 725 | xfs_extnum_t nextents; /* number of file extents */ |
| 726 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 727 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 728 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 729 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 730 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 731 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 732 | |
| 733 | /* |
| 734 | * Make space in the inode incore. |
| 735 | */ |
| 736 | xfs_iroot_realloc(ip, 1, whichfork); |
| 737 | ifp->if_flags |= XFS_IFBROOT; |
| 738 | |
| 739 | /* |
| 740 | * Fill in the root. |
| 741 | */ |
| 742 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 743 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 744 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 745 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 746 | /* |
| 747 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 748 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 749 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 750 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 751 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 752 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 753 | /* |
| 754 | * Convert to a btree with two levels, one record in root. |
| 755 | */ |
| 756 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 757 | memset(&args, 0, sizeof(args)); |
| 758 | args.tp = tp; |
| 759 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 760 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 761 | args.firstblock = *firstblock; |
| 762 | if (*firstblock == NULLFSBLOCK) { |
| 763 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 764 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 765 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 766 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 767 | args.fsbno = *firstblock; |
| 768 | } else { |
| 769 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 770 | args.fsbno = *firstblock; |
| 771 | } |
| 772 | args.minlen = args.maxlen = args.prod = 1; |
| 773 | args.wasdel = wasdel; |
| 774 | *logflagsp = 0; |
| 775 | if ((error = xfs_alloc_vextent(&args))) { |
| 776 | xfs_iroot_realloc(ip, -1, whichfork); |
| 777 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 778 | return error; |
| 779 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 780 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 781 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
| 782 | xfs_iroot_realloc(ip, -1, whichfork); |
| 783 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 784 | return -ENOSPC; |
| 785 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 786 | /* |
| 787 | * Allocation can't fail, the space was reserved. |
| 788 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 789 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 790 | args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 791 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 792 | cur->bc_private.b.allocated++; |
| 793 | ip->i_d.di_nblocks++; |
| 794 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 795 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 796 | /* |
| 797 | * Fill in the child block. |
| 798 | */ |
| 799 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 800 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 801 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 802 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 803 | XFS_BTREE_LONG_PTRS); |
| 804 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 805 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 806 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 807 | for (cnt = i = 0; i < nextents; i++) { |
| 808 | ep = xfs_iext_get_ext(ifp, i); |
| 809 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
| 810 | arp->l0 = cpu_to_be64(ep->l0); |
| 811 | arp->l1 = cpu_to_be64(ep->l1); |
| 812 | arp++; cnt++; |
| 813 | } |
| 814 | } |
| 815 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 816 | xfs_btree_set_numrecs(ablock, cnt); |
| 817 | |
| 818 | /* |
| 819 | * Fill in the root key and pointer. |
| 820 | */ |
| 821 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 822 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 823 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 824 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 825 | be16_to_cpu(block->bb_level))); |
| 826 | *pp = cpu_to_be64(args.fsbno); |
| 827 | |
| 828 | /* |
| 829 | * Do all this logging at the end so that |
| 830 | * the root is at the right level. |
| 831 | */ |
| 832 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 833 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 834 | ASSERT(*curp == NULL); |
| 835 | *curp = cur; |
| 836 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Convert a local file to an extents file. |
| 842 | * This code is out of bounds for data forks of regular files, |
| 843 | * since the file data needs to get logged so things will stay consistent. |
| 844 | * (The bmap-level manipulations are ok, though). |
| 845 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 846 | void |
| 847 | xfs_bmap_local_to_extents_empty( |
| 848 | struct xfs_inode *ip, |
| 849 | int whichfork) |
| 850 | { |
| 851 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 852 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 853 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 854 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 855 | ASSERT(ifp->if_bytes == 0); |
| 856 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 857 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 858 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 859 | ifp->if_flags &= ~XFS_IFINLINE; |
| 860 | ifp->if_flags |= XFS_IFEXTENTS; |
| 861 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 862 | } |
| 863 | |
| 864 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 865 | STATIC int /* error */ |
| 866 | xfs_bmap_local_to_extents( |
| 867 | xfs_trans_t *tp, /* transaction pointer */ |
| 868 | xfs_inode_t *ip, /* incore inode pointer */ |
| 869 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 870 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 871 | int *logflagsp, /* inode logging flags */ |
| 872 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 873 | void (*init_fn)(struct xfs_trans *tp, |
| 874 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 875 | struct xfs_inode *ip, |
| 876 | struct xfs_ifork *ifp)) |
| 877 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 878 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 879 | int flags; /* logging flags returned */ |
| 880 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 881 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 882 | xfs_buf_t *bp; /* buffer for extent block */ |
| 883 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 884 | |
| 885 | /* |
| 886 | * We don't want to deal with the case of keeping inode data inline yet. |
| 887 | * So sending the data fork of a regular inode is invalid. |
| 888 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 889 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 890 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 891 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 892 | |
| 893 | if (!ifp->if_bytes) { |
| 894 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 895 | flags = XFS_ILOG_CORE; |
| 896 | goto done; |
| 897 | } |
| 898 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 899 | flags = 0; |
| 900 | error = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 901 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
| 902 | XFS_IFINLINE); |
| 903 | memset(&args, 0, sizeof(args)); |
| 904 | args.tp = tp; |
| 905 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 906 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 907 | args.firstblock = *firstblock; |
| 908 | /* |
| 909 | * Allocate a block. We know we need only one, since the |
| 910 | * file currently fits in an inode. |
| 911 | */ |
| 912 | if (*firstblock == NULLFSBLOCK) { |
| 913 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 914 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 915 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 916 | args.fsbno = *firstblock; |
| 917 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 918 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 919 | args.total = total; |
| 920 | args.minlen = args.maxlen = args.prod = 1; |
| 921 | error = xfs_alloc_vextent(&args); |
| 922 | if (error) |
| 923 | goto done; |
| 924 | |
| 925 | /* Can't fail, the space was reserved. */ |
| 926 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 927 | ASSERT(args.len == 1); |
| 928 | *firstblock = args.fsbno; |
| 929 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 930 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 931 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 932 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 933 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 934 | * The callout is responsible for logging because the remote format |
| 935 | * might differ from the local format and thus we don't know how much to |
| 936 | * log here. Note that init_fn must also set the buffer log item type |
| 937 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 938 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 939 | init_fn(tp, bp, ip, ifp); |
| 940 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 941 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 942 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 943 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 944 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 945 | |
| 946 | xfs_iext_add(ifp, 0, 1); |
| 947 | ep = xfs_iext_get_ext(ifp, 0); |
| 948 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
| 949 | trace_xfs_bmap_post_update(ip, 0, |
| 950 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 951 | _THIS_IP_); |
| 952 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 953 | ip->i_d.di_nblocks = 1; |
| 954 | xfs_trans_mod_dquot_byino(tp, ip, |
| 955 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 956 | flags |= xfs_ilog_fext(whichfork); |
| 957 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 958 | done: |
| 959 | *logflagsp = flags; |
| 960 | return error; |
| 961 | } |
| 962 | |
| 963 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 965 | */ |
| 966 | STATIC int /* error */ |
| 967 | xfs_bmap_add_attrfork_btree( |
| 968 | xfs_trans_t *tp, /* transaction pointer */ |
| 969 | xfs_inode_t *ip, /* incore inode pointer */ |
| 970 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 971 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | int *flags) /* inode logging flags */ |
| 973 | { |
| 974 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 975 | int error; /* error return value */ |
| 976 | xfs_mount_t *mp; /* file system mount struct */ |
| 977 | int stat; /* newroot status */ |
| 978 | |
| 979 | mp = ip->i_mount; |
| 980 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 981 | *flags |= XFS_ILOG_DBROOT; |
| 982 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 983 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 984 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | cur->bc_private.b.firstblock = *firstblock; |
| 986 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 987 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 988 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 989 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 990 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | goto error0; |
| 992 | if (stat == 0) { |
| 993 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 994 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } |
| 996 | *firstblock = cur->bc_private.b.firstblock; |
| 997 | cur->bc_private.b.allocated = 0; |
| 998 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 999 | } |
| 1000 | return 0; |
| 1001 | error0: |
| 1002 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1003 | return error; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 1008 | */ |
| 1009 | STATIC int /* error */ |
| 1010 | xfs_bmap_add_attrfork_extents( |
| 1011 | xfs_trans_t *tp, /* transaction pointer */ |
| 1012 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1013 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1014 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | int *flags) /* inode logging flags */ |
| 1016 | { |
| 1017 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 1018 | int error; /* error return value */ |
| 1019 | |
| 1020 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 1021 | return 0; |
| 1022 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1023 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | flags, XFS_DATA_FORK); |
| 1025 | if (cur) { |
| 1026 | cur->bc_private.b.allocated = 0; |
| 1027 | xfs_btree_del_cursor(cur, |
| 1028 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 1029 | } |
| 1030 | return error; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1034 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 1035 | * different data fork content type needs a different callout to do the |
| 1036 | * conversion. Some are basic and only require special block initialisation |
| 1037 | * callouts for the data formating, others (directories) are so specialised they |
| 1038 | * handle everything themselves. |
| 1039 | * |
| 1040 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1041 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1042 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | */ |
| 1044 | STATIC int /* error */ |
| 1045 | xfs_bmap_add_attrfork_local( |
| 1046 | xfs_trans_t *tp, /* transaction pointer */ |
| 1047 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1048 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1049 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | int *flags) /* inode logging flags */ |
| 1051 | { |
| 1052 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1055 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1056 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1057 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1059 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | dargs.dp = ip; |
| 1061 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1062 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1063 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | dargs.whichfork = XFS_DATA_FORK; |
| 1065 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1066 | return xfs_dir2_sf_to_block(&dargs); |
| 1067 | } |
| 1068 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1069 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1070 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1071 | flags, XFS_DATA_FORK, |
| 1072 | xfs_symlink_local_to_remote); |
| 1073 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1074 | /* should only be called for types that support local format data */ |
| 1075 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1076 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1080 | * Convert inode from non-attributed to attributed. |
| 1081 | * Must not be in a transaction, ip must not be locked. |
| 1082 | */ |
| 1083 | int /* error code */ |
| 1084 | xfs_bmap_add_attrfork( |
| 1085 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1086 | int size, /* space new attribute needs */ |
| 1087 | int rsvd) /* xact may use reserved blks */ |
| 1088 | { |
| 1089 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1090 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1091 | xfs_mount_t *mp; /* mount structure */ |
| 1092 | xfs_trans_t *tp; /* transaction pointer */ |
| 1093 | int blks; /* space reservation */ |
| 1094 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1095 | int logflags; /* logging flags */ |
| 1096 | int error; /* error return value */ |
| 1097 | |
| 1098 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1099 | |
| 1100 | mp = ip->i_mount; |
| 1101 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1102 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1103 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1104 | |
| 1105 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1106 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1107 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1108 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1109 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1110 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1111 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1112 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1113 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1114 | if (error) |
| 1115 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1116 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1117 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1118 | if (ip->i_d.di_anextents != 0) { |
| 1119 | error = -EFSCORRUPTED; |
| 1120 | goto trans_cancel; |
| 1121 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1122 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1123 | /* |
| 1124 | * For inodes coming from pre-6.2 filesystems. |
| 1125 | */ |
| 1126 | ASSERT(ip->i_d.di_aformat == 0); |
| 1127 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1128 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1129 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1130 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1131 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1132 | |
| 1133 | switch (ip->i_d.di_format) { |
| 1134 | case XFS_DINODE_FMT_DEV: |
| 1135 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1136 | break; |
| 1137 | case XFS_DINODE_FMT_UUID: |
| 1138 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 1139 | break; |
| 1140 | case XFS_DINODE_FMT_LOCAL: |
| 1141 | case XFS_DINODE_FMT_EXTENTS: |
| 1142 | case XFS_DINODE_FMT_BTREE: |
| 1143 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1144 | if (!ip->i_d.di_forkoff) |
| 1145 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1146 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1147 | version = 2; |
| 1148 | break; |
| 1149 | default: |
| 1150 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1151 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1152 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | ASSERT(ip->i_afp == NULL); |
| 1156 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1157 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1158 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1159 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1160 | switch (ip->i_d.di_format) { |
| 1161 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1162 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1163 | &logflags); |
| 1164 | break; |
| 1165 | case XFS_DINODE_FMT_EXTENTS: |
| 1166 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1167 | &dfops, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1168 | break; |
| 1169 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1170 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1171 | &logflags); |
| 1172 | break; |
| 1173 | default: |
| 1174 | error = 0; |
| 1175 | break; |
| 1176 | } |
| 1177 | if (logflags) |
| 1178 | xfs_trans_log_inode(tp, ip, logflags); |
| 1179 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1180 | goto bmap_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1181 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1182 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1183 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1184 | |
| 1185 | spin_lock(&mp->m_sb_lock); |
| 1186 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1187 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1188 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1189 | } |
| 1190 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1191 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1192 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1193 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1194 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1195 | if (log_sb) |
| 1196 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1197 | } |
| 1198 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1199 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1200 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1201 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1202 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1203 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1204 | return error; |
| 1205 | |
| 1206 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1207 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1208 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1209 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1210 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1211 | return error; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * Internal and external extent tree search functions. |
| 1216 | */ |
| 1217 | |
| 1218 | /* |
| 1219 | * Read in the extents to if_extents. |
| 1220 | * All inode fields are set up by caller, we just traverse the btree |
| 1221 | * and copy the records in. If the file system cannot contain unwritten |
| 1222 | * extents, the records are checked for no "state" flags. |
| 1223 | */ |
| 1224 | int /* error */ |
| 1225 | xfs_bmap_read_extents( |
| 1226 | xfs_trans_t *tp, /* transaction pointer */ |
| 1227 | xfs_inode_t *ip, /* incore inode */ |
| 1228 | int whichfork) /* data or attr fork */ |
| 1229 | { |
| 1230 | struct xfs_btree_block *block; /* current btree block */ |
| 1231 | xfs_fsblock_t bno; /* block # of "block" */ |
| 1232 | xfs_buf_t *bp; /* buffer for "block" */ |
| 1233 | int error; /* error return value */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1234 | xfs_extnum_t i, j; /* index into the extents list */ |
| 1235 | xfs_ifork_t *ifp; /* fork structure */ |
| 1236 | int level; /* btree level, for checking */ |
| 1237 | xfs_mount_t *mp; /* file system mount structure */ |
| 1238 | __be64 *pp; /* pointer to block address */ |
| 1239 | /* REFERENCED */ |
| 1240 | xfs_extnum_t room; /* number of entries there's room for */ |
| 1241 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1242 | mp = ip->i_mount; |
| 1243 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1244 | block = ifp->if_broot; |
| 1245 | /* |
| 1246 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1247 | */ |
| 1248 | level = be16_to_cpu(block->bb_level); |
| 1249 | ASSERT(level > 0); |
| 1250 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1251 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1252 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1253 | /* |
| 1254 | * Go down the tree until leaf level is reached, following the first |
| 1255 | * pointer (leftmost) at each level. |
| 1256 | */ |
| 1257 | while (level-- > 0) { |
| 1258 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1259 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1260 | if (error) |
| 1261 | return error; |
| 1262 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1263 | if (level == 0) |
| 1264 | break; |
| 1265 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1266 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1267 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 1268 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1269 | xfs_trans_brelse(tp, bp); |
| 1270 | } |
| 1271 | /* |
| 1272 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1273 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1274 | room = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1275 | i = 0; |
| 1276 | /* |
| 1277 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1278 | */ |
| 1279 | for (;;) { |
| 1280 | xfs_bmbt_rec_t *frp; |
| 1281 | xfs_fsblock_t nextbno; |
| 1282 | xfs_extnum_t num_recs; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1283 | |
| 1284 | num_recs = xfs_btree_get_numrecs(block); |
| 1285 | if (unlikely(i + num_recs > room)) { |
| 1286 | ASSERT(i + num_recs <= room); |
| 1287 | xfs_warn(ip->i_mount, |
| 1288 | "corrupt dinode %Lu, (btree extents).", |
| 1289 | (unsigned long long) ip->i_ino); |
| 1290 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 1291 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
| 1292 | goto error0; |
| 1293 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1294 | /* |
| 1295 | * Read-ahead the next leaf block, if any. |
| 1296 | */ |
| 1297 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1298 | if (nextbno != NULLFSBLOCK) |
| 1299 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1300 | &xfs_bmbt_buf_ops); |
| 1301 | /* |
| 1302 | * Copy records into the extent records. |
| 1303 | */ |
| 1304 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1305 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 1306 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
| 1307 | trp->l0 = be64_to_cpu(frp->l0); |
| 1308 | trp->l1 = be64_to_cpu(frp->l1); |
Christoph Hellwig | 0c1d9e4 | 2017-04-20 09:42:48 -0700 | [diff] [blame] | 1309 | if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1310 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
Christoph Hellwig | 0c1d9e4 | 2017-04-20 09:42:48 -0700 | [diff] [blame] | 1311 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1312 | goto error0; |
| 1313 | } |
| 1314 | } |
| 1315 | xfs_trans_brelse(tp, bp); |
| 1316 | bno = nextbno; |
| 1317 | /* |
| 1318 | * If we've reached the end, stop. |
| 1319 | */ |
| 1320 | if (bno == NULLFSBLOCK) |
| 1321 | break; |
| 1322 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1323 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1324 | if (error) |
| 1325 | return error; |
| 1326 | block = XFS_BUF_TO_BLOCK(bp); |
| 1327 | } |
Darrick J. Wong | 356a322 | 2016-12-05 12:36:56 +1100 | [diff] [blame] | 1328 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) |
| 1329 | return -EFSCORRUPTED; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1330 | ASSERT(i == xfs_iext_count(ifp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1331 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
| 1332 | return 0; |
| 1333 | error0: |
| 1334 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1335 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1336 | } |
| 1337 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1338 | /* |
| 1339 | * Returns the file-relative block number of the first unused block(s) |
| 1340 | * in the file with at least "len" logically contiguous blocks free. |
| 1341 | * This is the lowest-address hole if the file has holes, else the first block |
| 1342 | * past the end of file. |
| 1343 | * Return 0 if the file is currently local (in-inode). |
| 1344 | */ |
| 1345 | int /* error */ |
| 1346 | xfs_bmap_first_unused( |
| 1347 | xfs_trans_t *tp, /* transaction pointer */ |
| 1348 | xfs_inode_t *ip, /* incore inode */ |
| 1349 | xfs_extlen_t len, /* size of hole to find */ |
| 1350 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1351 | int whichfork) /* data or attr fork */ |
| 1352 | { |
| 1353 | int error; /* error return value */ |
| 1354 | int idx; /* extent record index */ |
| 1355 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1356 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 1357 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 1358 | xfs_fileoff_t max; /* starting useful block */ |
| 1359 | xfs_fileoff_t off; /* offset for this block */ |
| 1360 | xfs_extnum_t nextents; /* number of extent entries */ |
| 1361 | |
| 1362 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1363 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1364 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 1365 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1366 | *first_unused = 0; |
| 1367 | return 0; |
| 1368 | } |
| 1369 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1370 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1371 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1372 | return error; |
| 1373 | lowest = *first_unused; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1374 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1375 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
| 1376 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
| 1377 | off = xfs_bmbt_get_startoff(ep); |
| 1378 | /* |
| 1379 | * See if the hole before this extent will work. |
| 1380 | */ |
| 1381 | if (off >= lowest + len && off - max >= len) { |
| 1382 | *first_unused = max; |
| 1383 | return 0; |
| 1384 | } |
| 1385 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 1386 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1387 | } |
| 1388 | *first_unused = max; |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1393 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1394 | * last_block (input value) in the file. |
| 1395 | * This is not based on i_size, it is based on the extent records. |
| 1396 | * Returns 0 for local files, as they do not have extent records. |
| 1397 | */ |
| 1398 | int /* error */ |
| 1399 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1400 | struct xfs_trans *tp, /* transaction pointer */ |
| 1401 | struct xfs_inode *ip, /* incore inode */ |
| 1402 | xfs_fileoff_t *last_block, /* last block */ |
| 1403 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1404 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1405 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1406 | struct xfs_bmbt_irec got; |
| 1407 | xfs_extnum_t idx; |
| 1408 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1409 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1410 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1411 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1412 | *last_block = 0; |
| 1413 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1414 | case XFS_DINODE_FMT_BTREE: |
| 1415 | case XFS_DINODE_FMT_EXTENTS: |
| 1416 | break; |
| 1417 | default: |
| 1418 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1419 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1420 | |
| 1421 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1422 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1423 | if (error) |
| 1424 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1425 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1426 | |
| 1427 | if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) { |
| 1428 | if (got.br_startoff <= *last_block - 1) |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | if (xfs_iext_get_extent(ifp, idx - 1, &got)) { |
| 1433 | *last_block = got.br_startoff + got.br_blockcount; |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1438 | return 0; |
| 1439 | } |
| 1440 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1441 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1442 | xfs_bmap_last_extent( |
| 1443 | struct xfs_trans *tp, |
| 1444 | struct xfs_inode *ip, |
| 1445 | int whichfork, |
| 1446 | struct xfs_bmbt_irec *rec, |
| 1447 | int *is_empty) |
| 1448 | { |
| 1449 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1450 | int error; |
| 1451 | int nextents; |
| 1452 | |
| 1453 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1454 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1455 | if (error) |
| 1456 | return error; |
| 1457 | } |
| 1458 | |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1459 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1460 | if (nextents == 0) { |
| 1461 | *is_empty = 1; |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 1466 | *is_empty = 0; |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
| 1470 | /* |
| 1471 | * Check the last inode extent to determine whether this allocation will result |
| 1472 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1473 | * blocks at the end of the file which do not start at the previous data block, |
| 1474 | * we will try to align the new blocks at stripe unit boundaries. |
| 1475 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1476 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1477 | * at, or past the EOF. |
| 1478 | */ |
| 1479 | STATIC int |
| 1480 | xfs_bmap_isaeof( |
| 1481 | struct xfs_bmalloca *bma, |
| 1482 | int whichfork) |
| 1483 | { |
| 1484 | struct xfs_bmbt_irec rec; |
| 1485 | int is_empty; |
| 1486 | int error; |
| 1487 | |
| 1488 | bma->aeof = 0; |
| 1489 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1490 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1491 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1492 | return error; |
| 1493 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1494 | if (is_empty) { |
| 1495 | bma->aeof = 1; |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1499 | /* |
| 1500 | * Check if we are allocation or past the last extent, or at least into |
| 1501 | * the last delayed allocated extent. |
| 1502 | */ |
| 1503 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1504 | (bma->offset >= rec.br_startoff && |
| 1505 | isnullstartblock(rec.br_startblock)); |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1510 | * Returns the file-relative block number of the first block past eof in |
| 1511 | * the file. This is not based on i_size, it is based on the extent records. |
| 1512 | * Returns 0 for local files, as they do not have extent records. |
| 1513 | */ |
| 1514 | int |
| 1515 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1516 | struct xfs_inode *ip, |
| 1517 | xfs_fileoff_t *last_block, |
| 1518 | int whichfork) |
| 1519 | { |
| 1520 | struct xfs_bmbt_irec rec; |
| 1521 | int is_empty; |
| 1522 | int error; |
| 1523 | |
| 1524 | *last_block = 0; |
| 1525 | |
| 1526 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1527 | return 0; |
| 1528 | |
| 1529 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1530 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1531 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1532 | |
| 1533 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1534 | if (error || is_empty) |
| 1535 | return error; |
| 1536 | |
| 1537 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | /* |
| 1542 | * Returns whether the selected fork of the inode has exactly one |
| 1543 | * block or not. For the data fork we check this matches di_size, |
| 1544 | * implying the file's range is 0..bsize-1. |
| 1545 | */ |
| 1546 | int /* 1=>1 block, 0=>otherwise */ |
| 1547 | xfs_bmap_one_block( |
| 1548 | xfs_inode_t *ip, /* incore inode */ |
| 1549 | int whichfork) /* data or attr fork */ |
| 1550 | { |
| 1551 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
| 1552 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1553 | int rval; /* return value */ |
| 1554 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 1555 | |
| 1556 | #ifndef DEBUG |
| 1557 | if (whichfork == XFS_DATA_FORK) |
| 1558 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1559 | #endif /* !DEBUG */ |
| 1560 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1561 | return 0; |
| 1562 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1563 | return 0; |
| 1564 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1565 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 1566 | ep = xfs_iext_get_ext(ifp, 0); |
| 1567 | xfs_bmbt_get_all(ep, &s); |
| 1568 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1569 | if (rval && whichfork == XFS_DATA_FORK) |
| 1570 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1571 | return rval; |
| 1572 | } |
| 1573 | |
| 1574 | /* |
| 1575 | * Extent tree manipulation functions used during allocation. |
| 1576 | */ |
| 1577 | |
| 1578 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1579 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | */ |
| 1581 | STATIC int /* error */ |
| 1582 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1583 | struct xfs_bmalloca *bma, |
| 1584 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1586 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1588 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1591 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1593 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1594 | /* left is 0, right is 1, prev is 2 */ |
| 1595 | int rval=0; /* return value (logging flags) */ |
| 1596 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1597 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1598 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1599 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 1600 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1602 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1603 | xfs_extnum_t *nextents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1605 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1606 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1607 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1608 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1609 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1610 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1611 | ASSERT(bma->idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1612 | ASSERT(bma->idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1613 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1614 | ASSERT(!bma->cur || |
| 1615 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1616 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1617 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | #define LEFT r[0] |
| 1620 | #define RIGHT r[1] |
| 1621 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1623 | if (whichfork == XFS_COW_FORK) |
| 1624 | state |= BMAP_COWFORK; |
| 1625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | /* |
| 1627 | * Set up a bunch of variables to make the tests simpler. |
| 1628 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1629 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | xfs_bmbt_get_all(ep, &PREV); |
| 1631 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1632 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1633 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1634 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1635 | da_old = startblockval(PREV.br_startblock); |
| 1636 | da_new = 0; |
| 1637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | /* |
| 1639 | * Set flags determining what part of the previous delayed allocation |
| 1640 | * extent is being replaced by a real allocation. |
| 1641 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1642 | if (PREV.br_startoff == new->br_startoff) |
| 1643 | state |= BMAP_LEFT_FILLING; |
| 1644 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1645 | state |= BMAP_RIGHT_FILLING; |
| 1646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | /* |
| 1648 | * Check and set flags if this segment has a left neighbor. |
| 1649 | * Don't set contiguous if the combined extent would be too large. |
| 1650 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1651 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1652 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1653 | 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] | 1654 | |
| 1655 | if (isnullstartblock(LEFT.br_startblock)) |
| 1656 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1658 | |
| 1659 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1660 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1661 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1662 | LEFT.br_state == new->br_state && |
| 1663 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1664 | state |= BMAP_LEFT_CONTIG; |
| 1665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | /* |
| 1667 | * Check and set flags if this segment has a right neighbor. |
| 1668 | * Don't set contiguous if the combined extent would be too large. |
| 1669 | * Also check for all-three-contiguous being too large. |
| 1670 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1671 | if (bma->idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1672 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1673 | 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] | 1674 | |
| 1675 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1676 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1678 | |
| 1679 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1680 | new_endoff == RIGHT.br_startoff && |
| 1681 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1682 | new->br_state == RIGHT.br_state && |
| 1683 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1684 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1685 | BMAP_RIGHT_FILLING)) != |
| 1686 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1687 | BMAP_RIGHT_FILLING) || |
| 1688 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1689 | <= MAXEXTLEN)) |
| 1690 | state |= BMAP_RIGHT_CONTIG; |
| 1691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | error = 0; |
| 1693 | /* |
| 1694 | * Switch out based on the FILLING and CONTIG state bits. |
| 1695 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1696 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1697 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1698 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1699 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | /* |
| 1701 | * Filling in all of a previously delayed allocation extent. |
| 1702 | * The left and right neighbors are both contiguous with new. |
| 1703 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1704 | bma->idx--; |
| 1705 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1706 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1708 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1709 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1710 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1711 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1712 | (*nextents)--; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1713 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1715 | else { |
| 1716 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1717 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1719 | RIGHT.br_blockcount, &i); |
| 1720 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1722 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1723 | error = xfs_btree_delete(bma->cur, &i); |
| 1724 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1726 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1727 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1728 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1730 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1731 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | LEFT.br_startblock, |
| 1733 | LEFT.br_blockcount + |
| 1734 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1735 | RIGHT.br_blockcount, LEFT.br_state); |
| 1736 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | goto done; |
| 1738 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | break; |
| 1740 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1741 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | /* |
| 1743 | * Filling in all of a previously delayed allocation extent. |
| 1744 | * The left neighbor is contiguous, the right is not. |
| 1745 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1746 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1747 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1748 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1749 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1750 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1751 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1752 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1753 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1754 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | rval = XFS_ILOG_DEXT; |
| 1756 | else { |
| 1757 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1758 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1760 | &i); |
| 1761 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1763 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1764 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | LEFT.br_startblock, |
| 1766 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1767 | PREV.br_blockcount, LEFT.br_state); |
| 1768 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | goto done; |
| 1770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | break; |
| 1772 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1773 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | /* |
| 1775 | * Filling in all of a previously delayed allocation extent. |
| 1776 | * The right neighbor is contiguous, the left is not. |
| 1777 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1778 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 1780 | xfs_bmbt_set_blockcount(ep, |
| 1781 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1782 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1783 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1784 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1785 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | rval = XFS_ILOG_DEXT; |
| 1787 | else { |
| 1788 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1789 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1791 | RIGHT.br_blockcount, &i); |
| 1792 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1794 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1795 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | new->br_startblock, |
| 1797 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1798 | RIGHT.br_blockcount, PREV.br_state); |
| 1799 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | goto done; |
| 1801 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | break; |
| 1803 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1804 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | /* |
| 1806 | * Filling in all of a previously delayed allocation extent. |
| 1807 | * Neither the left nor right neighbors are contiguous with |
| 1808 | * the new one. |
| 1809 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1810 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 1812 | xfs_bmbt_set_state(ep, new->br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1813 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1814 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1815 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1816 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1818 | else { |
| 1819 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1820 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1822 | &i); |
| 1823 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1825 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1826 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1827 | error = xfs_btree_insert(bma->cur, &i); |
| 1828 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1830 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | break; |
| 1833 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1834 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | /* |
| 1836 | * Filling in the first part of a previous delayed allocation. |
| 1837 | * The left neighbor is contiguous. |
| 1838 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1839 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 1840 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | LEFT.br_blockcount + new->br_blockcount); |
| 1842 | xfs_bmbt_set_startoff(ep, |
| 1843 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1844 | 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] | 1845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | temp = PREV.br_blockcount - new->br_blockcount; |
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_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1849 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | rval = XFS_ILOG_DEXT; |
| 1851 | else { |
| 1852 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1853 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1855 | &i); |
| 1856 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1858 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1859 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | LEFT.br_startblock, |
| 1861 | LEFT.br_blockcount + |
| 1862 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1863 | LEFT.br_state); |
| 1864 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | goto done; |
| 1866 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1867 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1868 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1869 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1870 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1871 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1872 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | break; |
| 1874 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1875 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | /* |
| 1877 | * Filling in the first part of a previous delayed allocation. |
| 1878 | * The left neighbor is not contiguous. |
| 1879 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1880 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1882 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1883 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1884 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1885 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1886 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1888 | else { |
| 1889 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1890 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1892 | &i); |
| 1893 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1895 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1896 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1897 | error = xfs_btree_insert(bma->cur, &i); |
| 1898 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1900 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1902 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1903 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1904 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1905 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1906 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | rval |= tmp_rval; |
| 1908 | if (error) |
| 1909 | goto done; |
| 1910 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1911 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1912 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1913 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 1914 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1915 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1916 | 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] | 1917 | break; |
| 1918 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1919 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | /* |
| 1921 | * Filling in the last part of a previous delayed allocation. |
| 1922 | * The right neighbor is contiguous with the new allocation. |
| 1923 | */ |
| 1924 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1925 | 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] | 1926 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1927 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1928 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | new->br_blockcount + RIGHT.br_blockcount, |
| 1930 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1931 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 1932 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | rval = XFS_ILOG_DEXT; |
| 1934 | else { |
| 1935 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1936 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1938 | RIGHT.br_blockcount, &i); |
| 1939 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1941 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1942 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | new->br_startblock, |
| 1944 | new->br_blockcount + |
| 1945 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1946 | RIGHT.br_state); |
| 1947 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | goto done; |
| 1949 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1950 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1951 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1952 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1953 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1954 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1955 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1956 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1957 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | break; |
| 1959 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1960 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | /* |
| 1962 | * Filling in the last part of a previous delayed allocation. |
| 1963 | * The right neighbor is not contiguous. |
| 1964 | */ |
| 1965 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1966 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1968 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1969 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1970 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1972 | else { |
| 1973 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1974 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1976 | &i); |
| 1977 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1979 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1980 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1981 | error = xfs_btree_insert(bma->cur, &i); |
| 1982 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1984 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1986 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1987 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1988 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1989 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1990 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | rval |= tmp_rval; |
| 1992 | if (error) |
| 1993 | goto done; |
| 1994 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1995 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1996 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1997 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 1998 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1999 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2000 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2001 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2002 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | break; |
| 2004 | |
| 2005 | case 0: |
| 2006 | /* |
| 2007 | * Filling in the middle part of a previous delayed allocation. |
| 2008 | * Contiguity is impossible here. |
| 2009 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2010 | * |
| 2011 | * We start with a delayed allocation: |
| 2012 | * |
| 2013 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 2014 | * PREV @ idx |
| 2015 | * |
| 2016 | * and we are allocating: |
| 2017 | * +rrrrrrrrrrrrrrrrr+ |
| 2018 | * new |
| 2019 | * |
| 2020 | * and we set it up for insertion as: |
| 2021 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 2022 | * new |
| 2023 | * PREV @ idx LEFT RIGHT |
| 2024 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | */ |
| 2026 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2028 | 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] | 2029 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 2030 | LEFT = *new; |
| 2031 | RIGHT.br_state = PREV.br_state; |
| 2032 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2033 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2034 | RIGHT.br_startoff = new_endoff; |
| 2035 | RIGHT.br_blockcount = temp2; |
| 2036 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2037 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2038 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2039 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2041 | else { |
| 2042 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2043 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2045 | &i); |
| 2046 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2048 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2049 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2050 | error = xfs_btree_insert(bma->cur, &i); |
| 2051 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2053 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2055 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2056 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2057 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2058 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2059 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | rval |= tmp_rval; |
| 2061 | if (error) |
| 2062 | goto done; |
| 2063 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2064 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 2065 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Brian Foster | 0daaeca | 2017-05-12 10:44:08 -0700 | [diff] [blame] | 2066 | diff = (int)(temp + temp2 - |
| 2067 | (startblockval(PREV.br_startblock) - |
| 2068 | (bma->cur ? |
| 2069 | bma->cur->bc_private.b.allocated : 0))); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2070 | if (diff > 0) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2071 | error = xfs_mod_fdblocks(bma->ip->i_mount, |
| 2072 | -((int64_t)diff), false); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2073 | ASSERT(!error); |
| 2074 | if (error) |
| 2075 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2077 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2078 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2079 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2080 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2081 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 2082 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2083 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2084 | 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] | 2085 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2086 | bma->idx++; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2087 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | break; |
| 2089 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2090 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2091 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2092 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2093 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2094 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2095 | case BMAP_LEFT_CONTIG: |
| 2096 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | /* |
| 2098 | * These cases are all impossible. |
| 2099 | */ |
| 2100 | ASSERT(0); |
| 2101 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2102 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2103 | /* add reverse mapping */ |
| 2104 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 2105 | if (error) |
| 2106 | goto done; |
| 2107 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2108 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2109 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2110 | int tmp_logflags; /* partial log flag return val */ |
| 2111 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2112 | ASSERT(bma->cur == NULL); |
| 2113 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2114 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2115 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2116 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2117 | if (error) |
| 2118 | goto done; |
| 2119 | } |
| 2120 | |
| 2121 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2122 | if (da_old || da_new) { |
| 2123 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2124 | if (bma->cur) |
| 2125 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2126 | if (temp < da_old) |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2127 | xfs_mod_fdblocks(bma->ip->i_mount, |
| 2128 | (int64_t)(da_old - temp), false); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2132 | if (bma->cur) |
| 2133 | bma->cur->bc_private.b.allocated = 0; |
| 2134 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2135 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2137 | if (whichfork != XFS_COW_FORK) |
| 2138 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | return error; |
| 2140 | #undef LEFT |
| 2141 | #undef RIGHT |
| 2142 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2146 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | */ |
| 2148 | STATIC int /* error */ |
| 2149 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2150 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2152 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2153 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2155 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2156 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2157 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2158 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2161 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2164 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 2166 | xfs_exntst_t newext; /* new extent state */ |
| 2167 | xfs_exntst_t oldext; /* old extent state */ |
| 2168 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2169 | /* left is 0, right is 1, prev is 2 */ |
| 2170 | int rval=0; /* return value (logging flags) */ |
| 2171 | int state = 0;/* state bits, accessed thru macros */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2172 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2174 | *logflagsp = 0; |
| 2175 | |
| 2176 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2177 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2178 | if (whichfork == XFS_COW_FORK) |
| 2179 | state |= BMAP_COWFORK; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2180 | |
| 2181 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2182 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2183 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2184 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2185 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | #define LEFT r[0] |
| 2188 | #define RIGHT r[1] |
| 2189 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | /* |
| 2192 | * Set up a bunch of variables to make the tests simpler. |
| 2193 | */ |
| 2194 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2195 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | xfs_bmbt_get_all(ep, &PREV); |
| 2197 | newext = new->br_state; |
| 2198 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 2199 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 2200 | ASSERT(PREV.br_state == oldext); |
| 2201 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2202 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2203 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | /* |
| 2206 | * Set flags determining what part of the previous oldext allocation |
| 2207 | * extent is being replaced by a newext allocation. |
| 2208 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2209 | if (PREV.br_startoff == new->br_startoff) |
| 2210 | state |= BMAP_LEFT_FILLING; |
| 2211 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2212 | state |= BMAP_RIGHT_FILLING; |
| 2213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | /* |
| 2215 | * Check and set flags if this segment has a left neighbor. |
| 2216 | * Don't set contiguous if the combined extent would be too large. |
| 2217 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2218 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2219 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2220 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2221 | |
| 2222 | if (isnullstartblock(LEFT.br_startblock)) |
| 2223 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2225 | |
| 2226 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2227 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2228 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 2229 | LEFT.br_state == newext && |
| 2230 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2231 | state |= BMAP_LEFT_CONTIG; |
| 2232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | /* |
| 2234 | * Check and set flags if this segment has a right neighbor. |
| 2235 | * Don't set contiguous if the combined extent would be too large. |
| 2236 | * Also check for all-three-contiguous being too large. |
| 2237 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2238 | if (*idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2239 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2240 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2241 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2242 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2244 | |
| 2245 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2246 | new_endoff == RIGHT.br_startoff && |
| 2247 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 2248 | newext == RIGHT.br_state && |
| 2249 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2250 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2251 | BMAP_RIGHT_FILLING)) != |
| 2252 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2253 | BMAP_RIGHT_FILLING) || |
| 2254 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2255 | <= MAXEXTLEN)) |
| 2256 | state |= BMAP_RIGHT_CONTIG; |
| 2257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | /* |
| 2259 | * Switch out based on the FILLING and CONTIG state bits. |
| 2260 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2261 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2262 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2263 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2264 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | /* |
| 2266 | * Setting all of a previous oldext extent to newext. |
| 2267 | * The left and right neighbors are both contiguous with new. |
| 2268 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2269 | --*idx; |
| 2270 | |
| 2271 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2272 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2274 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2275 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2276 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2277 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2278 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2279 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | if (cur == NULL) |
| 2281 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2282 | else { |
| 2283 | rval = XFS_ILOG_CORE; |
| 2284 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2285 | RIGHT.br_startblock, |
| 2286 | RIGHT.br_blockcount, &i))) |
| 2287 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2288 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2289 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2291 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2292 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2294 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2295 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2297 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2298 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2300 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2302 | LEFT.br_startblock, |
| 2303 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2304 | RIGHT.br_blockcount, LEFT.br_state))) |
| 2305 | goto done; |
| 2306 | } |
| 2307 | break; |
| 2308 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2309 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | /* |
| 2311 | * Setting all of a previous oldext extent to newext. |
| 2312 | * The left neighbor is contiguous, the right is not. |
| 2313 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2314 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2315 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2316 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2317 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 2318 | LEFT.br_blockcount + PREV.br_blockcount); |
| 2319 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2320 | |
| 2321 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2322 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2323 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | if (cur == NULL) |
| 2325 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2326 | else { |
| 2327 | rval = XFS_ILOG_CORE; |
| 2328 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2329 | PREV.br_startblock, PREV.br_blockcount, |
| 2330 | &i))) |
| 2331 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2332 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2333 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2335 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2336 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2338 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2340 | LEFT.br_startblock, |
| 2341 | LEFT.br_blockcount + PREV.br_blockcount, |
| 2342 | LEFT.br_state))) |
| 2343 | goto done; |
| 2344 | } |
| 2345 | break; |
| 2346 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2347 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | /* |
| 2349 | * Setting all of a previous oldext extent to newext. |
| 2350 | * The right neighbor is contiguous, the left is not. |
| 2351 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2352 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | xfs_bmbt_set_blockcount(ep, |
| 2354 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 2355 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2356 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2357 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2358 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2359 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | if (cur == NULL) |
| 2361 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2362 | else { |
| 2363 | rval = XFS_ILOG_CORE; |
| 2364 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2365 | RIGHT.br_startblock, |
| 2366 | RIGHT.br_blockcount, &i))) |
| 2367 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2368 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2369 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2371 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2372 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2374 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2376 | new->br_startblock, |
| 2377 | new->br_blockcount + RIGHT.br_blockcount, |
| 2378 | newext))) |
| 2379 | goto done; |
| 2380 | } |
| 2381 | break; |
| 2382 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2383 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | /* |
| 2385 | * Setting all of a previous oldext extent to newext. |
| 2386 | * Neither the left nor right neighbors are contiguous with |
| 2387 | * the new one. |
| 2388 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2389 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2391 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | if (cur == NULL) |
| 2394 | rval = XFS_ILOG_DEXT; |
| 2395 | else { |
| 2396 | rval = 0; |
| 2397 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2398 | new->br_startblock, new->br_blockcount, |
| 2399 | &i))) |
| 2400 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2401 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2403 | new->br_startblock, new->br_blockcount, |
| 2404 | newext))) |
| 2405 | goto done; |
| 2406 | } |
| 2407 | break; |
| 2408 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2409 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | /* |
| 2411 | * Setting the first part of a previous oldext extent to newext. |
| 2412 | * The left neighbor is contiguous. |
| 2413 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2414 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 2415 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | LEFT.br_blockcount + new->br_blockcount); |
| 2417 | xfs_bmbt_set_startoff(ep, |
| 2418 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2419 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2420 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2421 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | xfs_bmbt_set_startblock(ep, |
| 2423 | new->br_startblock + new->br_blockcount); |
| 2424 | xfs_bmbt_set_blockcount(ep, |
| 2425 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2426 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2427 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2428 | --*idx; |
| 2429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | if (cur == NULL) |
| 2431 | rval = XFS_ILOG_DEXT; |
| 2432 | else { |
| 2433 | rval = 0; |
| 2434 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2435 | PREV.br_startblock, PREV.br_blockcount, |
| 2436 | &i))) |
| 2437 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2438 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | if ((error = xfs_bmbt_update(cur, |
| 2440 | PREV.br_startoff + new->br_blockcount, |
| 2441 | PREV.br_startblock + new->br_blockcount, |
| 2442 | PREV.br_blockcount - new->br_blockcount, |
| 2443 | oldext))) |
| 2444 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2445 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2447 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | LEFT.br_startblock, |
| 2449 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2450 | LEFT.br_state); |
| 2451 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | goto done; |
| 2453 | } |
| 2454 | break; |
| 2455 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2456 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | /* |
| 2458 | * Setting the first part of a previous oldext extent to newext. |
| 2459 | * The left neighbor is not contiguous. |
| 2460 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2461 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 2463 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 2464 | xfs_bmbt_set_blockcount(ep, |
| 2465 | PREV.br_blockcount - new->br_blockcount); |
| 2466 | xfs_bmbt_set_startblock(ep, |
| 2467 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2468 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2469 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2470 | xfs_iext_insert(ip, *idx, 1, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2471 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2472 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | if (cur == NULL) |
| 2474 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2475 | else { |
| 2476 | rval = XFS_ILOG_CORE; |
| 2477 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2478 | PREV.br_startblock, PREV.br_blockcount, |
| 2479 | &i))) |
| 2480 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2481 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | if ((error = xfs_bmbt_update(cur, |
| 2483 | PREV.br_startoff + new->br_blockcount, |
| 2484 | PREV.br_startblock + new->br_blockcount, |
| 2485 | PREV.br_blockcount - new->br_blockcount, |
| 2486 | oldext))) |
| 2487 | goto done; |
| 2488 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2489 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2491 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | } |
| 2493 | break; |
| 2494 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2495 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | /* |
| 2497 | * Setting the last part of a previous oldext extent to newext. |
| 2498 | * The right neighbor is contiguous with the new allocation. |
| 2499 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2500 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | xfs_bmbt_set_blockcount(ep, |
| 2502 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2503 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2504 | |
| 2505 | ++*idx; |
| 2506 | |
| 2507 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2508 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2509 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2511 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | if (cur == NULL) |
| 2514 | rval = XFS_ILOG_DEXT; |
| 2515 | else { |
| 2516 | rval = 0; |
| 2517 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2518 | PREV.br_startblock, |
| 2519 | PREV.br_blockcount, &i))) |
| 2520 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2521 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2523 | PREV.br_startblock, |
| 2524 | PREV.br_blockcount - new->br_blockcount, |
| 2525 | oldext))) |
| 2526 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2527 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | goto done; |
| 2529 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2530 | new->br_startblock, |
| 2531 | new->br_blockcount + RIGHT.br_blockcount, |
| 2532 | newext))) |
| 2533 | goto done; |
| 2534 | } |
| 2535 | break; |
| 2536 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2537 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | /* |
| 2539 | * Setting the last part of a previous oldext extent to newext. |
| 2540 | * The right neighbor is not contiguous. |
| 2541 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2542 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | xfs_bmbt_set_blockcount(ep, |
| 2544 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2545 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2546 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2547 | ++*idx; |
| 2548 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2549 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2550 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2551 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | if (cur == NULL) |
| 2553 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2554 | else { |
| 2555 | rval = XFS_ILOG_CORE; |
| 2556 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2557 | PREV.br_startblock, PREV.br_blockcount, |
| 2558 | &i))) |
| 2559 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2560 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2562 | PREV.br_startblock, |
| 2563 | PREV.br_blockcount - new->br_blockcount, |
| 2564 | oldext))) |
| 2565 | goto done; |
| 2566 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2567 | new->br_startblock, new->br_blockcount, |
| 2568 | &i))) |
| 2569 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2570 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2572 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2574 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | } |
| 2576 | break; |
| 2577 | |
| 2578 | case 0: |
| 2579 | /* |
| 2580 | * Setting the middle part of a previous oldext extent to |
| 2581 | * newext. Contiguity is impossible here. |
| 2582 | * One extent becomes three extents. |
| 2583 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2584 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | xfs_bmbt_set_blockcount(ep, |
| 2586 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2587 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | r[0] = *new; |
| 2590 | r[1].br_startoff = new_endoff; |
| 2591 | r[1].br_blockcount = |
| 2592 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 2593 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 2594 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2595 | |
| 2596 | ++*idx; |
| 2597 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 2598 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2599 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2600 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | if (cur == NULL) |
| 2602 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2603 | else { |
| 2604 | rval = XFS_ILOG_CORE; |
| 2605 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2606 | PREV.br_startblock, PREV.br_blockcount, |
| 2607 | &i))) |
| 2608 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2609 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | /* new right extent - oldext */ |
| 2611 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 2612 | r[1].br_startblock, r[1].br_blockcount, |
| 2613 | r[1].br_state))) |
| 2614 | goto done; |
| 2615 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 2617 | cur->bc_rec.b.br_blockcount = |
| 2618 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2619 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2621 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2622 | /* |
| 2623 | * Reset the cursor to the position of the new extent |
| 2624 | * we are about to insert as we can't trust it after |
| 2625 | * the previous insert. |
| 2626 | */ |
| 2627 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2628 | new->br_startblock, new->br_blockcount, |
| 2629 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2631 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2633 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2634 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2636 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | } |
| 2638 | break; |
| 2639 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2640 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2641 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2642 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2643 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2644 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2645 | case BMAP_LEFT_CONTIG: |
| 2646 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | /* |
| 2648 | * These cases are all impossible. |
| 2649 | */ |
| 2650 | ASSERT(0); |
| 2651 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2652 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2653 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2654 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2655 | if (error) |
| 2656 | goto done; |
| 2657 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2658 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2659 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2660 | int tmp_logflags; /* partial log flag return val */ |
| 2661 | |
| 2662 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2663 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2664 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2665 | *logflagsp |= tmp_logflags; |
| 2666 | if (error) |
| 2667 | goto done; |
| 2668 | } |
| 2669 | |
| 2670 | /* clear out the allocated field, done with it now in any case. */ |
| 2671 | if (cur) { |
| 2672 | cur->bc_private.b.allocated = 0; |
| 2673 | *curp = cur; |
| 2674 | } |
| 2675 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2676 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2678 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | return error; |
| 2680 | #undef LEFT |
| 2681 | #undef RIGHT |
| 2682 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2686 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2688 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | xfs_bmap_add_extent_hole_delay( |
| 2690 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2691 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2692 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2693 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2695 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2697 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2698 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2699 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 2700 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2701 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2703 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | state = 0; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2705 | if (whichfork == XFS_COW_FORK) |
| 2706 | state |= BMAP_COWFORK; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2707 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | /* |
| 2710 | * Check and set flags if this segment has a left neighbor |
| 2711 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2712 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2713 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2714 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2715 | |
| 2716 | if (isnullstartblock(left.br_startblock)) |
| 2717 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | /* |
| 2721 | * Check and set flags if the current (right) segment exists. |
| 2722 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2723 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2724 | if (*idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2725 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2726 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2727 | |
| 2728 | if (isnullstartblock(right.br_startblock)) |
| 2729 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | /* |
| 2733 | * Set contiguity flags on the left and right neighbors. |
| 2734 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2735 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2736 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2737 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2738 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2739 | state |= BMAP_LEFT_CONTIG; |
| 2740 | |
| 2741 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2742 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2743 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2744 | (!(state & BMAP_LEFT_CONTIG) || |
| 2745 | (left.br_blockcount + new->br_blockcount + |
| 2746 | right.br_blockcount <= MAXEXTLEN))) |
| 2747 | state |= BMAP_RIGHT_CONTIG; |
| 2748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | /* |
| 2750 | * Switch out based on the contiguity flags. |
| 2751 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2752 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2753 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | /* |
| 2755 | * New allocation is contiguous with delayed allocations |
| 2756 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2757 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2759 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | temp = left.br_blockcount + new->br_blockcount + |
| 2761 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2762 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2763 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2764 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2765 | oldlen = startblockval(left.br_startblock) + |
| 2766 | startblockval(new->br_startblock) + |
| 2767 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2768 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2769 | oldlen); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2770 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2771 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2772 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2773 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2774 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | break; |
| 2776 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2777 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | /* |
| 2779 | * New allocation is contiguous with a delayed allocation |
| 2780 | * on the left. |
| 2781 | * Merge the new allocation with the left neighbor. |
| 2782 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2783 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2785 | |
| 2786 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2787 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2788 | oldlen = startblockval(left.br_startblock) + |
| 2789 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2790 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2791 | oldlen); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2792 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2793 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2794 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | break; |
| 2796 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2797 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | /* |
| 2799 | * New allocation is contiguous with a delayed allocation |
| 2800 | * on the right. |
| 2801 | * Merge the new allocation with the right neighbor. |
| 2802 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2803 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2805 | oldlen = startblockval(new->br_startblock) + |
| 2806 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2807 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2808 | oldlen); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2809 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 2810 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2811 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2812 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | break; |
| 2814 | |
| 2815 | case 0: |
| 2816 | /* |
| 2817 | * New allocation is not contiguous with another |
| 2818 | * delayed allocation. |
| 2819 | * Insert a new entry. |
| 2820 | */ |
| 2821 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2822 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | break; |
| 2824 | } |
| 2825 | if (oldlen != newlen) { |
| 2826 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2827 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2828 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | /* |
| 2830 | * Nothing to do for disk quota accounting here. |
| 2831 | */ |
| 2832 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2836 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | */ |
| 2838 | STATIC int /* error */ |
| 2839 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2840 | struct xfs_trans *tp, |
| 2841 | struct xfs_inode *ip, |
| 2842 | int whichfork, |
| 2843 | xfs_extnum_t *idx, |
| 2844 | struct xfs_btree_cur **curp, |
| 2845 | struct xfs_bmbt_irec *new, |
| 2846 | xfs_fsblock_t *first, |
| 2847 | struct xfs_defer_ops *dfops, |
| 2848 | int *logflagsp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2850 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2851 | struct xfs_mount *mp = ip->i_mount; |
| 2852 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2856 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2857 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | int state; /* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2860 | ASSERT(*idx >= 0); |
| 2861 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2862 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2863 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2864 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2865 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2866 | |
| 2867 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2868 | if (whichfork == XFS_ATTR_FORK) |
| 2869 | state |= BMAP_ATTRFORK; |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 2870 | if (whichfork == XFS_COW_FORK) |
| 2871 | state |= BMAP_COWFORK; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | /* |
| 2874 | * Check and set flags if this segment has a left neighbor. |
| 2875 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2876 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2877 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2878 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2879 | if (isnullstartblock(left.br_startblock)) |
| 2880 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | /* |
| 2884 | * Check and set flags if this segment has a current value. |
| 2885 | * Not true if we're inserting into the "hole" at eof. |
| 2886 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2887 | if (*idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2888 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2889 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2890 | if (isnullstartblock(right.br_startblock)) |
| 2891 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | /* |
| 2895 | * We're inserting a real allocation between "left" and "right". |
| 2896 | * Set the contiguity flags. Don't let extents get too large. |
| 2897 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2898 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2899 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2900 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2901 | left.br_state == new->br_state && |
| 2902 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2903 | state |= BMAP_LEFT_CONTIG; |
| 2904 | |
| 2905 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2906 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2907 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2908 | new->br_state == right.br_state && |
| 2909 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2910 | (!(state & BMAP_LEFT_CONTIG) || |
| 2911 | left.br_blockcount + new->br_blockcount + |
| 2912 | right.br_blockcount <= MAXEXTLEN)) |
| 2913 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2915 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | /* |
| 2917 | * Select which case we're in here, and implement it. |
| 2918 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2919 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2920 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | /* |
| 2922 | * New allocation is contiguous with real allocations on the |
| 2923 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2924 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2926 | --*idx; |
| 2927 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2928 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | left.br_blockcount + new->br_blockcount + |
| 2930 | right.br_blockcount); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2931 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2932 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2933 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2934 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2935 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2936 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2937 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2938 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2939 | } else { |
| 2940 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2941 | error = xfs_bmbt_lookup_eq(cur, right.br_startoff, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2942 | right.br_startblock, right.br_blockcount, |
| 2943 | &i); |
| 2944 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2945 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2946 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2947 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2948 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2949 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2950 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2951 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2952 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2953 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2954 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2955 | error = xfs_bmbt_update(cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2956 | left.br_startblock, |
| 2957 | left.br_blockcount + |
| 2958 | new->br_blockcount + |
| 2959 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2960 | left.br_state); |
| 2961 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2962 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2964 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2966 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | /* |
| 2968 | * New allocation is contiguous with a real allocation |
| 2969 | * on the left. |
| 2970 | * Merge the new allocation with the left neighbor. |
| 2971 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2972 | --*idx; |
| 2973 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2974 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2976 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2977 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2978 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2979 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2980 | } else { |
| 2981 | rval = 0; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2982 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2983 | left.br_startblock, left.br_blockcount, |
| 2984 | &i); |
| 2985 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2986 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2987 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2988 | error = xfs_bmbt_update(cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2989 | left.br_startblock, |
| 2990 | left.br_blockcount + |
| 2991 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2992 | left.br_state); |
| 2993 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2994 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2996 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2998 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | /* |
| 3000 | * New allocation is contiguous with a real allocation |
| 3001 | * on the right. |
| 3002 | * Merge the new allocation with the right neighbor. |
| 3003 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3004 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 3005 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 3006 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3007 | new->br_blockcount + right.br_blockcount, |
| 3008 | right.br_state); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3009 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3010 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3011 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3012 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3013 | } else { |
| 3014 | rval = 0; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3015 | error = xfs_bmbt_lookup_eq(cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3016 | right.br_startoff, |
| 3017 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3018 | right.br_blockcount, &i); |
| 3019 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3020 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3021 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3022 | error = xfs_bmbt_update(cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3023 | new->br_startblock, |
| 3024 | new->br_blockcount + |
| 3025 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3026 | right.br_state); |
| 3027 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3028 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3030 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | |
| 3032 | case 0: |
| 3033 | /* |
| 3034 | * New allocation is not contiguous with another |
| 3035 | * real allocation. |
| 3036 | * Insert a new entry. |
| 3037 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3038 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 3039 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3040 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 3041 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3042 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3043 | } else { |
| 3044 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3045 | error = xfs_bmbt_lookup_eq(cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3046 | new->br_startoff, |
| 3047 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3048 | new->br_blockcount, &i); |
| 3049 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3050 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3051 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3052 | cur->bc_rec.b.br_state = new->br_state; |
| 3053 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3054 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3055 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3056 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3058 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3060 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 3061 | /* add reverse mapping */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3062 | error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 3063 | if (error) |
| 3064 | goto done; |
| 3065 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3066 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3067 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3068 | int tmp_logflags; /* partial log flag return val */ |
| 3069 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3070 | ASSERT(cur == NULL); |
| 3071 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3072 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3073 | *logflagsp |= tmp_logflags; |
| 3074 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3075 | if (error) |
| 3076 | goto done; |
| 3077 | } |
| 3078 | |
| 3079 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3080 | if (cur) |
| 3081 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3082 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3083 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3084 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3085 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3086 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3087 | } |
| 3088 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3089 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 3090 | * Functions used in the extent read, allocate and remove paths |
| 3091 | */ |
| 3092 | |
| 3093 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3094 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 3095 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3096 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3097 | xfs_bmap_extsize_align( |
| 3098 | xfs_mount_t *mp, |
| 3099 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 3100 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 3101 | xfs_extlen_t extsz, /* align to this extent size */ |
| 3102 | int rt, /* is this a realtime inode? */ |
| 3103 | int eof, /* is extent at end-of-file? */ |
| 3104 | int delay, /* creating delalloc extent? */ |
| 3105 | int convert, /* overwriting unwritten extent? */ |
| 3106 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 3107 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 3108 | { |
| 3109 | xfs_fileoff_t orig_off; /* original offset */ |
| 3110 | xfs_extlen_t orig_alen; /* original length */ |
| 3111 | xfs_fileoff_t orig_end; /* original off+len */ |
| 3112 | xfs_fileoff_t nexto; /* next file offset */ |
| 3113 | xfs_fileoff_t prevo; /* previous file offset */ |
| 3114 | xfs_fileoff_t align_off; /* temp for offset */ |
| 3115 | xfs_extlen_t align_alen; /* temp for length */ |
| 3116 | xfs_extlen_t temp; /* temp for calculations */ |
| 3117 | |
| 3118 | if (convert) |
| 3119 | return 0; |
| 3120 | |
| 3121 | orig_off = align_off = *offp; |
| 3122 | orig_alen = align_alen = *lenp; |
| 3123 | orig_end = orig_off + orig_alen; |
| 3124 | |
| 3125 | /* |
| 3126 | * If this request overlaps an existing extent, then don't |
| 3127 | * attempt to perform any additional alignment. |
| 3128 | */ |
| 3129 | if (!delay && !eof && |
| 3130 | (orig_off >= gotp->br_startoff) && |
| 3131 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 3132 | return 0; |
| 3133 | } |
| 3134 | |
| 3135 | /* |
| 3136 | * If the file offset is unaligned vs. the extent size |
| 3137 | * we need to align it. This will be possible unless |
| 3138 | * the file was previously written with a kernel that didn't |
| 3139 | * perform this alignment, or if a truncate shot us in the |
| 3140 | * foot. |
| 3141 | */ |
| 3142 | temp = do_mod(orig_off, extsz); |
| 3143 | if (temp) { |
| 3144 | align_alen += temp; |
| 3145 | align_off -= temp; |
| 3146 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3147 | |
| 3148 | /* Same adjustment for the end of the requested area. */ |
| 3149 | temp = (align_alen % extsz); |
| 3150 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3151 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3152 | |
| 3153 | /* |
| 3154 | * For large extent hint sizes, the aligned extent might be larger than |
| 3155 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 3156 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 3157 | * short allocation just fine, so it is safe to do this. We only want to |
| 3158 | * do it when we are forced to, though, because it means more allocation |
| 3159 | * operations are required. |
| 3160 | */ |
| 3161 | while (align_alen > MAXEXTLEN) |
| 3162 | align_alen -= extsz; |
| 3163 | ASSERT(align_alen <= MAXEXTLEN); |
| 3164 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3165 | /* |
| 3166 | * If the previous block overlaps with this proposed allocation |
| 3167 | * then move the start forward without adjusting the length. |
| 3168 | */ |
| 3169 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3170 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3171 | prevo = prevp->br_startoff; |
| 3172 | else |
| 3173 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3174 | } else |
| 3175 | prevo = 0; |
| 3176 | if (align_off != orig_off && align_off < prevo) |
| 3177 | align_off = prevo; |
| 3178 | /* |
| 3179 | * If the next block overlaps with this proposed allocation |
| 3180 | * then move the start back without adjusting the length, |
| 3181 | * but not before offset 0. |
| 3182 | * This may of course make the start overlap previous block, |
| 3183 | * and if we hit the offset 0 limit then the next block |
| 3184 | * can still overlap too. |
| 3185 | */ |
| 3186 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3187 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3188 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3189 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3190 | else |
| 3191 | nexto = gotp->br_startoff; |
| 3192 | } else |
| 3193 | nexto = NULLFILEOFF; |
| 3194 | if (!eof && |
| 3195 | align_off + align_alen != orig_end && |
| 3196 | align_off + align_alen > nexto) |
| 3197 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3198 | /* |
| 3199 | * If we're now overlapping the next or previous extent that |
| 3200 | * means we can't fit an extsz piece in this hole. Just move |
| 3201 | * the start forward to the first valid spot and set |
| 3202 | * the length so we hit the end. |
| 3203 | */ |
| 3204 | if (align_off != orig_off && align_off < prevo) |
| 3205 | align_off = prevo; |
| 3206 | if (align_off + align_alen != orig_end && |
| 3207 | align_off + align_alen > nexto && |
| 3208 | nexto != NULLFILEOFF) { |
| 3209 | ASSERT(nexto > prevo); |
| 3210 | align_alen = nexto - align_off; |
| 3211 | } |
| 3212 | |
| 3213 | /* |
| 3214 | * If realtime, and the result isn't a multiple of the realtime |
| 3215 | * extent size we need to remove blocks until it is. |
| 3216 | */ |
| 3217 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3218 | /* |
| 3219 | * We're not covering the original request, or |
| 3220 | * we won't be able to once we fix the length. |
| 3221 | */ |
| 3222 | if (orig_off < align_off || |
| 3223 | orig_end > align_off + align_alen || |
| 3224 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3225 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3226 | /* |
| 3227 | * Try to fix it by moving the start up. |
| 3228 | */ |
| 3229 | if (align_off + temp <= orig_off) { |
| 3230 | align_alen -= temp; |
| 3231 | align_off += temp; |
| 3232 | } |
| 3233 | /* |
| 3234 | * Try to fix it by moving the end in. |
| 3235 | */ |
| 3236 | else if (align_off + align_alen - temp >= orig_end) |
| 3237 | align_alen -= temp; |
| 3238 | /* |
| 3239 | * Set the start to the minimum then trim the length. |
| 3240 | */ |
| 3241 | else { |
| 3242 | align_alen -= orig_off - align_off; |
| 3243 | align_off = orig_off; |
| 3244 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3245 | } |
| 3246 | /* |
| 3247 | * Result doesn't cover the request, fail it. |
| 3248 | */ |
| 3249 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3250 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3251 | } else { |
| 3252 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3253 | /* see MAXEXTLEN handling above */ |
| 3254 | ASSERT(orig_end <= align_off + align_alen || |
| 3255 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | #ifdef DEBUG |
| 3259 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3260 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3261 | if (prevp->br_startoff != NULLFILEOFF) |
| 3262 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3263 | #endif |
| 3264 | |
| 3265 | *lenp = align_alen; |
| 3266 | *offp = align_off; |
| 3267 | return 0; |
| 3268 | } |
| 3269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3271 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3272 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3273 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3274 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | { |
| 3276 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3278 | xfs_mount_t *mp; /* mount point structure */ |
| 3279 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3280 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | |
| 3282 | #define ISVALID(x,y) \ |
| 3283 | (rt ? \ |
| 3284 | (x) < mp->m_sb.sb_rblocks : \ |
| 3285 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3286 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3287 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3290 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3291 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3292 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3293 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | /* |
| 3295 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3296 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3298 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3299 | !isnullstartblock(ap->prev.br_startblock) && |
| 3300 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3301 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3302 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | /* |
| 3304 | * Adjust for the gap between prevp and us. |
| 3305 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3306 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3307 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3309 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3310 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | } |
| 3312 | /* |
| 3313 | * If not at eof, then compare the two neighbor blocks. |
| 3314 | * Figure out whether either one gives us a good starting point, |
| 3315 | * and pick the better one. |
| 3316 | */ |
| 3317 | else if (!ap->eof) { |
| 3318 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3319 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3320 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3321 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3322 | |
| 3323 | /* |
| 3324 | * If there's a previous (left) block, select a requested |
| 3325 | * start block based on it. |
| 3326 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3327 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3328 | !isnullstartblock(ap->prev.br_startblock) && |
| 3329 | (prevbno = ap->prev.br_startblock + |
| 3330 | ap->prev.br_blockcount) && |
| 3331 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | /* |
| 3333 | * Calculate gap to end of previous block. |
| 3334 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3335 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3336 | (ap->prev.br_startoff + |
| 3337 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | /* |
| 3339 | * Figure the startblock based on the previous block's |
| 3340 | * end and the gap size. |
| 3341 | * Heuristic! |
| 3342 | * If the gap is large relative to the piece we're |
| 3343 | * allocating, or using it gives us an invalid block |
| 3344 | * number, then just use the end of the previous block. |
| 3345 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3346 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3348 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | prevbno += adjust; |
| 3350 | else |
| 3351 | prevdiff += adjust; |
| 3352 | /* |
| 3353 | * If the firstblock forbids it, can't use it, |
| 3354 | * must use default. |
| 3355 | */ |
| 3356 | if (!rt && !nullfb && |
| 3357 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3358 | prevbno = NULLFSBLOCK; |
| 3359 | } |
| 3360 | /* |
| 3361 | * No previous block or can't follow it, just default. |
| 3362 | */ |
| 3363 | else |
| 3364 | prevbno = NULLFSBLOCK; |
| 3365 | /* |
| 3366 | * If there's a following (right) block, select a requested |
| 3367 | * start block based on it. |
| 3368 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3369 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | /* |
| 3371 | * Calculate gap to start of next block. |
| 3372 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3373 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3374 | /* |
| 3375 | * Figure the startblock based on the next block's |
| 3376 | * start and the gap size. |
| 3377 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3378 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | /* |
| 3380 | * Heuristic! |
| 3381 | * If the gap is large relative to the piece we're |
| 3382 | * allocating, or using it gives us an invalid block |
| 3383 | * number, then just use the start of the next block |
| 3384 | * offset by our length. |
| 3385 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3386 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3387 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3388 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3389 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3390 | gotbno -= ap->length; |
| 3391 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | } else |
| 3393 | gotdiff += adjust; |
| 3394 | /* |
| 3395 | * If the firstblock forbids it, can't use it, |
| 3396 | * must use default. |
| 3397 | */ |
| 3398 | if (!rt && !nullfb && |
| 3399 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3400 | gotbno = NULLFSBLOCK; |
| 3401 | } |
| 3402 | /* |
| 3403 | * No next block, just default. |
| 3404 | */ |
| 3405 | else |
| 3406 | gotbno = NULLFSBLOCK; |
| 3407 | /* |
| 3408 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3409 | * 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] | 3410 | */ |
| 3411 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3412 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3414 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3416 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3418 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3419 | } |
| 3420 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3421 | static int |
| 3422 | xfs_bmap_longest_free_extent( |
| 3423 | struct xfs_trans *tp, |
| 3424 | xfs_agnumber_t ag, |
| 3425 | xfs_extlen_t *blen, |
| 3426 | int *notinit) |
| 3427 | { |
| 3428 | struct xfs_mount *mp = tp->t_mountp; |
| 3429 | struct xfs_perag *pag; |
| 3430 | xfs_extlen_t longest; |
| 3431 | int error = 0; |
| 3432 | |
| 3433 | pag = xfs_perag_get(mp, ag); |
| 3434 | if (!pag->pagf_init) { |
| 3435 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3436 | if (error) |
| 3437 | goto out; |
| 3438 | |
| 3439 | if (!pag->pagf_init) { |
| 3440 | *notinit = 1; |
| 3441 | goto out; |
| 3442 | } |
| 3443 | } |
| 3444 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 3445 | longest = xfs_alloc_longest_free_extent(mp, pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3446 | xfs_alloc_min_freelist(mp, pag), |
| 3447 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3448 | if (*blen < longest) |
| 3449 | *blen = longest; |
| 3450 | |
| 3451 | out: |
| 3452 | xfs_perag_put(pag); |
| 3453 | return error; |
| 3454 | } |
| 3455 | |
| 3456 | static void |
| 3457 | xfs_bmap_select_minlen( |
| 3458 | struct xfs_bmalloca *ap, |
| 3459 | struct xfs_alloc_arg *args, |
| 3460 | xfs_extlen_t *blen, |
| 3461 | int notinit) |
| 3462 | { |
| 3463 | if (notinit || *blen < ap->minlen) { |
| 3464 | /* |
| 3465 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3466 | * there is space for this request. |
| 3467 | */ |
| 3468 | args->minlen = ap->minlen; |
| 3469 | } else if (*blen < args->maxlen) { |
| 3470 | /* |
| 3471 | * If the best seen length is less than the request length, |
| 3472 | * use the best as the minimum. |
| 3473 | */ |
| 3474 | args->minlen = *blen; |
| 3475 | } else { |
| 3476 | /* |
| 3477 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3478 | * as the minimum. |
| 3479 | */ |
| 3480 | args->minlen = args->maxlen; |
| 3481 | } |
| 3482 | } |
| 3483 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3484 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3485 | xfs_bmap_btalloc_nullfb( |
| 3486 | struct xfs_bmalloca *ap, |
| 3487 | struct xfs_alloc_arg *args, |
| 3488 | xfs_extlen_t *blen) |
| 3489 | { |
| 3490 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3491 | xfs_agnumber_t ag, startag; |
| 3492 | int notinit = 0; |
| 3493 | int error; |
| 3494 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3495 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3496 | args->total = ap->total; |
| 3497 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3498 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3499 | if (startag == NULLAGNUMBER) |
| 3500 | startag = ag = 0; |
| 3501 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3502 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3503 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3504 | ¬init); |
| 3505 | if (error) |
| 3506 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3507 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3508 | if (++ag == mp->m_sb.sb_agcount) |
| 3509 | ag = 0; |
| 3510 | if (ag == startag) |
| 3511 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3512 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3513 | |
| 3514 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3515 | return 0; |
| 3516 | } |
| 3517 | |
| 3518 | STATIC int |
| 3519 | xfs_bmap_btalloc_filestreams( |
| 3520 | struct xfs_bmalloca *ap, |
| 3521 | struct xfs_alloc_arg *args, |
| 3522 | xfs_extlen_t *blen) |
| 3523 | { |
| 3524 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3525 | xfs_agnumber_t ag; |
| 3526 | int notinit = 0; |
| 3527 | int error; |
| 3528 | |
| 3529 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3530 | args->total = ap->total; |
| 3531 | |
| 3532 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3533 | if (ag == NULLAGNUMBER) |
| 3534 | ag = 0; |
| 3535 | |
| 3536 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3537 | if (error) |
| 3538 | return error; |
| 3539 | |
| 3540 | if (*blen < args->maxlen) { |
| 3541 | error = xfs_filestream_new_ag(ap, &ag); |
| 3542 | if (error) |
| 3543 | return error; |
| 3544 | |
| 3545 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3546 | ¬init); |
| 3547 | if (error) |
| 3548 | return error; |
| 3549 | |
| 3550 | } |
| 3551 | |
| 3552 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3553 | |
| 3554 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3555 | * Set the failure fallback case to look in the selected AG as stream |
| 3556 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3557 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3558 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3559 | return 0; |
| 3560 | } |
| 3561 | |
| 3562 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3563 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3564 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3565 | { |
| 3566 | xfs_mount_t *mp; /* mount point structure */ |
| 3567 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3568 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3569 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3570 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3571 | xfs_alloc_arg_t args; |
| 3572 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3573 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3574 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3575 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3576 | int tryagain; |
| 3577 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3578 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3579 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3580 | ASSERT(ap->length); |
| 3581 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3582 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3583 | |
| 3584 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3585 | stripe_align = 0; |
| 3586 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3587 | stripe_align = mp->m_swidth; |
| 3588 | else if (mp->m_dalign) |
| 3589 | stripe_align = mp->m_dalign; |
| 3590 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3591 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3592 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3593 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3594 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3595 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3596 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3597 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3598 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3599 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3600 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3601 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3602 | |
| 3603 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3604 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3605 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3606 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3607 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3608 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3609 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3610 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3611 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3612 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3613 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3614 | } |
| 3615 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3616 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3617 | |
| 3618 | xfs_bmap_adjacent(ap); |
| 3619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3621 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | * it's in the right allocation group. |
| 3623 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3624 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | ; |
| 3626 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3627 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3629 | * Normal allocation, done through xfs_alloc_vextent. |
| 3630 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3631 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3632 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3633 | args.tp = ap->tp; |
| 3634 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3635 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3636 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3637 | |
| 3638 | /* Trim the allocation back to the maximum an AG can fit. */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 3639 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3640 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3641 | blen = 0; |
| 3642 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3643 | /* |
| 3644 | * Search for an allocation group with a single extent large |
| 3645 | * enough for the request. If one isn't found, then adjust |
| 3646 | * the minimum allocation size to the largest space found. |
| 3647 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3648 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3649 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3650 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3651 | else |
| 3652 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3653 | if (error) |
| 3654 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3655 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3656 | if (xfs_inode_is_filestream(ap->ip)) |
| 3657 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3658 | else |
| 3659 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3660 | args.total = args.minlen = ap->minlen; |
| 3661 | } else { |
| 3662 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3663 | args.total = ap->total; |
| 3664 | args.minlen = ap->minlen; |
| 3665 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3666 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3667 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3668 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3669 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3670 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3671 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3672 | args.prod = 1; |
| 3673 | args.mod = 0; |
| 3674 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3675 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3676 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3677 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3678 | } |
| 3679 | /* |
| 3680 | * If we are not low on available data blocks, and the |
| 3681 | * underlying logical volume manager is a stripe, and |
| 3682 | * the file offset is zero then try to allocate data |
| 3683 | * blocks on stripe unit boundary. |
| 3684 | * NOTE: ap->aeof is only set if the allocation length |
| 3685 | * is >= the stripe unit and the allocation offset is |
| 3686 | * at the end of file. |
| 3687 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3688 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3689 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3690 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3691 | atype = args.type; |
| 3692 | isaligned = 1; |
| 3693 | /* |
| 3694 | * Adjust for alignment |
| 3695 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3696 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3697 | args.minlen = blen - args.alignment; |
| 3698 | args.minalignslop = 0; |
| 3699 | } else { |
| 3700 | /* |
| 3701 | * First try an exact bno allocation. |
| 3702 | * If it fails then do a near or start bno |
| 3703 | * allocation with alignment turned on. |
| 3704 | */ |
| 3705 | atype = args.type; |
| 3706 | tryagain = 1; |
| 3707 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3708 | args.alignment = 1; |
| 3709 | /* |
| 3710 | * Compute the minlen+alignment for the |
| 3711 | * next case. Set slop so that the value |
| 3712 | * of minlen+alignment+slop doesn't go up |
| 3713 | * between the calls. |
| 3714 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3715 | if (blen > stripe_align && blen <= args.maxlen) |
| 3716 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3717 | else |
| 3718 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3719 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3720 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3721 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3722 | args.minlen - 1; |
| 3723 | else |
| 3724 | args.minalignslop = 0; |
| 3725 | } |
| 3726 | } else { |
| 3727 | args.alignment = 1; |
| 3728 | args.minalignslop = 0; |
| 3729 | } |
| 3730 | args.minleft = ap->minleft; |
| 3731 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3732 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3733 | args.datatype = ap->datatype; |
| 3734 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3735 | args.ip = ap->ip; |
| 3736 | |
| 3737 | error = xfs_alloc_vextent(&args); |
| 3738 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3739 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3740 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3741 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3742 | /* |
| 3743 | * Exact allocation failed. Now try with alignment |
| 3744 | * turned on. |
| 3745 | */ |
| 3746 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3747 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3748 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3749 | args.minlen = nextminlen; |
| 3750 | args.minalignslop = 0; |
| 3751 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3752 | if ((error = xfs_alloc_vextent(&args))) |
| 3753 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3754 | } |
| 3755 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3756 | /* |
| 3757 | * allocation failed, so turn off alignment and |
| 3758 | * try again. |
| 3759 | */ |
| 3760 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3761 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3762 | args.alignment = 0; |
| 3763 | if ((error = xfs_alloc_vextent(&args))) |
| 3764 | return error; |
| 3765 | } |
| 3766 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3767 | args.minlen > ap->minlen) { |
| 3768 | args.minlen = ap->minlen; |
| 3769 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3770 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3771 | if ((error = xfs_alloc_vextent(&args))) |
| 3772 | return error; |
| 3773 | } |
| 3774 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3775 | args.fsbno = 0; |
| 3776 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3777 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3778 | if ((error = xfs_alloc_vextent(&args))) |
| 3779 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3780 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3781 | } |
| 3782 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3783 | /* |
| 3784 | * check the allocation happened at the same or higher AG than |
| 3785 | * the first block that was allocated. |
| 3786 | */ |
| 3787 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3788 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) <= |
| 3789 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3790 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3791 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3792 | if (*ap->firstblock == NULLFSBLOCK) |
| 3793 | *ap->firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3794 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3795 | ap->length = args.len; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 3796 | if (!(ap->flags & XFS_BMAPI_COWFORK)) |
| 3797 | ap->ip->i_d.di_nblocks += args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3798 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3799 | if (ap->wasdel) |
| 3800 | ap->ip->i_delayed_blks -= args.len; |
| 3801 | /* |
| 3802 | * Adjust the disk quota also. This was reserved |
| 3803 | * earlier. |
| 3804 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3805 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3806 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3807 | XFS_TRANS_DQ_BCOUNT, |
| 3808 | (long) args.len); |
| 3809 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3810 | ap->blkno = NULLFSBLOCK; |
| 3811 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | } |
| 3813 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | /* |
| 3817 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3818 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3819 | */ |
| 3820 | STATIC int |
| 3821 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3822 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3823 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3824 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3825 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3826 | return xfs_bmap_rtalloc(ap); |
| 3827 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3830 | /* Trim extent to fit a logical block range. */ |
| 3831 | void |
| 3832 | xfs_trim_extent( |
| 3833 | struct xfs_bmbt_irec *irec, |
| 3834 | xfs_fileoff_t bno, |
| 3835 | xfs_filblks_t len) |
| 3836 | { |
| 3837 | xfs_fileoff_t distance; |
| 3838 | xfs_fileoff_t end = bno + len; |
| 3839 | |
| 3840 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3841 | irec->br_startoff >= end) { |
| 3842 | irec->br_blockcount = 0; |
| 3843 | return; |
| 3844 | } |
| 3845 | |
| 3846 | if (irec->br_startoff < bno) { |
| 3847 | distance = bno - irec->br_startoff; |
| 3848 | if (isnullstartblock(irec->br_startblock)) |
| 3849 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3850 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3851 | irec->br_startblock != HOLESTARTBLOCK) |
| 3852 | irec->br_startblock += distance; |
| 3853 | irec->br_startoff += distance; |
| 3854 | irec->br_blockcount -= distance; |
| 3855 | } |
| 3856 | |
| 3857 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3858 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3859 | irec->br_blockcount -= distance; |
| 3860 | } |
| 3861 | } |
| 3862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3863 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3864 | * Trim the returned map to the required bounds |
| 3865 | */ |
| 3866 | STATIC void |
| 3867 | xfs_bmapi_trim_map( |
| 3868 | struct xfs_bmbt_irec *mval, |
| 3869 | struct xfs_bmbt_irec *got, |
| 3870 | xfs_fileoff_t *bno, |
| 3871 | xfs_filblks_t len, |
| 3872 | xfs_fileoff_t obno, |
| 3873 | xfs_fileoff_t end, |
| 3874 | int n, |
| 3875 | int flags) |
| 3876 | { |
| 3877 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3878 | got->br_startoff + got->br_blockcount <= obno) { |
| 3879 | *mval = *got; |
| 3880 | if (isnullstartblock(got->br_startblock)) |
| 3881 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3882 | return; |
| 3883 | } |
| 3884 | |
| 3885 | if (obno > *bno) |
| 3886 | *bno = obno; |
| 3887 | ASSERT((*bno >= obno) || (n == 0)); |
| 3888 | ASSERT(*bno < end); |
| 3889 | mval->br_startoff = *bno; |
| 3890 | if (isnullstartblock(got->br_startblock)) |
| 3891 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3892 | else |
| 3893 | mval->br_startblock = got->br_startblock + |
| 3894 | (*bno - got->br_startoff); |
| 3895 | /* |
| 3896 | * Return the minimum of what we got and what we asked for for |
| 3897 | * the length. We can use the len variable here because it is |
| 3898 | * modified below and we could have been there before coming |
| 3899 | * here if the first part of the allocation didn't overlap what |
| 3900 | * was asked for. |
| 3901 | */ |
| 3902 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3903 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3904 | mval->br_state = got->br_state; |
| 3905 | ASSERT(mval->br_blockcount <= len); |
| 3906 | return; |
| 3907 | } |
| 3908 | |
| 3909 | /* |
| 3910 | * Update and validate the extent map to return |
| 3911 | */ |
| 3912 | STATIC void |
| 3913 | xfs_bmapi_update_map( |
| 3914 | struct xfs_bmbt_irec **map, |
| 3915 | xfs_fileoff_t *bno, |
| 3916 | xfs_filblks_t *len, |
| 3917 | xfs_fileoff_t obno, |
| 3918 | xfs_fileoff_t end, |
| 3919 | int *n, |
| 3920 | int flags) |
| 3921 | { |
| 3922 | xfs_bmbt_irec_t *mval = *map; |
| 3923 | |
| 3924 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3925 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3926 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3927 | (mval->br_startoff < obno)); |
| 3928 | |
| 3929 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3930 | *len = end - *bno; |
| 3931 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3932 | /* update previous map with new information */ |
| 3933 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3934 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3935 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3936 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3937 | mval[-1].br_state = mval->br_state; |
| 3938 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3939 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3940 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3941 | mval->br_startblock == mval[-1].br_startblock + |
| 3942 | mval[-1].br_blockcount && |
| 3943 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3944 | mval[-1].br_state == mval->br_state)) { |
| 3945 | ASSERT(mval->br_startoff == |
| 3946 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3947 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3948 | } else if (*n > 0 && |
| 3949 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3950 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3951 | mval->br_startoff == |
| 3952 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3953 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3954 | mval[-1].br_state = mval->br_state; |
| 3955 | } else if (!((*n == 0) && |
| 3956 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3957 | obno))) { |
| 3958 | mval++; |
| 3959 | (*n)++; |
| 3960 | } |
| 3961 | *map = mval; |
| 3962 | } |
| 3963 | |
| 3964 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3965 | * Map file blocks to filesystem blocks without allocation. |
| 3966 | */ |
| 3967 | int |
| 3968 | xfs_bmapi_read( |
| 3969 | struct xfs_inode *ip, |
| 3970 | xfs_fileoff_t bno, |
| 3971 | xfs_filblks_t len, |
| 3972 | struct xfs_bmbt_irec *mval, |
| 3973 | int *nmap, |
| 3974 | int flags) |
| 3975 | { |
| 3976 | struct xfs_mount *mp = ip->i_mount; |
| 3977 | struct xfs_ifork *ifp; |
| 3978 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3979 | xfs_fileoff_t obno; |
| 3980 | xfs_fileoff_t end; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3981 | xfs_extnum_t idx; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3982 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3983 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3984 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3985 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3986 | |
| 3987 | ASSERT(*nmap >= 1); |
| 3988 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3989 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3990 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3991 | |
| 3992 | if (unlikely(XFS_TEST_ERROR( |
| 3993 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3994 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 3995 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3996 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3997 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
| 4000 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4001 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4002 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4003 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4004 | |
| 4005 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4006 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4007 | /* No CoW fork? Return a hole. */ |
| 4008 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 4009 | mval->br_startoff = bno; |
| 4010 | mval->br_startblock = HOLESTARTBLOCK; |
| 4011 | mval->br_blockcount = len; |
| 4012 | mval->br_state = XFS_EXT_NORM; |
| 4013 | *nmap = 1; |
| 4014 | return 0; |
| 4015 | } |
| 4016 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4017 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4018 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4019 | if (error) |
| 4020 | return error; |
| 4021 | } |
| 4022 | |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4023 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) |
| 4024 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4025 | end = bno + len; |
| 4026 | obno = bno; |
| 4027 | |
| 4028 | while (bno < end && n < *nmap) { |
| 4029 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4030 | if (eof) |
| 4031 | got.br_startoff = end; |
| 4032 | if (got.br_startoff > bno) { |
| 4033 | /* Reading in a hole. */ |
| 4034 | mval->br_startoff = bno; |
| 4035 | mval->br_startblock = HOLESTARTBLOCK; |
| 4036 | mval->br_blockcount = |
| 4037 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4038 | mval->br_state = XFS_EXT_NORM; |
| 4039 | bno += mval->br_blockcount; |
| 4040 | len -= mval->br_blockcount; |
| 4041 | mval++; |
| 4042 | n++; |
| 4043 | continue; |
| 4044 | } |
| 4045 | |
| 4046 | /* set up the extent map to return. */ |
| 4047 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4048 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4049 | |
| 4050 | /* If we're done, stop now. */ |
| 4051 | if (bno >= end || n >= *nmap) |
| 4052 | break; |
| 4053 | |
| 4054 | /* Else go on to the next record. */ |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4055 | if (!xfs_iext_get_extent(ifp, ++idx, &got)) |
| 4056 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4057 | } |
| 4058 | *nmap = n; |
| 4059 | return 0; |
| 4060 | } |
| 4061 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 4062 | /* |
| 4063 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 4064 | * global pool and the extent inserted into the inode in-core extent tree. |
| 4065 | * |
| 4066 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 4067 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 4068 | * to the extent record that was inserted to the inode fork. |
| 4069 | * |
| 4070 | * Note that the allocated extent may have been merged with contiguous extents |
| 4071 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 4072 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 4073 | * look up the updated record in the inode fork. |
| 4074 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 4075 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4076 | xfs_bmapi_reserve_delalloc( |
| 4077 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4078 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4079 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4080 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4081 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4082 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4083 | xfs_extnum_t *lastx, |
| 4084 | int eof) |
| 4085 | { |
| 4086 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4087 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4088 | xfs_extlen_t alen; |
| 4089 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4090 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4091 | xfs_extlen_t extsz; |
| 4092 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4093 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4094 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4095 | /* |
| 4096 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 4097 | * tag the inode before we return. |
| 4098 | */ |
| 4099 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4100 | if (!eof) |
| 4101 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4102 | if (prealloc && alen >= len) |
| 4103 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4104 | |
| 4105 | /* Figure out the extent size, adjust alen */ |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 4106 | if (whichfork == XFS_COW_FORK) |
| 4107 | extsz = xfs_get_cowextsz_hint(ip); |
| 4108 | else |
| 4109 | extsz = xfs_get_extsz_hint(ip); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4110 | if (extsz) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 4111 | struct xfs_bmbt_irec prev; |
| 4112 | |
| 4113 | if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev)) |
| 4114 | prev.br_startoff = NULLFILEOFF; |
| 4115 | |
| 4116 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4117 | 1, 0, &aoff, &alen); |
| 4118 | ASSERT(!error); |
| 4119 | } |
| 4120 | |
| 4121 | if (rt) |
| 4122 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4123 | |
| 4124 | /* |
| 4125 | * Make a transaction-less quota reservation for delayed allocation |
| 4126 | * blocks. This number gets adjusted later. We return if we haven't |
| 4127 | * allocated blocks already inside this loop. |
| 4128 | */ |
| 4129 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4130 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4131 | if (error) |
| 4132 | return error; |
| 4133 | |
| 4134 | /* |
| 4135 | * Split changing sb for alen and indlen since they could be coming |
| 4136 | * from different places. |
| 4137 | */ |
| 4138 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4139 | ASSERT(indlen > 0); |
| 4140 | |
| 4141 | if (rt) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4142 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4143 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4144 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | if (error) |
| 4148 | goto out_unreserve_quota; |
| 4149 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4150 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4151 | if (error) |
| 4152 | goto out_unreserve_blocks; |
| 4153 | |
| 4154 | |
| 4155 | ip->i_delayed_blks += alen; |
| 4156 | |
| 4157 | got->br_startoff = aoff; |
| 4158 | got->br_startblock = nullstartblock(indlen); |
| 4159 | got->br_blockcount = alen; |
| 4160 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4161 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 4162 | xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4163 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4164 | /* |
| 4165 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 4166 | * preallocation can occur at the start or end of the extent, even when |
| 4167 | * prealloc == 0, so we must also check the aligned offset and length. |
| 4168 | */ |
| 4169 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4170 | xfs_inode_set_eofblocks_tag(ip); |
| 4171 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4172 | xfs_inode_set_cowblocks_tag(ip); |
| 4173 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4174 | return 0; |
| 4175 | |
| 4176 | out_unreserve_blocks: |
| 4177 | if (rt) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4178 | xfs_mod_frextents(mp, extsz); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4179 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4180 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4181 | out_unreserve_quota: |
| 4182 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 4183 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4184 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4185 | return error; |
| 4186 | } |
| 4187 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4188 | static int |
| 4189 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4190 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4191 | { |
| 4192 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4193 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4194 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4195 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4196 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4197 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4198 | ASSERT(bma->length > 0); |
| 4199 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4200 | /* |
| 4201 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4202 | * for in this bmap call but that wouldn't be as good. |
| 4203 | */ |
| 4204 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4205 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4206 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | 0e8d630 | 2016-11-24 11:40:32 +1100 | [diff] [blame] | 4207 | if (bma->idx) { |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4208 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4209 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4210 | } |
| 4211 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4212 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4213 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4214 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4215 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
| 4218 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4219 | * Set the data type being allocated. For the data fork, the first data |
| 4220 | * in the file is treated differently to all other allocations. For the |
| 4221 | * attribute fork, we only need to ensure the allocated range is not on |
| 4222 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4223 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4224 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4225 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4226 | if (whichfork == XFS_DATA_FORK) { |
| 4227 | if (bma->offset == 0) |
| 4228 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4229 | else |
| 4230 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4231 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4232 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4233 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4236 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4237 | |
| 4238 | /* |
| 4239 | * Only want to do the alignment at the eof if it is userdata and |
| 4240 | * allocation length is larger than a stripe unit. |
| 4241 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4242 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4243 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4244 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4245 | if (error) |
| 4246 | return error; |
| 4247 | } |
| 4248 | |
| 4249 | error = xfs_bmap_alloc(bma); |
| 4250 | if (error) |
| 4251 | return error; |
| 4252 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4253 | if (bma->cur) |
| 4254 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4255 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4256 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4257 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4258 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4259 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4260 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4261 | } |
| 4262 | /* |
| 4263 | * Bump the number of extents we've allocated |
| 4264 | * in this call. |
| 4265 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4266 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4267 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4268 | if (bma->cur) |
| 4269 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4270 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4271 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4272 | bma->got.br_startoff = bma->offset; |
| 4273 | bma->got.br_startblock = bma->blkno; |
| 4274 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4275 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4276 | |
| 4277 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4278 | * In the data fork, a wasdelay extent has been initialized, so |
| 4279 | * shouldn't be flagged as unwritten. |
| 4280 | * |
| 4281 | * For the cow fork, however, we convert delalloc reservations |
| 4282 | * (extents allocated for speculative preallocation) to |
| 4283 | * allocated unwritten extents, and only convert the unwritten |
| 4284 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4285 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4286 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4287 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4288 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4289 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4290 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4291 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4292 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4293 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4294 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
| 4295 | whichfork, &bma->idx, &bma->cur, &bma->got, |
| 4296 | bma->firstblock, bma->dfops, &bma->logflags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4297 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4298 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4299 | if (error) |
| 4300 | return error; |
| 4301 | |
| 4302 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4303 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4304 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4305 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4306 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4307 | 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] | 4308 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4309 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4310 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4311 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4312 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4313 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4314 | return 0; |
| 4315 | } |
| 4316 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4317 | STATIC int |
| 4318 | xfs_bmapi_convert_unwritten( |
| 4319 | struct xfs_bmalloca *bma, |
| 4320 | struct xfs_bmbt_irec *mval, |
| 4321 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4322 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4323 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4324 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4325 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4326 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4327 | int error; |
| 4328 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4329 | /* check if we need to do unwritten->real conversion */ |
| 4330 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4331 | (flags & XFS_BMAPI_PREALLOC)) |
| 4332 | return 0; |
| 4333 | |
| 4334 | /* check if we need to do real->unwritten conversion */ |
| 4335 | if (mval->br_state == XFS_EXT_NORM && |
| 4336 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4337 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4338 | return 0; |
| 4339 | |
| 4340 | /* |
| 4341 | * Modify (by adding) the state flag, if writing. |
| 4342 | */ |
| 4343 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4344 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4345 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4346 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4347 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4348 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4349 | } |
| 4350 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4351 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4352 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4353 | /* |
| 4354 | * Before insertion into the bmbt, zero the range being converted |
| 4355 | * if required. |
| 4356 | */ |
| 4357 | if (flags & XFS_BMAPI_ZERO) { |
| 4358 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4359 | mval->br_blockcount); |
| 4360 | if (error) |
| 4361 | return error; |
| 4362 | } |
| 4363 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4364 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
| 4365 | &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4366 | &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4367 | /* |
| 4368 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4369 | * path because the conversion might not have done so (e.g., if the |
| 4370 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4371 | * in the transaction for the sake of fsync(), even if nothing has |
| 4372 | * changed, because fsync() will not force the log for this transaction |
| 4373 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4374 | * |
| 4375 | * Note: If we're only converting cow fork extents, there aren't |
| 4376 | * any on-disk updates to make, so we don't need to log anything. |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4377 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4378 | if (whichfork != XFS_COW_FORK) |
| 4379 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4380 | if (error) |
| 4381 | return error; |
| 4382 | |
| 4383 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4384 | * Update our extent pointer, given that |
| 4385 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4386 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4387 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4388 | 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] | 4389 | |
| 4390 | /* |
| 4391 | * We may have combined previously unwritten space with written space, |
| 4392 | * so generate another request. |
| 4393 | */ |
| 4394 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4395 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4396 | return 0; |
| 4397 | } |
| 4398 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4399 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4400 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4401 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4402 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4403 | * locking problems. |
| 4404 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | * The returned value in "firstblock" from the first call in a transaction |
| 4406 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4407 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4408 | * the first call in "total"; if no allocation group has that many free |
| 4409 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4410 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4411 | int |
| 4412 | xfs_bmapi_write( |
| 4413 | struct xfs_trans *tp, /* transaction pointer */ |
| 4414 | struct xfs_inode *ip, /* incore inode */ |
| 4415 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4416 | xfs_filblks_t len, /* length to map in file */ |
| 4417 | int flags, /* XFS_BMAPI_... */ |
| 4418 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4419 | controls a.g. for allocs */ |
| 4420 | xfs_extlen_t total, /* total blocks needed */ |
| 4421 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4422 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4423 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4425 | struct xfs_mount *mp = ip->i_mount; |
| 4426 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4427 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4428 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4429 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4430 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4431 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4432 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4433 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4436 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4437 | int orig_flags; /* original flags arg value */ |
| 4438 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4439 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4440 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | |
| 4442 | orig_bno = bno; |
| 4443 | orig_len = len; |
| 4444 | orig_flags = flags; |
| 4445 | orig_mval = mval; |
| 4446 | orig_nmap = *nmap; |
| 4447 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4448 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4451 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4452 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4453 | ASSERT(tp != NULL || |
| 4454 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4455 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4456 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4457 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4458 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4459 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4460 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4461 | /* zeroing is for currently only for data extents, not metadata */ |
| 4462 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4463 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4464 | /* |
| 4465 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4466 | * but it makes no sense to do both at once. This would result in |
| 4467 | * zeroing the unwritten extent twice, but it still being an |
| 4468 | * unwritten extent.... |
| 4469 | */ |
| 4470 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4471 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | if (unlikely(XFS_TEST_ERROR( |
| 4474 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4475 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4476 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4477 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4478 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4482 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4485 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4486 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4487 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4488 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4490 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4492 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4493 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4494 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4498 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4499 | if (error) |
| 4500 | goto error0; |
| 4501 | } |
| 4502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | n = 0; |
| 4504 | end = bno + len; |
| 4505 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4506 | |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4507 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got)) |
| 4508 | eof = true; |
| 4509 | if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev)) |
| 4510 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4511 | bma.tp = tp; |
| 4512 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4513 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4514 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4515 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4516 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4519 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4520 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4521 | /* in hole or beyoned EOF? */ |
| 4522 | if (eof || bma.got.br_startoff > bno) { |
| 4523 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4524 | /* |
| 4525 | * For the COW fork we can reasonably get a |
| 4526 | * request for converting an extent that races |
| 4527 | * with other threads already having converted |
| 4528 | * part of it, as there converting COW to |
| 4529 | * regular blocks is not protected using the |
| 4530 | * IOLOCK. |
| 4531 | */ |
| 4532 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4533 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4534 | error = -EIO; |
| 4535 | goto error0; |
| 4536 | } |
| 4537 | |
| 4538 | if (eof || bno >= end) |
| 4539 | break; |
| 4540 | } else { |
| 4541 | need_alloc = true; |
| 4542 | } |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4543 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4544 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4545 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4546 | |
| 4547 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | * First, deal with the hole before the allocated space |
| 4549 | * that we found, if any. |
| 4550 | */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4551 | if (need_alloc || 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; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4577 | |
| 4578 | /* |
| 4579 | * If this is a CoW allocation, record the data in |
| 4580 | * the refcount btree for orphan recovery. |
| 4581 | */ |
| 4582 | if (whichfork == XFS_COW_FORK) { |
| 4583 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4584 | bma.blkno, bma.length); |
| 4585 | if (error) |
| 4586 | goto error0; |
| 4587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4589 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4590 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4591 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4592 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4594 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4595 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4596 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4597 | continue; |
| 4598 | if (error) |
| 4599 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4601 | /* update the extent map to return */ |
| 4602 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | /* |
| 4605 | * If we're done, stop now. Stop when we've allocated |
| 4606 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4607 | * the transaction may get too big. |
| 4608 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4609 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4611 | |
| 4612 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4613 | bma.prev = bma.got; |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4614 | if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got)) |
| 4615 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | /* |
| 4620 | * Transform from btree to extents, give it cur. |
| 4621 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4622 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4623 | int tmp_logflags = 0; |
| 4624 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4625 | ASSERT(bma.cur); |
| 4626 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4628 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | if (error) |
| 4630 | goto error0; |
| 4631 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4634 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4635 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | error0: |
| 4638 | /* |
| 4639 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4640 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4642 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4644 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4645 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4647 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | /* |
| 4649 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4650 | * detecting a case where the data is changed, there's an error, |
| 4651 | * and it's not logged so we don't shutdown when we should. |
| 4652 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4653 | if (bma.logflags) |
| 4654 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4655 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4656 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | if (!error) { |
| 4658 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4659 | XFS_FSB_TO_AGNO(mp, *firstblock) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | XFS_FSB_TO_AGNO(mp, |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4661 | bma.cur->bc_private.b.firstblock)); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4662 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4664 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4666 | } |
| 4667 | if (!error) |
| 4668 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4669 | orig_nmap, *nmap); |
| 4670 | return error; |
| 4671 | } |
| 4672 | |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4673 | static int |
| 4674 | xfs_bmapi_remap( |
| 4675 | struct xfs_trans *tp, |
| 4676 | struct xfs_inode *ip, |
| 4677 | xfs_fileoff_t bno, |
| 4678 | xfs_filblks_t len, |
| 4679 | xfs_fsblock_t startblock, |
| 4680 | struct xfs_defer_ops *dfops) |
| 4681 | { |
| 4682 | struct xfs_mount *mp = ip->i_mount; |
| 4683 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4684 | struct xfs_btree_cur *cur = NULL; |
| 4685 | xfs_fsblock_t firstblock = NULLFSBLOCK; |
| 4686 | struct xfs_bmbt_irec got; |
| 4687 | xfs_extnum_t idx; |
| 4688 | int logflags = 0, error; |
| 4689 | |
| 4690 | ASSERT(len > 0); |
| 4691 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4692 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 4693 | |
| 4694 | if (unlikely(XFS_TEST_ERROR( |
| 4695 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4696 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4697 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4698 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4699 | return -EFSCORRUPTED; |
| 4700 | } |
| 4701 | |
| 4702 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4703 | return -EIO; |
| 4704 | |
| 4705 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4706 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4707 | if (error) |
| 4708 | return error; |
| 4709 | } |
| 4710 | |
| 4711 | if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) { |
| 4712 | /* make sure we only reflink into a hole. */ |
| 4713 | ASSERT(got.br_startoff > bno); |
| 4714 | ASSERT(got.br_startoff - bno >= len); |
| 4715 | } |
| 4716 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4717 | ip->i_d.di_nblocks += len; |
| 4718 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4719 | |
| 4720 | if (ifp->if_flags & XFS_IFBROOT) { |
| 4721 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
| 4722 | cur->bc_private.b.firstblock = firstblock; |
| 4723 | cur->bc_private.b.dfops = dfops; |
| 4724 | cur->bc_private.b.flags = 0; |
| 4725 | } |
| 4726 | |
| 4727 | got.br_startoff = bno; |
| 4728 | got.br_startblock = startblock; |
| 4729 | got.br_blockcount = len; |
| 4730 | got.br_state = XFS_EXT_NORM; |
| 4731 | |
| 4732 | error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur, |
| 4733 | &got, &firstblock, dfops, &logflags); |
| 4734 | if (error) |
| 4735 | goto error0; |
| 4736 | |
| 4737 | if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) { |
| 4738 | int tmp_logflags = 0; |
| 4739 | |
| 4740 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4741 | &tmp_logflags, XFS_DATA_FORK); |
| 4742 | logflags |= tmp_logflags; |
| 4743 | } |
| 4744 | |
| 4745 | error0: |
| 4746 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4747 | logflags &= ~XFS_ILOG_DEXT; |
| 4748 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4749 | logflags &= ~XFS_ILOG_DBROOT; |
| 4750 | |
| 4751 | if (logflags) |
| 4752 | xfs_trans_log_inode(tp, ip, logflags); |
| 4753 | if (cur) { |
| 4754 | xfs_btree_del_cursor(cur, |
| 4755 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4756 | } |
| 4757 | return error; |
| 4758 | } |
| 4759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4761 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4762 | * indlen reservation must be shared across the two new extents that are left |
| 4763 | * behind. |
| 4764 | * |
| 4765 | * Given the original reservation and the worst case indlen for the two new |
| 4766 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4767 | * reservation fairly across the two new extents. If necessary, steal available |
| 4768 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4769 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4770 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4771 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4772 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4773 | xfs_bmap_split_indlen( |
| 4774 | xfs_filblks_t ores, /* original res. */ |
| 4775 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4776 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4777 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4778 | { |
| 4779 | xfs_filblks_t len1 = *indlen1; |
| 4780 | xfs_filblks_t len2 = *indlen2; |
| 4781 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4782 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4783 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4784 | |
| 4785 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4786 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4787 | * indlen for both new extents. |
| 4788 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4789 | if (ores < nres && avail) |
| 4790 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4791 | ores += stolen; |
| 4792 | |
| 4793 | /* nothing else to do if we've satisfied the new reservation */ |
| 4794 | if (ores >= nres) |
| 4795 | return stolen; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4796 | |
| 4797 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4798 | * We can't meet the total required reservation for the two extents. |
| 4799 | * Calculate the percent of the overall shortage between both extents |
| 4800 | * and apply this percentage to each of the requested indlen values. |
| 4801 | * This distributes the shortage fairly and reduces the chances that one |
| 4802 | * of the two extents is left with nothing when extents are repeatedly |
| 4803 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4804 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4805 | resfactor = (ores * 100); |
| 4806 | do_div(resfactor, nres); |
| 4807 | len1 *= resfactor; |
| 4808 | do_div(len1, 100); |
| 4809 | len2 *= resfactor; |
| 4810 | do_div(len2, 100); |
| 4811 | ASSERT(len1 + len2 <= ores); |
| 4812 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4813 | |
| 4814 | /* |
| 4815 | * Hand out the remainder to each extent. If one of the two reservations |
| 4816 | * is zero, we want to make sure that one gets a block first. The loop |
| 4817 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4818 | * is zero. |
| 4819 | */ |
| 4820 | ores -= (len1 + len2); |
| 4821 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4822 | if (ores && !len2 && *indlen2) { |
| 4823 | len2++; |
| 4824 | ores--; |
| 4825 | } |
| 4826 | while (ores) { |
| 4827 | if (len1 < *indlen1) { |
| 4828 | len1++; |
| 4829 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4830 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4831 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4832 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4833 | if (len2 < *indlen2) { |
| 4834 | len2++; |
| 4835 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4836 | } |
| 4837 | } |
| 4838 | |
| 4839 | *indlen1 = len1; |
| 4840 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4841 | |
| 4842 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4843 | } |
| 4844 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4845 | int |
| 4846 | xfs_bmap_del_extent_delay( |
| 4847 | struct xfs_inode *ip, |
| 4848 | int whichfork, |
| 4849 | xfs_extnum_t *idx, |
| 4850 | struct xfs_bmbt_irec *got, |
| 4851 | struct xfs_bmbt_irec *del) |
| 4852 | { |
| 4853 | struct xfs_mount *mp = ip->i_mount; |
| 4854 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4855 | struct xfs_bmbt_irec new; |
| 4856 | int64_t da_old, da_new, da_diff = 0; |
| 4857 | xfs_fileoff_t del_endoff, got_endoff; |
| 4858 | xfs_filblks_t got_indlen, new_indlen, stolen; |
| 4859 | int error = 0, state = 0; |
| 4860 | bool isrt; |
| 4861 | |
| 4862 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4863 | |
| 4864 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4865 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4866 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4867 | da_old = startblockval(got->br_startblock); |
| 4868 | da_new = 0; |
| 4869 | |
| 4870 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 4871 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4872 | ASSERT(del->br_blockcount > 0); |
| 4873 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4874 | ASSERT(got_endoff >= del_endoff); |
| 4875 | |
| 4876 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4877 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4878 | |
| 4879 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4880 | xfs_mod_frextents(mp, rtexts); |
| 4881 | } |
| 4882 | |
| 4883 | /* |
| 4884 | * Update the inode delalloc counter now and wait to update the |
| 4885 | * sb counters as we might have to borrow some blocks for the |
| 4886 | * indirect block accounting. |
| 4887 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4888 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4889 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4890 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4891 | if (error) |
| 4892 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4893 | ip->i_delayed_blks -= del->br_blockcount; |
| 4894 | |
| 4895 | if (whichfork == XFS_COW_FORK) |
| 4896 | state |= BMAP_COWFORK; |
| 4897 | |
| 4898 | if (got->br_startoff == del->br_startoff) |
| 4899 | state |= BMAP_LEFT_CONTIG; |
| 4900 | if (got_endoff == del_endoff) |
| 4901 | state |= BMAP_RIGHT_CONTIG; |
| 4902 | |
| 4903 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 4904 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 4905 | /* |
| 4906 | * Matches the whole extent. Delete the entry. |
| 4907 | */ |
| 4908 | xfs_iext_remove(ip, *idx, 1, state); |
| 4909 | --*idx; |
| 4910 | break; |
| 4911 | case BMAP_LEFT_CONTIG: |
| 4912 | /* |
| 4913 | * Deleting the first part of the extent. |
| 4914 | */ |
| 4915 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4916 | got->br_startoff = del_endoff; |
| 4917 | got->br_blockcount -= del->br_blockcount; |
| 4918 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4919 | got->br_blockcount), da_old); |
| 4920 | got->br_startblock = nullstartblock((int)da_new); |
| 4921 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4922 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4923 | break; |
| 4924 | case BMAP_RIGHT_CONTIG: |
| 4925 | /* |
| 4926 | * Deleting the last part of the extent. |
| 4927 | */ |
| 4928 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4929 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4930 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4931 | got->br_blockcount), da_old); |
| 4932 | got->br_startblock = nullstartblock((int)da_new); |
| 4933 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4934 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4935 | break; |
| 4936 | case 0: |
| 4937 | /* |
| 4938 | * Deleting the middle of the extent. |
| 4939 | * |
| 4940 | * Distribute the original indlen reservation across the two new |
| 4941 | * extents. Steal blocks from the deleted extent if necessary. |
| 4942 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4943 | * Warn if either of the new indlen reservations is zero as this |
| 4944 | * can lead to delalloc problems. |
| 4945 | */ |
| 4946 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4947 | |
| 4948 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4949 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4950 | |
| 4951 | new.br_blockcount = got_endoff - del_endoff; |
| 4952 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4953 | |
| 4954 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4955 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4956 | del->br_blockcount); |
| 4957 | |
| 4958 | got->br_startblock = nullstartblock((int)got_indlen); |
| 4959 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4960 | trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_); |
| 4961 | |
| 4962 | new.br_startoff = del_endoff; |
| 4963 | new.br_state = got->br_state; |
| 4964 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4965 | |
| 4966 | ++*idx; |
| 4967 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 4968 | |
| 4969 | da_new = got_indlen + new_indlen - stolen; |
| 4970 | del->br_blockcount -= stolen; |
| 4971 | break; |
| 4972 | } |
| 4973 | |
| 4974 | ASSERT(da_old >= da_new); |
| 4975 | da_diff = da_old - da_new; |
| 4976 | if (!isrt) |
| 4977 | da_diff += del->br_blockcount; |
| 4978 | if (da_diff) |
| 4979 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4980 | return error; |
| 4981 | } |
| 4982 | |
| 4983 | void |
| 4984 | xfs_bmap_del_extent_cow( |
| 4985 | struct xfs_inode *ip, |
| 4986 | xfs_extnum_t *idx, |
| 4987 | struct xfs_bmbt_irec *got, |
| 4988 | struct xfs_bmbt_irec *del) |
| 4989 | { |
| 4990 | struct xfs_mount *mp = ip->i_mount; |
| 4991 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4992 | struct xfs_bmbt_irec new; |
| 4993 | xfs_fileoff_t del_endoff, got_endoff; |
| 4994 | int state = BMAP_COWFORK; |
| 4995 | |
| 4996 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4997 | |
| 4998 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4999 | got_endoff = got->br_startoff + got->br_blockcount; |
| 5000 | |
| 5001 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5002 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 5003 | ASSERT(del->br_blockcount > 0); |
| 5004 | ASSERT(got->br_startoff <= del->br_startoff); |
| 5005 | ASSERT(got_endoff >= del_endoff); |
| 5006 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 5007 | |
| 5008 | if (got->br_startoff == del->br_startoff) |
| 5009 | state |= BMAP_LEFT_CONTIG; |
| 5010 | if (got_endoff == del_endoff) |
| 5011 | state |= BMAP_RIGHT_CONTIG; |
| 5012 | |
| 5013 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 5014 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 5015 | /* |
| 5016 | * Matches the whole extent. Delete the entry. |
| 5017 | */ |
| 5018 | xfs_iext_remove(ip, *idx, 1, state); |
| 5019 | --*idx; |
| 5020 | break; |
| 5021 | case BMAP_LEFT_CONTIG: |
| 5022 | /* |
| 5023 | * Deleting the first part of the extent. |
| 5024 | */ |
| 5025 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5026 | got->br_startoff = del_endoff; |
| 5027 | got->br_blockcount -= del->br_blockcount; |
| 5028 | got->br_startblock = del->br_startblock + del->br_blockcount; |
| 5029 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5030 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5031 | break; |
| 5032 | case BMAP_RIGHT_CONTIG: |
| 5033 | /* |
| 5034 | * Deleting the last part of the extent. |
| 5035 | */ |
| 5036 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5037 | got->br_blockcount -= del->br_blockcount; |
| 5038 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5039 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5040 | break; |
| 5041 | case 0: |
| 5042 | /* |
| 5043 | * Deleting the middle of the extent. |
| 5044 | */ |
| 5045 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5046 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 5047 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5048 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5049 | |
| 5050 | new.br_startoff = del_endoff; |
| 5051 | new.br_blockcount = got_endoff - del_endoff; |
| 5052 | new.br_state = got->br_state; |
| 5053 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 5054 | |
| 5055 | ++*idx; |
| 5056 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 5057 | break; |
| 5058 | } |
| 5059 | } |
| 5060 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5061 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5062 | * Called by xfs_bmapi to update file extent records and the btree |
| 5063 | * after removing space (or undoing a delayed allocation). |
| 5064 | */ |
| 5065 | STATIC int /* error */ |
| 5066 | xfs_bmap_del_extent( |
| 5067 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5068 | xfs_trans_t *tp, /* current transaction pointer */ |
| 5069 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5070 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5071 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 5072 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 5073 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5074 | int whichfork, /* data or attr fork */ |
| 5075 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5076 | { |
| 5077 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 5078 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 5079 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 5080 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 5081 | int delay; /* current block is delayed allocated */ |
| 5082 | int do_fx; /* free extent at end of routine */ |
| 5083 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
| 5084 | int error; /* error return value */ |
| 5085 | int flags; /* inode logging flags */ |
| 5086 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 5087 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 5088 | int i; /* temp state */ |
| 5089 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5090 | xfs_mount_t *mp; /* mount structure */ |
| 5091 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 5092 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 5093 | /* REFERENCED */ |
| 5094 | uint qfield; /* quota field to update */ |
| 5095 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 5096 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 5097 | int state = 0; |
| 5098 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5099 | mp = ip->i_mount; |
| 5100 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5101 | |
| 5102 | if (whichfork == XFS_ATTR_FORK) |
| 5103 | state |= BMAP_ATTRFORK; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5104 | else if (whichfork == XFS_COW_FORK) |
| 5105 | state |= BMAP_COWFORK; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5106 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5107 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5108 | ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp))); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5109 | ASSERT(del->br_blockcount > 0); |
| 5110 | ep = xfs_iext_get_ext(ifp, *idx); |
| 5111 | xfs_bmbt_get_all(ep, &got); |
| 5112 | ASSERT(got.br_startoff <= del->br_startoff); |
| 5113 | del_endoff = del->br_startoff + del->br_blockcount; |
| 5114 | got_endoff = got.br_startoff + got.br_blockcount; |
| 5115 | ASSERT(got_endoff >= del_endoff); |
| 5116 | delay = isnullstartblock(got.br_startblock); |
| 5117 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
| 5118 | flags = 0; |
| 5119 | qfield = 0; |
| 5120 | error = 0; |
| 5121 | /* |
| 5122 | * If deleting a real allocation, must free up the disk space. |
| 5123 | */ |
| 5124 | if (!delay) { |
| 5125 | flags = XFS_ILOG_CORE; |
| 5126 | /* |
| 5127 | * Realtime allocation. Free it and record di_nblocks update. |
| 5128 | */ |
| 5129 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 5130 | xfs_fsblock_t bno; |
| 5131 | xfs_filblks_t len; |
| 5132 | |
| 5133 | ASSERT(do_mod(del->br_blockcount, |
| 5134 | mp->m_sb.sb_rextsize) == 0); |
| 5135 | ASSERT(do_mod(del->br_startblock, |
| 5136 | mp->m_sb.sb_rextsize) == 0); |
| 5137 | bno = del->br_startblock; |
| 5138 | len = del->br_blockcount; |
| 5139 | do_div(bno, mp->m_sb.sb_rextsize); |
| 5140 | do_div(len, mp->m_sb.sb_rextsize); |
| 5141 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 5142 | if (error) |
| 5143 | goto done; |
| 5144 | do_fx = 0; |
| 5145 | nblks = len * mp->m_sb.sb_rextsize; |
| 5146 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 5147 | } |
| 5148 | /* |
| 5149 | * Ordinary allocation. |
| 5150 | */ |
| 5151 | else { |
| 5152 | do_fx = 1; |
| 5153 | nblks = del->br_blockcount; |
| 5154 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 5155 | } |
| 5156 | /* |
| 5157 | * Set up del_endblock and cur for later. |
| 5158 | */ |
| 5159 | del_endblock = del->br_startblock + del->br_blockcount; |
| 5160 | if (cur) { |
| 5161 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 5162 | got.br_startblock, got.br_blockcount, |
| 5163 | &i))) |
| 5164 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5165 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5166 | } |
| 5167 | da_old = da_new = 0; |
| 5168 | } else { |
| 5169 | da_old = startblockval(got.br_startblock); |
| 5170 | da_new = 0; |
| 5171 | nblks = 0; |
| 5172 | do_fx = 0; |
| 5173 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 5174 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5175 | /* |
| 5176 | * Set flag value to use in switch statement. |
| 5177 | * Left-contig is 2, right-contig is 1. |
| 5178 | */ |
| 5179 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 5180 | (got_endoff == del_endoff)) { |
| 5181 | case 3: |
| 5182 | /* |
| 5183 | * Matches the whole extent. Delete the entry. |
| 5184 | */ |
Darrick J. Wong | 4862cfe | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 5185 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5186 | xfs_iext_remove(ip, *idx, 1, |
| 5187 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 5188 | --*idx; |
| 5189 | if (delay) |
| 5190 | break; |
| 5191 | |
| 5192 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5193 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5194 | flags |= XFS_ILOG_CORE; |
| 5195 | if (!cur) { |
| 5196 | flags |= xfs_ilog_fext(whichfork); |
| 5197 | break; |
| 5198 | } |
| 5199 | if ((error = xfs_btree_delete(cur, &i))) |
| 5200 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5201 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5202 | break; |
| 5203 | |
| 5204 | case 2: |
| 5205 | /* |
| 5206 | * Deleting the first part of the extent. |
| 5207 | */ |
| 5208 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5209 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 5210 | temp = got.br_blockcount - del->br_blockcount; |
| 5211 | xfs_bmbt_set_blockcount(ep, temp); |
| 5212 | if (delay) { |
| 5213 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 5214 | da_old); |
| 5215 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 5216 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5217 | da_new = temp; |
| 5218 | break; |
| 5219 | } |
| 5220 | xfs_bmbt_set_startblock(ep, del_endblock); |
| 5221 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5222 | if (!cur) { |
| 5223 | flags |= xfs_ilog_fext(whichfork); |
| 5224 | break; |
| 5225 | } |
| 5226 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 5227 | got.br_blockcount - del->br_blockcount, |
| 5228 | got.br_state))) |
| 5229 | goto done; |
| 5230 | break; |
| 5231 | |
| 5232 | case 1: |
| 5233 | /* |
| 5234 | * Deleting the last part of the extent. |
| 5235 | */ |
| 5236 | temp = got.br_blockcount - del->br_blockcount; |
| 5237 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5238 | xfs_bmbt_set_blockcount(ep, temp); |
| 5239 | if (delay) { |
| 5240 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 5241 | da_old); |
| 5242 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 5243 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5244 | da_new = temp; |
| 5245 | break; |
| 5246 | } |
| 5247 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5248 | if (!cur) { |
| 5249 | flags |= xfs_ilog_fext(whichfork); |
| 5250 | break; |
| 5251 | } |
| 5252 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 5253 | got.br_startblock, |
| 5254 | got.br_blockcount - del->br_blockcount, |
| 5255 | got.br_state))) |
| 5256 | goto done; |
| 5257 | break; |
| 5258 | |
| 5259 | case 0: |
| 5260 | /* |
| 5261 | * Deleting the middle of the extent. |
| 5262 | */ |
| 5263 | temp = del->br_startoff - got.br_startoff; |
| 5264 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5265 | xfs_bmbt_set_blockcount(ep, temp); |
| 5266 | new.br_startoff = del_endoff; |
| 5267 | temp2 = got_endoff - del_endoff; |
| 5268 | new.br_blockcount = temp2; |
| 5269 | new.br_state = got.br_state; |
| 5270 | if (!delay) { |
| 5271 | new.br_startblock = del_endblock; |
| 5272 | flags |= XFS_ILOG_CORE; |
| 5273 | if (cur) { |
| 5274 | if ((error = xfs_bmbt_update(cur, |
| 5275 | got.br_startoff, |
| 5276 | got.br_startblock, temp, |
| 5277 | got.br_state))) |
| 5278 | goto done; |
| 5279 | if ((error = xfs_btree_increment(cur, 0, &i))) |
| 5280 | goto done; |
| 5281 | cur->bc_rec.b = new; |
| 5282 | error = xfs_btree_insert(cur, &i); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5283 | if (error && error != -ENOSPC) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5284 | goto done; |
| 5285 | /* |
| 5286 | * If get no-space back from btree insert, |
| 5287 | * it tried a split, and we have a zero |
| 5288 | * block reservation. |
| 5289 | * Fix up our state and return the error. |
| 5290 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5291 | if (error == -ENOSPC) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5292 | /* |
| 5293 | * Reset the cursor, don't trust |
| 5294 | * it after any insert operation. |
| 5295 | */ |
| 5296 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 5297 | got.br_startoff, |
| 5298 | got.br_startblock, |
| 5299 | temp, &i))) |
| 5300 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5301 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 5302 | i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5303 | /* |
| 5304 | * Update the btree record back |
| 5305 | * to the original value. |
| 5306 | */ |
| 5307 | if ((error = xfs_bmbt_update(cur, |
| 5308 | got.br_startoff, |
| 5309 | got.br_startblock, |
| 5310 | got.br_blockcount, |
| 5311 | got.br_state))) |
| 5312 | goto done; |
| 5313 | /* |
| 5314 | * Reset the extent record back |
| 5315 | * to the original value. |
| 5316 | */ |
| 5317 | xfs_bmbt_set_blockcount(ep, |
| 5318 | got.br_blockcount); |
| 5319 | flags = 0; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5320 | error = -ENOSPC; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5321 | goto done; |
| 5322 | } |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5323 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5324 | } else |
| 5325 | flags |= xfs_ilog_fext(whichfork); |
| 5326 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5327 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5328 | } else { |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5329 | xfs_filblks_t stolen; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5330 | ASSERT(whichfork == XFS_DATA_FORK); |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5331 | |
| 5332 | /* |
| 5333 | * Distribute the original indlen reservation across the |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5334 | * two new extents. Steal blocks from the deleted extent |
| 5335 | * if necessary. Stealing blocks simply fudges the |
| 5336 | * fdblocks accounting in xfs_bunmapi(). |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5337 | */ |
| 5338 | temp = xfs_bmap_worst_indlen(ip, got.br_blockcount); |
| 5339 | temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5340 | stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2, |
| 5341 | del->br_blockcount); |
| 5342 | da_new = temp + temp2 - stolen; |
| 5343 | del->br_blockcount -= stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5344 | |
| 5345 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5346 | * Set the reservation for each extent. Warn if either |
| 5347 | * is zero as this can lead to delalloc problems. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5348 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5349 | WARN_ON_ONCE(!temp || !temp2); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5350 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5351 | new.br_startblock = nullstartblock((int)temp2); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5352 | } |
| 5353 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5354 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 5355 | ++*idx; |
| 5356 | break; |
| 5357 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5358 | |
| 5359 | /* remove reverse mapping */ |
| 5360 | if (!delay) { |
| 5361 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5362 | if (error) |
| 5363 | goto done; |
| 5364 | } |
| 5365 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5366 | /* |
| 5367 | * If we need to, add to list of extents to delete. |
| 5368 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5369 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5370 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5371 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5372 | if (error) |
| 5373 | goto done; |
| 5374 | } else |
| 5375 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5376 | del->br_blockcount, NULL); |
| 5377 | } |
| 5378 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5379 | /* |
| 5380 | * Adjust inode # blocks in the file. |
| 5381 | */ |
| 5382 | if (nblks) |
| 5383 | ip->i_d.di_nblocks -= nblks; |
| 5384 | /* |
| 5385 | * Adjust quota data. |
| 5386 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5387 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5388 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5389 | |
| 5390 | /* |
| 5391 | * Account for change in delayed indirect blocks. |
| 5392 | * Nothing to do for disk quota accounting here. |
| 5393 | */ |
| 5394 | ASSERT(da_old >= da_new); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 5395 | if (da_old > da_new) |
| 5396 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5397 | done: |
| 5398 | *logflagsp = flags; |
| 5399 | return error; |
| 5400 | } |
| 5401 | |
| 5402 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5403 | * Unmap (remove) blocks from a file. |
| 5404 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5405 | * that value. If not all extents in the block range can be removed then |
| 5406 | * *done is set. |
| 5407 | */ |
| 5408 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5409 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | xfs_trans_t *tp, /* transaction pointer */ |
| 5411 | struct xfs_inode *ip, /* incore inode */ |
| 5412 | xfs_fileoff_t bno, /* starting offset to unmap */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5413 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | int flags, /* misc flags */ |
| 5415 | xfs_extnum_t nexts, /* number of extents max */ |
| 5416 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5417 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5418 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | { |
| 5420 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5421 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | int error; /* error return value */ |
| 5423 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5424 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5425 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5426 | int isrt; /* freeing in rt area */ |
| 5427 | xfs_extnum_t lastx; /* last extent index used */ |
| 5428 | int logflags; /* transaction logging flags */ |
| 5429 | xfs_extlen_t mod; /* rt extent offset */ |
| 5430 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5432 | int tmp_logflags; /* partial logging flags */ |
| 5433 | int wasdel; /* was a delayed alloc extent */ |
| 5434 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5435 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5436 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5437 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame^] | 5438 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5440 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5441 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5442 | whichfork = xfs_bmapi_whichfork(flags); |
| 5443 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5445 | if (unlikely( |
| 5446 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5447 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5448 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5449 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5450 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | } |
| 5452 | mp = ip->i_mount; |
| 5453 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5454 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5455 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5456 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | ASSERT(len > 0); |
| 5458 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5459 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5460 | /* |
| 5461 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5462 | * blowing out the transaction with a mix of EFIs and reflink |
| 5463 | * adjustments. |
| 5464 | */ |
| 5465 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
| 5466 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5467 | else |
| 5468 | max_len = len; |
| 5469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5471 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5472 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5473 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5474 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | return 0; |
| 5476 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5477 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5478 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | start = bno; |
| 5480 | bno = start + len - 1; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5482 | /* |
| 5483 | * Check to see if the given block number is past the end of the |
| 5484 | * file, back up to the last block if so... |
| 5485 | */ |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5486 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) { |
| 5487 | ASSERT(lastx > 0); |
| 5488 | xfs_iext_get_extent(ifp, --lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5490 | } |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | logflags = 0; |
| 5493 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5494 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5495 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5497 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5498 | cur->bc_private.b.flags = 0; |
| 5499 | } else |
| 5500 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5501 | |
| 5502 | if (isrt) { |
| 5503 | /* |
| 5504 | * Synchronize by locking the bitmap inode. |
| 5505 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5506 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5507 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5508 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5509 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5512 | extno = 0; |
| 5513 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5514 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | /* |
| 5516 | * Is the found extent after a hole in which bno lives? |
| 5517 | * Just back up to the previous extent, if so. |
| 5518 | */ |
| 5519 | if (got.br_startoff > bno) { |
| 5520 | if (--lastx < 0) |
| 5521 | break; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5522 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | } |
| 5524 | /* |
| 5525 | * Is the last block of this extent before the range |
| 5526 | * we're supposed to delete? If so, we're done. |
| 5527 | */ |
| 5528 | bno = XFS_FILEOFF_MIN(bno, |
| 5529 | got.br_startoff + got.br_blockcount - 1); |
| 5530 | if (bno < start) |
| 5531 | break; |
| 5532 | /* |
| 5533 | * Then deal with the (possibly delayed) allocated space |
| 5534 | * we found. |
| 5535 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5537 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame^] | 5538 | |
| 5539 | /* |
| 5540 | * Make sure we don't touch multiple AGF headers out of order |
| 5541 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5542 | */ |
| 5543 | if (!wasdel) { |
| 5544 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5545 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5546 | break; |
| 5547 | prev_agno = agno; |
| 5548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | if (got.br_startoff < start) { |
| 5550 | del.br_startoff = start; |
| 5551 | del.br_blockcount -= start - got.br_startoff; |
| 5552 | if (!wasdel) |
| 5553 | del.br_startblock += start - got.br_startoff; |
| 5554 | } |
| 5555 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5556 | del.br_blockcount = bno + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5557 | |
| 5558 | /* How much can we safely unmap? */ |
| 5559 | if (max_len < del.br_blockcount) { |
| 5560 | del.br_startoff += del.br_blockcount - max_len; |
| 5561 | if (!wasdel) |
| 5562 | del.br_startblock += del.br_blockcount - max_len; |
| 5563 | del.br_blockcount = max_len; |
| 5564 | } |
| 5565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | sum = del.br_startblock + del.br_blockcount; |
| 5567 | if (isrt && |
| 5568 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5569 | /* |
| 5570 | * Realtime extent not lined up at the end. |
| 5571 | * The extent could have been split into written |
| 5572 | * and unwritten pieces, or we could just be |
| 5573 | * unmapping part of it. But we can't really |
| 5574 | * get rid of part of a realtime extent. |
| 5575 | */ |
| 5576 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5577 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | /* |
| 5579 | * This piece is unwritten, or we're not |
| 5580 | * using unwritten extents. Skip over it. |
| 5581 | */ |
| 5582 | ASSERT(bno >= mod); |
| 5583 | bno -= mod > del.br_blockcount ? |
| 5584 | del.br_blockcount : mod; |
| 5585 | if (bno < got.br_startoff) { |
| 5586 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5587 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5588 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | } |
| 5590 | continue; |
| 5591 | } |
| 5592 | /* |
| 5593 | * It's written, turn it unwritten. |
| 5594 | * This is better than zeroing it. |
| 5595 | */ |
| 5596 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5597 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | /* |
| 5599 | * If this spans a realtime extent boundary, |
| 5600 | * chop it back to the start of the one we end at. |
| 5601 | */ |
| 5602 | if (del.br_blockcount > mod) { |
| 5603 | del.br_startoff += del.br_blockcount - mod; |
| 5604 | del.br_startblock += del.br_blockcount - mod; |
| 5605 | del.br_blockcount = mod; |
| 5606 | } |
| 5607 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5608 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5609 | whichfork, &lastx, &cur, &del, |
| 5610 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | if (error) |
| 5612 | goto error0; |
| 5613 | goto nodelete; |
| 5614 | } |
| 5615 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5616 | /* |
| 5617 | * Realtime extent is lined up at the end but not |
| 5618 | * at the front. We'll get rid of full extents if |
| 5619 | * we can. |
| 5620 | */ |
| 5621 | mod = mp->m_sb.sb_rextsize - mod; |
| 5622 | if (del.br_blockcount > mod) { |
| 5623 | del.br_blockcount -= mod; |
| 5624 | del.br_startoff += mod; |
| 5625 | del.br_startblock += mod; |
| 5626 | } else if ((del.br_startoff == start && |
| 5627 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5628 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5629 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5630 | /* |
| 5631 | * Can't make it unwritten. There isn't |
| 5632 | * a full extent here so just skip it. |
| 5633 | */ |
| 5634 | ASSERT(bno >= del.br_blockcount); |
| 5635 | bno -= del.br_blockcount; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5636 | if (got.br_startoff > bno && --lastx >= 0) |
| 5637 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | continue; |
| 5639 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5640 | struct xfs_bmbt_irec prev; |
| 5641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | /* |
| 5643 | * This one is already unwritten. |
| 5644 | * It must have a written left neighbor. |
| 5645 | * Unwrite the killed part of that one and |
| 5646 | * try again. |
| 5647 | */ |
| 5648 | ASSERT(lastx > 0); |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5649 | xfs_iext_get_extent(ifp, lastx - 1, &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5651 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | ASSERT(del.br_startblock == |
| 5653 | prev.br_startblock + prev.br_blockcount); |
| 5654 | if (prev.br_startoff < start) { |
| 5655 | mod = start - prev.br_startoff; |
| 5656 | prev.br_blockcount -= mod; |
| 5657 | prev.br_startblock += mod; |
| 5658 | prev.br_startoff = start; |
| 5659 | } |
| 5660 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5661 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5662 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5663 | ip, whichfork, &lastx, &cur, |
| 5664 | &prev, firstblock, dfops, |
| 5665 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | if (error) |
| 5667 | goto error0; |
| 5668 | goto nodelete; |
| 5669 | } else { |
| 5670 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5671 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5672 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5673 | ip, whichfork, &lastx, &cur, |
| 5674 | &del, firstblock, dfops, |
| 5675 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | if (error) |
| 5677 | goto error0; |
| 5678 | goto nodelete; |
| 5679 | } |
| 5680 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | /* |
| 5683 | * If it's the case where the directory code is running |
| 5684 | * with no block reservation, and the deleted block is in |
| 5685 | * the middle of its extent, and the resulting insert |
| 5686 | * of an extent would cause transformation to btree format, |
| 5687 | * then reject it. The calling code will then swap |
| 5688 | * blocks around instead. |
| 5689 | * We have to do this now, rather than waiting for the |
| 5690 | * conversion to btree format, since the transaction |
| 5691 | * will be dirty. |
| 5692 | */ |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5693 | if (!wasdel && tp->t_blk_res == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5695 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ |
| 5696 | XFS_IFORK_MAXEXT(ip, whichfork) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | del.br_startoff > got.br_startoff && |
| 5698 | del.br_startoff + del.br_blockcount < |
| 5699 | got.br_startoff + got.br_blockcount) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5700 | error = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | goto error0; |
| 5702 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5703 | |
| 5704 | /* |
| 5705 | * Unreserve quota and update realtime free space, if |
| 5706 | * appropriate. If delayed allocation, update the inode delalloc |
| 5707 | * counter now and wait to update the sb counters as |
| 5708 | * xfs_bmap_del_extent() might need to borrow some blocks. |
| 5709 | */ |
| 5710 | if (wasdel) { |
| 5711 | ASSERT(startblockval(del.br_startblock) > 0); |
| 5712 | if (isrt) { |
| 5713 | xfs_filblks_t rtexts; |
| 5714 | |
| 5715 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5716 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5717 | xfs_mod_frextents(mp, (int64_t)rtexts); |
| 5718 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5719 | ip, -((long)del.br_blockcount), 0, |
| 5720 | XFS_QMOPT_RES_RTBLKS); |
| 5721 | } else { |
| 5722 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5723 | ip, -((long)del.br_blockcount), 0, |
| 5724 | XFS_QMOPT_RES_REGBLKS); |
| 5725 | } |
| 5726 | ip->i_delayed_blks -= del.br_blockcount; |
| 5727 | if (cur) |
| 5728 | cur->bc_private.b.flags |= |
| 5729 | XFS_BTCUR_BPRV_WASDEL; |
| 5730 | } else if (cur) |
| 5731 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5732 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5733 | error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del, |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5734 | &tmp_logflags, whichfork, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | logflags |= tmp_logflags; |
| 5736 | if (error) |
| 5737 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5738 | |
| 5739 | if (!isrt && wasdel) |
| 5740 | xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false); |
| 5741 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5742 | max_len -= del.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | bno = del.br_startoff - 1; |
| 5744 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | /* |
| 5746 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5749 | if (lastx >= 0) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5750 | xfs_iext_get_extent(ifp, lastx, &got); |
| 5751 | if (got.br_startoff > bno && --lastx >= 0) |
| 5752 | xfs_iext_get_extent(ifp, lastx, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5754 | extno++; |
| 5755 | } |
| 5756 | } |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5757 | if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0) |
| 5758 | *rlen = 0; |
| 5759 | else |
| 5760 | *rlen = bno - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | /* |
| 5763 | * Convert to a btree if necessary. |
| 5764 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5765 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5767 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | &cur, 0, &tmp_logflags, whichfork); |
| 5769 | logflags |= tmp_logflags; |
| 5770 | if (error) |
| 5771 | goto error0; |
| 5772 | } |
| 5773 | /* |
| 5774 | * transform from btree to extents, give it cur |
| 5775 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5776 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | ASSERT(cur != NULL); |
| 5778 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5779 | whichfork); |
| 5780 | logflags |= tmp_logflags; |
| 5781 | if (error) |
| 5782 | goto error0; |
| 5783 | } |
| 5784 | /* |
| 5785 | * transform from extents to local? |
| 5786 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5787 | error = 0; |
| 5788 | error0: |
| 5789 | /* |
| 5790 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5791 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5793 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5795 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5796 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5798 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | /* |
| 5800 | * Log inode even in the error case, if the transaction |
| 5801 | * is dirty we'll need to shut down the filesystem. |
| 5802 | */ |
| 5803 | if (logflags) |
| 5804 | xfs_trans_log_inode(tp, ip, logflags); |
| 5805 | if (cur) { |
| 5806 | if (!error) { |
| 5807 | *firstblock = cur->bc_private.b.firstblock; |
| 5808 | cur->bc_private.b.allocated = 0; |
| 5809 | } |
| 5810 | xfs_btree_del_cursor(cur, |
| 5811 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5812 | } |
| 5813 | return error; |
| 5814 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5815 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5816 | /* Unmap a range of a file. */ |
| 5817 | int |
| 5818 | xfs_bunmapi( |
| 5819 | xfs_trans_t *tp, |
| 5820 | struct xfs_inode *ip, |
| 5821 | xfs_fileoff_t bno, |
| 5822 | xfs_filblks_t len, |
| 5823 | int flags, |
| 5824 | xfs_extnum_t nexts, |
| 5825 | xfs_fsblock_t *firstblock, |
| 5826 | struct xfs_defer_ops *dfops, |
| 5827 | int *done) |
| 5828 | { |
| 5829 | int error; |
| 5830 | |
| 5831 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5832 | dfops); |
| 5833 | *done = (len == 0); |
| 5834 | return error; |
| 5835 | } |
| 5836 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5837 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5838 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5839 | * extent that precedes the target hole of the shift. |
| 5840 | */ |
| 5841 | STATIC bool |
| 5842 | xfs_bmse_can_merge( |
| 5843 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5844 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5845 | xfs_fileoff_t shift) /* shift fsb */ |
| 5846 | { |
| 5847 | xfs_fileoff_t startoff; |
| 5848 | |
| 5849 | startoff = got->br_startoff - shift; |
| 5850 | |
| 5851 | /* |
| 5852 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5853 | * the preceding extent. |
| 5854 | */ |
| 5855 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5856 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5857 | (left->br_state != got->br_state) || |
| 5858 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5859 | return false; |
| 5860 | |
| 5861 | return true; |
| 5862 | } |
| 5863 | |
| 5864 | /* |
| 5865 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5866 | * hole in the file. If an extent shift would result in the extent being fully |
| 5867 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5868 | * the preceding extent rather than do the shift. |
| 5869 | * |
| 5870 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5871 | * with the provided extents via xfs_bmse_can_merge(). |
| 5872 | */ |
| 5873 | STATIC int |
| 5874 | xfs_bmse_merge( |
| 5875 | struct xfs_inode *ip, |
| 5876 | int whichfork, |
| 5877 | xfs_fileoff_t shift, /* shift fsb */ |
| 5878 | int current_ext, /* idx of gotp */ |
| 5879 | struct xfs_bmbt_rec_host *gotp, /* extent to shift */ |
| 5880 | struct xfs_bmbt_rec_host *leftp, /* preceding extent */ |
| 5881 | struct xfs_btree_cur *cur, |
| 5882 | int *logflags) /* output */ |
| 5883 | { |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5884 | struct xfs_bmbt_irec got; |
| 5885 | struct xfs_bmbt_irec left; |
| 5886 | xfs_filblks_t blockcount; |
| 5887 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5888 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5889 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5890 | xfs_bmbt_get_all(gotp, &got); |
| 5891 | xfs_bmbt_get_all(leftp, &left); |
| 5892 | blockcount = left.br_blockcount + got.br_blockcount; |
| 5893 | |
| 5894 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5895 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 5896 | ASSERT(xfs_bmse_can_merge(&left, &got, shift)); |
| 5897 | |
| 5898 | /* |
| 5899 | * Merge the in-core extents. Note that the host record pointers and |
| 5900 | * current_ext index are invalid once the extent has been removed via |
| 5901 | * xfs_iext_remove(). |
| 5902 | */ |
| 5903 | xfs_bmbt_set_blockcount(leftp, blockcount); |
| 5904 | xfs_iext_remove(ip, current_ext, 1, 0); |
| 5905 | |
| 5906 | /* |
| 5907 | * Update the on-disk extent count, the btree if necessary and log the |
| 5908 | * inode. |
| 5909 | */ |
| 5910 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5911 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5912 | *logflags |= XFS_ILOG_CORE; |
| 5913 | if (!cur) { |
| 5914 | *logflags |= XFS_ILOG_DEXT; |
| 5915 | return 0; |
| 5916 | } |
| 5917 | |
| 5918 | /* lookup and remove the extent to merge */ |
| 5919 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5920 | got.br_blockcount, &i); |
| 5921 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5922 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5923 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5924 | |
| 5925 | error = xfs_btree_delete(cur, &i); |
| 5926 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5927 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5928 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5929 | |
| 5930 | /* lookup and update size of the previous extent */ |
| 5931 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock, |
| 5932 | left.br_blockcount, &i); |
| 5933 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5934 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5935 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5936 | |
| 5937 | left.br_blockcount = blockcount; |
| 5938 | |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5939 | return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock, |
| 5940 | left.br_blockcount, left.br_state); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5941 | } |
| 5942 | |
| 5943 | /* |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5944 | * Shift a single extent. |
| 5945 | */ |
| 5946 | STATIC int |
| 5947 | xfs_bmse_shift_one( |
| 5948 | struct xfs_inode *ip, |
| 5949 | int whichfork, |
| 5950 | xfs_fileoff_t offset_shift_fsb, |
| 5951 | int *current_ext, |
| 5952 | struct xfs_bmbt_rec_host *gotp, |
| 5953 | struct xfs_btree_cur *cur, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5954 | int *logflags, |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5955 | enum shift_direction direction, |
| 5956 | struct xfs_defer_ops *dfops) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5957 | { |
| 5958 | struct xfs_ifork *ifp; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5959 | struct xfs_mount *mp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5960 | xfs_fileoff_t startoff; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5961 | struct xfs_bmbt_rec_host *adj_irecp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5962 | struct xfs_bmbt_irec got; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5963 | struct xfs_bmbt_irec adj_irec; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5964 | int error; |
| 5965 | int i; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5966 | int total_extents; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5967 | |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5968 | mp = ip->i_mount; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5969 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5970 | total_extents = xfs_iext_count(ifp); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5971 | |
| 5972 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5973 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5974 | /* delalloc extents should be prevented by caller */ |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5975 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5976 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5977 | if (direction == SHIFT_LEFT) { |
| 5978 | startoff = got.br_startoff - offset_shift_fsb; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5979 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5980 | /* |
| 5981 | * Check for merge if we've got an extent to the left, |
| 5982 | * otherwise make sure there's enough room at the start |
| 5983 | * of the file for the shift. |
| 5984 | */ |
| 5985 | if (!*current_ext) { |
| 5986 | if (got.br_startoff < offset_shift_fsb) |
| 5987 | return -EINVAL; |
| 5988 | goto update_current_ext; |
| 5989 | } |
| 5990 | /* |
| 5991 | * grab the left extent and check for a large |
| 5992 | * enough hole. |
| 5993 | */ |
| 5994 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1); |
| 5995 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5996 | |
| 5997 | if (startoff < |
| 5998 | adj_irec.br_startoff + adj_irec.br_blockcount) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5999 | return -EINVAL; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6000 | |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 6001 | /* check whether to merge the extent or shift it down */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6002 | if (xfs_bmse_can_merge(&adj_irec, &got, |
| 6003 | offset_shift_fsb)) { |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6004 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
| 6005 | *current_ext, gotp, adj_irecp, |
| 6006 | cur, logflags); |
| 6007 | if (error) |
| 6008 | return error; |
| 6009 | adj_irec = got; |
| 6010 | goto update_rmap; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 6011 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6012 | } else { |
| 6013 | startoff = got.br_startoff + offset_shift_fsb; |
| 6014 | /* nothing to move if this is the last extent */ |
| 6015 | if (*current_ext >= (total_extents - 1)) |
| 6016 | goto update_current_ext; |
| 6017 | /* |
| 6018 | * If this is not the last extent in the file, make sure there |
| 6019 | * is enough room between current extent and next extent for |
| 6020 | * accommodating the shift. |
| 6021 | */ |
| 6022 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1); |
| 6023 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 6024 | if (startoff + got.br_blockcount > adj_irec.br_startoff) |
| 6025 | return -EINVAL; |
| 6026 | /* |
| 6027 | * Unlike a left shift (which involves a hole punch), |
| 6028 | * a right shift does not modify extent neighbors |
| 6029 | * in any way. We should never find mergeable extents |
| 6030 | * in this scenario. Check anyways and warn if we |
| 6031 | * encounter two extents that could be one. |
| 6032 | */ |
| 6033 | if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb)) |
| 6034 | WARN_ON_ONCE(1); |
| 6035 | } |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6036 | /* |
| 6037 | * Increment the extent index for the next iteration, update the start |
| 6038 | * offset of the in-core extent and update the btree if applicable. |
| 6039 | */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6040 | update_current_ext: |
| 6041 | if (direction == SHIFT_LEFT) |
| 6042 | (*current_ext)++; |
| 6043 | else |
| 6044 | (*current_ext)--; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6045 | xfs_bmbt_set_startoff(gotp, startoff); |
| 6046 | *logflags |= XFS_ILOG_CORE; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6047 | adj_irec = got; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6048 | if (!cur) { |
| 6049 | *logflags |= XFS_ILOG_DEXT; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6050 | goto update_rmap; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6051 | } |
| 6052 | |
| 6053 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 6054 | got.br_blockcount, &i); |
| 6055 | if (error) |
| 6056 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 6057 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6058 | |
| 6059 | got.br_startoff = startoff; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6060 | error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock, |
| 6061 | got.br_blockcount, got.br_state); |
| 6062 | if (error) |
| 6063 | return error; |
| 6064 | |
| 6065 | update_rmap: |
| 6066 | /* update reverse mapping */ |
| 6067 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec); |
| 6068 | if (error) |
| 6069 | return error; |
| 6070 | adj_irec.br_startoff = startoff; |
| 6071 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6072 | } |
| 6073 | |
| 6074 | /* |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6075 | * Shift extent records to the left/right to cover/create a hole. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6076 | * |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6077 | * 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] | 6078 | * @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] | 6079 | * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb |
| 6080 | * is the length by which each extent is shifted. If there is no hole to shift |
| 6081 | * the extents into, this will be considered invalid operation and we abort |
| 6082 | * immediately. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6083 | */ |
| 6084 | int |
| 6085 | xfs_bmap_shift_extents( |
| 6086 | struct xfs_trans *tp, |
| 6087 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6088 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6089 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6090 | int *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6091 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6092 | xfs_fsblock_t *firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6093 | struct xfs_defer_ops *dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6094 | enum shift_direction direction, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6095 | int num_exts) |
| 6096 | { |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6097 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6098 | struct xfs_bmbt_rec_host *gotp; |
| 6099 | struct xfs_bmbt_irec got; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6100 | struct xfs_mount *mp = ip->i_mount; |
| 6101 | struct xfs_ifork *ifp; |
| 6102 | xfs_extnum_t nexts = 0; |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6103 | xfs_extnum_t current_ext; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6104 | xfs_extnum_t total_extents; |
| 6105 | xfs_extnum_t stop_extent; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6106 | int error = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6107 | int whichfork = XFS_DATA_FORK; |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6108 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6109 | |
| 6110 | if (unlikely(XFS_TEST_ERROR( |
| 6111 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 6112 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6113 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6114 | XFS_ERROR_REPORT("xfs_bmap_shift_extents", |
| 6115 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 6116 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 6120 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6121 | |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6122 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 6123 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6124 | ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT); |
| 6125 | ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6126 | |
| 6127 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6128 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6129 | /* Read in all the extents */ |
| 6130 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6131 | if (error) |
| 6132 | return error; |
| 6133 | } |
| 6134 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6135 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6136 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 6137 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6138 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6139 | cur->bc_private.b.flags = 0; |
| 6140 | } |
| 6141 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6142 | /* |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 6143 | * 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] | 6144 | * are collapsing out, so we cannot use the count of real extents here. |
| 6145 | * Instead we have to calculate it from the incore fork. |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 6146 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 6147 | total_extents = xfs_iext_count(ifp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6148 | if (total_extents == 0) { |
| 6149 | *done = 1; |
| 6150 | goto del_cursor; |
| 6151 | } |
| 6152 | |
| 6153 | /* |
| 6154 | * In case of first right shift, we need to initialize next_fsb |
| 6155 | */ |
| 6156 | if (*next_fsb == NULLFSBLOCK) { |
| 6157 | gotp = xfs_iext_get_ext(ifp, total_extents - 1); |
| 6158 | xfs_bmbt_get_all(gotp, &got); |
| 6159 | *next_fsb = got.br_startoff; |
| 6160 | if (stop_fsb > *next_fsb) { |
| 6161 | *done = 1; |
| 6162 | goto del_cursor; |
| 6163 | } |
| 6164 | } |
| 6165 | |
| 6166 | /* Lookup the extent index at which we have to stop */ |
| 6167 | if (direction == SHIFT_RIGHT) { |
| 6168 | gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent); |
| 6169 | /* Make stop_extent exclusive of shift range */ |
| 6170 | stop_extent--; |
| 6171 | } else |
| 6172 | stop_extent = total_extents; |
| 6173 | |
| 6174 | /* |
| 6175 | * Look up the extent index for the fsb where we start shifting. We can |
| 6176 | * henceforth iterate with current_ext as extent list changes are locked |
| 6177 | * out via ilock. |
| 6178 | * |
| 6179 | * gotp can be null in 2 cases: 1) if there are no extents or 2) |
| 6180 | * *next_fsb lies in a hole beyond which there are no extents. Either |
| 6181 | * way, we are done. |
| 6182 | */ |
| 6183 | gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext); |
| 6184 | if (!gotp) { |
| 6185 | *done = 1; |
| 6186 | goto del_cursor; |
| 6187 | } |
| 6188 | |
| 6189 | /* some sanity checking before we finally start shifting extents */ |
| 6190 | if ((direction == SHIFT_LEFT && current_ext >= stop_extent) || |
| 6191 | (direction == SHIFT_RIGHT && current_ext <= stop_extent)) { |
| 6192 | error = -EIO; |
| 6193 | goto del_cursor; |
| 6194 | } |
| 6195 | |
| 6196 | while (nexts++ < num_exts) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6197 | error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6198 | ¤t_ext, gotp, cur, &logflags, |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6199 | direction, dfops); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6200 | if (error) |
| 6201 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6202 | /* |
| 6203 | * If there was an extent merge during the shift, the extent |
| 6204 | * count can change. Update the total and grade the next record. |
| 6205 | */ |
| 6206 | if (direction == SHIFT_LEFT) { |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 6207 | total_extents = xfs_iext_count(ifp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6208 | stop_extent = total_extents; |
| 6209 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6210 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6211 | if (current_ext == stop_extent) { |
| 6212 | *done = 1; |
| 6213 | *next_fsb = NULLFSBLOCK; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6214 | break; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6215 | } |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6216 | gotp = xfs_iext_get_ext(ifp, current_ext); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6217 | } |
| 6218 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6219 | if (!*done) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6220 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6221 | *next_fsb = got.br_startoff; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6222 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6223 | |
| 6224 | del_cursor: |
| 6225 | if (cur) |
| 6226 | xfs_btree_del_cursor(cur, |
| 6227 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6228 | |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6229 | if (logflags) |
| 6230 | xfs_trans_log_inode(tp, ip, logflags); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6231 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6232 | return error; |
| 6233 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6234 | |
| 6235 | /* |
| 6236 | * Splits an extent into two extents at split_fsb block such that it is |
| 6237 | * the first block of the current_ext. @current_ext is a target extent |
| 6238 | * to be split. @split_fsb is a block where the extents is split. |
| 6239 | * If split_fsb lies in a hole or the first block of extents, just return 0. |
| 6240 | */ |
| 6241 | STATIC int |
| 6242 | xfs_bmap_split_extent_at( |
| 6243 | struct xfs_trans *tp, |
| 6244 | struct xfs_inode *ip, |
| 6245 | xfs_fileoff_t split_fsb, |
| 6246 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6247 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6248 | { |
| 6249 | int whichfork = XFS_DATA_FORK; |
| 6250 | struct xfs_btree_cur *cur = NULL; |
| 6251 | struct xfs_bmbt_rec_host *gotp; |
| 6252 | struct xfs_bmbt_irec got; |
| 6253 | struct xfs_bmbt_irec new; /* split extent */ |
| 6254 | struct xfs_mount *mp = ip->i_mount; |
| 6255 | struct xfs_ifork *ifp; |
| 6256 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
| 6257 | xfs_extnum_t current_ext; |
| 6258 | int error = 0; |
| 6259 | int logflags = 0; |
| 6260 | int i = 0; |
| 6261 | |
| 6262 | if (unlikely(XFS_TEST_ERROR( |
| 6263 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 6264 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6265 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6266 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 6267 | XFS_ERRLEVEL_LOW, mp); |
| 6268 | return -EFSCORRUPTED; |
| 6269 | } |
| 6270 | |
| 6271 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 6272 | return -EIO; |
| 6273 | |
| 6274 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6275 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6276 | /* Read in all the extents */ |
| 6277 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6278 | if (error) |
| 6279 | return error; |
| 6280 | } |
| 6281 | |
| 6282 | /* |
| 6283 | * gotp can be null in 2 cases: 1) if there are no extents |
| 6284 | * or 2) split_fsb lies in a hole beyond which there are |
| 6285 | * no extents. Either way, we are done. |
| 6286 | */ |
| 6287 | gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext); |
| 6288 | if (!gotp) |
| 6289 | return 0; |
| 6290 | |
| 6291 | xfs_bmbt_get_all(gotp, &got); |
| 6292 | |
| 6293 | /* |
| 6294 | * Check split_fsb lies in a hole or the start boundary offset |
| 6295 | * of the extent. |
| 6296 | */ |
| 6297 | if (got.br_startoff >= split_fsb) |
| 6298 | return 0; |
| 6299 | |
| 6300 | gotblkcnt = split_fsb - got.br_startoff; |
| 6301 | new.br_startoff = split_fsb; |
| 6302 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 6303 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 6304 | new.br_state = got.br_state; |
| 6305 | |
| 6306 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6307 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 6308 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6309 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6310 | cur->bc_private.b.flags = 0; |
| 6311 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 6312 | got.br_startblock, |
| 6313 | got.br_blockcount, |
| 6314 | &i); |
| 6315 | if (error) |
| 6316 | goto del_cursor; |
| 6317 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6318 | } |
| 6319 | |
| 6320 | xfs_bmbt_set_blockcount(gotp, gotblkcnt); |
| 6321 | got.br_blockcount = gotblkcnt; |
| 6322 | |
| 6323 | logflags = XFS_ILOG_CORE; |
| 6324 | if (cur) { |
| 6325 | error = xfs_bmbt_update(cur, got.br_startoff, |
| 6326 | got.br_startblock, |
| 6327 | got.br_blockcount, |
| 6328 | got.br_state); |
| 6329 | if (error) |
| 6330 | goto del_cursor; |
| 6331 | } else |
| 6332 | logflags |= XFS_ILOG_DEXT; |
| 6333 | |
| 6334 | /* Add new extent */ |
| 6335 | current_ext++; |
| 6336 | xfs_iext_insert(ip, current_ext, 1, &new, 0); |
| 6337 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 6338 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 6339 | |
| 6340 | if (cur) { |
| 6341 | error = xfs_bmbt_lookup_eq(cur, new.br_startoff, |
| 6342 | new.br_startblock, new.br_blockcount, |
| 6343 | &i); |
| 6344 | if (error) |
| 6345 | goto del_cursor; |
| 6346 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
| 6347 | cur->bc_rec.b.br_state = new.br_state; |
| 6348 | |
| 6349 | error = xfs_btree_insert(cur, &i); |
| 6350 | if (error) |
| 6351 | goto del_cursor; |
| 6352 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6353 | } |
| 6354 | |
| 6355 | /* |
| 6356 | * Convert to a btree if necessary. |
| 6357 | */ |
| 6358 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 6359 | int tmp_logflags; /* partial log flag return val */ |
| 6360 | |
| 6361 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6362 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6363 | &cur, 0, &tmp_logflags, whichfork); |
| 6364 | logflags |= tmp_logflags; |
| 6365 | } |
| 6366 | |
| 6367 | del_cursor: |
| 6368 | if (cur) { |
| 6369 | cur->bc_private.b.allocated = 0; |
| 6370 | xfs_btree_del_cursor(cur, |
| 6371 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6372 | } |
| 6373 | |
| 6374 | if (logflags) |
| 6375 | xfs_trans_log_inode(tp, ip, logflags); |
| 6376 | return error; |
| 6377 | } |
| 6378 | |
| 6379 | int |
| 6380 | xfs_bmap_split_extent( |
| 6381 | struct xfs_inode *ip, |
| 6382 | xfs_fileoff_t split_fsb) |
| 6383 | { |
| 6384 | struct xfs_mount *mp = ip->i_mount; |
| 6385 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6386 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6387 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6388 | int error; |
| 6389 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6390 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6391 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6392 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6393 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6394 | |
| 6395 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6396 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6397 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6398 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6399 | |
| 6400 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6401 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6402 | if (error) |
| 6403 | goto out; |
| 6404 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6405 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6406 | if (error) |
| 6407 | goto out; |
| 6408 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6409 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6410 | |
| 6411 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6412 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6413 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6414 | return error; |
| 6415 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6416 | |
| 6417 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6418 | static bool |
| 6419 | xfs_bmap_is_update_needed( |
| 6420 | struct xfs_bmbt_irec *bmap) |
| 6421 | { |
| 6422 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6423 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6424 | } |
| 6425 | |
| 6426 | /* Record a bmap intent. */ |
| 6427 | static int |
| 6428 | __xfs_bmap_add( |
| 6429 | struct xfs_mount *mp, |
| 6430 | struct xfs_defer_ops *dfops, |
| 6431 | enum xfs_bmap_intent_type type, |
| 6432 | struct xfs_inode *ip, |
| 6433 | int whichfork, |
| 6434 | struct xfs_bmbt_irec *bmap) |
| 6435 | { |
| 6436 | int error; |
| 6437 | struct xfs_bmap_intent *bi; |
| 6438 | |
| 6439 | trace_xfs_bmap_defer(mp, |
| 6440 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6441 | type, |
| 6442 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6443 | ip->i_ino, whichfork, |
| 6444 | bmap->br_startoff, |
| 6445 | bmap->br_blockcount, |
| 6446 | bmap->br_state); |
| 6447 | |
| 6448 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6449 | INIT_LIST_HEAD(&bi->bi_list); |
| 6450 | bi->bi_type = type; |
| 6451 | bi->bi_owner = ip; |
| 6452 | bi->bi_whichfork = whichfork; |
| 6453 | bi->bi_bmap = *bmap; |
| 6454 | |
| 6455 | error = xfs_defer_join(dfops, bi->bi_owner); |
| 6456 | if (error) { |
| 6457 | kmem_free(bi); |
| 6458 | return error; |
| 6459 | } |
| 6460 | |
| 6461 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6462 | return 0; |
| 6463 | } |
| 6464 | |
| 6465 | /* Map an extent into a file. */ |
| 6466 | int |
| 6467 | xfs_bmap_map_extent( |
| 6468 | struct xfs_mount *mp, |
| 6469 | struct xfs_defer_ops *dfops, |
| 6470 | struct xfs_inode *ip, |
| 6471 | struct xfs_bmbt_irec *PREV) |
| 6472 | { |
| 6473 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6474 | return 0; |
| 6475 | |
| 6476 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6477 | XFS_DATA_FORK, PREV); |
| 6478 | } |
| 6479 | |
| 6480 | /* Unmap an extent out of a file. */ |
| 6481 | int |
| 6482 | xfs_bmap_unmap_extent( |
| 6483 | struct xfs_mount *mp, |
| 6484 | struct xfs_defer_ops *dfops, |
| 6485 | struct xfs_inode *ip, |
| 6486 | struct xfs_bmbt_irec *PREV) |
| 6487 | { |
| 6488 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6489 | return 0; |
| 6490 | |
| 6491 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6492 | XFS_DATA_FORK, PREV); |
| 6493 | } |
| 6494 | |
| 6495 | /* |
| 6496 | * Process one of the deferred bmap operations. We pass back the |
| 6497 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6498 | */ |
| 6499 | int |
| 6500 | xfs_bmap_finish_one( |
| 6501 | struct xfs_trans *tp, |
| 6502 | struct xfs_defer_ops *dfops, |
| 6503 | struct xfs_inode *ip, |
| 6504 | enum xfs_bmap_intent_type type, |
| 6505 | int whichfork, |
| 6506 | xfs_fileoff_t startoff, |
| 6507 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6508 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6509 | xfs_exntst_t state) |
| 6510 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6511 | xfs_fsblock_t firstfsb; |
| 6512 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6513 | |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6514 | /* |
| 6515 | * firstfsb is tied to the transaction lifetime and is used to |
| 6516 | * ensure correct AG locking order and schedule work item |
| 6517 | * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us |
| 6518 | * to only making one bmap call per transaction, so it should |
| 6519 | * be safe to have it as a local variable here. |
| 6520 | */ |
| 6521 | firstfsb = NULLFSBLOCK; |
| 6522 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6523 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6524 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6525 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6526 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6527 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6528 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6529 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6530 | |
| 6531 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6532 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6533 | return -EIO; |
| 6534 | |
| 6535 | switch (type) { |
| 6536 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6537 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6538 | startblock, dfops); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6539 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6540 | break; |
| 6541 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6542 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
| 6543 | XFS_BMAPI_REMAP, 1, &firstfsb, dfops); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6544 | break; |
| 6545 | default: |
| 6546 | ASSERT(0); |
| 6547 | error = -EFSCORRUPTED; |
| 6548 | } |
| 6549 | |
| 6550 | return error; |
| 6551 | } |