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) { |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 766 | try_another_ag: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 767 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 768 | args.fsbno = *firstblock; |
| 769 | } else { |
| 770 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 771 | args.fsbno = *firstblock; |
| 772 | } |
| 773 | args.minlen = args.maxlen = args.prod = 1; |
| 774 | args.wasdel = wasdel; |
| 775 | *logflagsp = 0; |
| 776 | if ((error = xfs_alloc_vextent(&args))) { |
| 777 | xfs_iroot_realloc(ip, -1, whichfork); |
| 778 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 779 | return error; |
| 780 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 781 | |
| 782 | /* |
| 783 | * During a CoW operation, the allocation and bmbt updates occur in |
| 784 | * different transactions. The mapping code tries to put new bmbt |
| 785 | * blocks near extents being mapped, but the only way to guarantee this |
| 786 | * is if the alloc and the mapping happen in a single transaction that |
| 787 | * has a block reservation. That isn't the case here, so if we run out |
| 788 | * of space we'll try again with another AG. |
| 789 | */ |
| 790 | if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) && |
| 791 | args.fsbno == NULLFSBLOCK && |
| 792 | args.type == XFS_ALLOCTYPE_NEAR_BNO) { |
| 793 | dfops->dop_low = true; |
| 794 | goto try_another_ag; |
| 795 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 796 | /* |
| 797 | * Allocation can't fail, the space was reserved. |
| 798 | */ |
| 799 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 800 | ASSERT(*firstblock == NULLFSBLOCK || |
| 801 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 802 | (dfops->dop_low && |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 803 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 804 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 805 | cur->bc_private.b.allocated++; |
| 806 | ip->i_d.di_nblocks++; |
| 807 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 808 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 809 | /* |
| 810 | * Fill in the child block. |
| 811 | */ |
| 812 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 813 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 814 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 815 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 816 | XFS_BTREE_LONG_PTRS); |
| 817 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 818 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 819 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 820 | for (cnt = i = 0; i < nextents; i++) { |
| 821 | ep = xfs_iext_get_ext(ifp, i); |
| 822 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
| 823 | arp->l0 = cpu_to_be64(ep->l0); |
| 824 | arp->l1 = cpu_to_be64(ep->l1); |
| 825 | arp++; cnt++; |
| 826 | } |
| 827 | } |
| 828 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 829 | xfs_btree_set_numrecs(ablock, cnt); |
| 830 | |
| 831 | /* |
| 832 | * Fill in the root key and pointer. |
| 833 | */ |
| 834 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 835 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 836 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 837 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 838 | be16_to_cpu(block->bb_level))); |
| 839 | *pp = cpu_to_be64(args.fsbno); |
| 840 | |
| 841 | /* |
| 842 | * Do all this logging at the end so that |
| 843 | * the root is at the right level. |
| 844 | */ |
| 845 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 846 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 847 | ASSERT(*curp == NULL); |
| 848 | *curp = cur; |
| 849 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Convert a local file to an extents file. |
| 855 | * This code is out of bounds for data forks of regular files, |
| 856 | * since the file data needs to get logged so things will stay consistent. |
| 857 | * (The bmap-level manipulations are ok, though). |
| 858 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 859 | void |
| 860 | xfs_bmap_local_to_extents_empty( |
| 861 | struct xfs_inode *ip, |
| 862 | int whichfork) |
| 863 | { |
| 864 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 865 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 866 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 867 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 868 | ASSERT(ifp->if_bytes == 0); |
| 869 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 870 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 871 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 872 | ifp->if_flags &= ~XFS_IFINLINE; |
| 873 | ifp->if_flags |= XFS_IFEXTENTS; |
| 874 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 875 | } |
| 876 | |
| 877 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 878 | STATIC int /* error */ |
| 879 | xfs_bmap_local_to_extents( |
| 880 | xfs_trans_t *tp, /* transaction pointer */ |
| 881 | xfs_inode_t *ip, /* incore inode pointer */ |
| 882 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 883 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 884 | int *logflagsp, /* inode logging flags */ |
| 885 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 886 | void (*init_fn)(struct xfs_trans *tp, |
| 887 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 888 | struct xfs_inode *ip, |
| 889 | struct xfs_ifork *ifp)) |
| 890 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 891 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 892 | int flags; /* logging flags returned */ |
| 893 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 894 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 895 | xfs_buf_t *bp; /* buffer for extent block */ |
| 896 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 897 | |
| 898 | /* |
| 899 | * We don't want to deal with the case of keeping inode data inline yet. |
| 900 | * So sending the data fork of a regular inode is invalid. |
| 901 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 902 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 903 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 904 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 905 | |
| 906 | if (!ifp->if_bytes) { |
| 907 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 908 | flags = XFS_ILOG_CORE; |
| 909 | goto done; |
| 910 | } |
| 911 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 912 | flags = 0; |
| 913 | error = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 914 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
| 915 | XFS_IFINLINE); |
| 916 | memset(&args, 0, sizeof(args)); |
| 917 | args.tp = tp; |
| 918 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 919 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 920 | args.firstblock = *firstblock; |
| 921 | /* |
| 922 | * Allocate a block. We know we need only one, since the |
| 923 | * file currently fits in an inode. |
| 924 | */ |
| 925 | if (*firstblock == NULLFSBLOCK) { |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 926 | try_another_ag: |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 927 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 928 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 929 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 930 | args.fsbno = *firstblock; |
| 931 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 932 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 933 | args.total = total; |
| 934 | args.minlen = args.maxlen = args.prod = 1; |
| 935 | error = xfs_alloc_vextent(&args); |
| 936 | if (error) |
| 937 | goto done; |
| 938 | |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 939 | /* |
| 940 | * During a CoW operation, the allocation and bmbt updates occur in |
| 941 | * different transactions. The mapping code tries to put new bmbt |
| 942 | * blocks near extents being mapped, but the only way to guarantee this |
| 943 | * is if the alloc and the mapping happen in a single transaction that |
| 944 | * has a block reservation. That isn't the case here, so if we run out |
| 945 | * of space we'll try again with another AG. |
| 946 | */ |
| 947 | if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) && |
| 948 | args.fsbno == NULLFSBLOCK && |
| 949 | args.type == XFS_ALLOCTYPE_NEAR_BNO) { |
| 950 | goto try_another_ag; |
| 951 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 952 | /* Can't fail, the space was reserved. */ |
| 953 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 954 | ASSERT(args.len == 1); |
| 955 | *firstblock = args.fsbno; |
| 956 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 957 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 958 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 959 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 960 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 961 | * The callout is responsible for logging because the remote format |
| 962 | * might differ from the local format and thus we don't know how much to |
| 963 | * log here. Note that init_fn must also set the buffer log item type |
| 964 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 965 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 966 | init_fn(tp, bp, ip, ifp); |
| 967 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 968 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 969 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 970 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 971 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 972 | |
| 973 | xfs_iext_add(ifp, 0, 1); |
| 974 | ep = xfs_iext_get_ext(ifp, 0); |
| 975 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
| 976 | trace_xfs_bmap_post_update(ip, 0, |
| 977 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 978 | _THIS_IP_); |
| 979 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 980 | ip->i_d.di_nblocks = 1; |
| 981 | xfs_trans_mod_dquot_byino(tp, ip, |
| 982 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 983 | flags |= xfs_ilog_fext(whichfork); |
| 984 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 985 | done: |
| 986 | *logflagsp = flags; |
| 987 | return error; |
| 988 | } |
| 989 | |
| 990 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 992 | */ |
| 993 | STATIC int /* error */ |
| 994 | xfs_bmap_add_attrfork_btree( |
| 995 | xfs_trans_t *tp, /* transaction pointer */ |
| 996 | xfs_inode_t *ip, /* incore inode pointer */ |
| 997 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 998 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | int *flags) /* inode logging flags */ |
| 1000 | { |
| 1001 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 1002 | int error; /* error return value */ |
| 1003 | xfs_mount_t *mp; /* file system mount struct */ |
| 1004 | int stat; /* newroot status */ |
| 1005 | |
| 1006 | mp = ip->i_mount; |
| 1007 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1008 | *flags |= XFS_ILOG_DBROOT; |
| 1009 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1010 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1011 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | cur->bc_private.b.firstblock = *firstblock; |
| 1013 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 1014 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1015 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1016 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 1017 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | goto error0; |
| 1019 | if (stat == 0) { |
| 1020 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1021 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | } |
| 1023 | *firstblock = cur->bc_private.b.firstblock; |
| 1024 | cur->bc_private.b.allocated = 0; |
| 1025 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1026 | } |
| 1027 | return 0; |
| 1028 | error0: |
| 1029 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1030 | return error; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
| 1034 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 1035 | */ |
| 1036 | STATIC int /* error */ |
| 1037 | xfs_bmap_add_attrfork_extents( |
| 1038 | xfs_trans_t *tp, /* transaction pointer */ |
| 1039 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1040 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1041 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | int *flags) /* inode logging flags */ |
| 1043 | { |
| 1044 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 1045 | int error; /* error return value */ |
| 1046 | |
| 1047 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 1048 | return 0; |
| 1049 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1050 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | flags, XFS_DATA_FORK); |
| 1052 | if (cur) { |
| 1053 | cur->bc_private.b.allocated = 0; |
| 1054 | xfs_btree_del_cursor(cur, |
| 1055 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 1056 | } |
| 1057 | return error; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1061 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 1062 | * different data fork content type needs a different callout to do the |
| 1063 | * conversion. Some are basic and only require special block initialisation |
| 1064 | * callouts for the data formating, others (directories) are so specialised they |
| 1065 | * handle everything themselves. |
| 1066 | * |
| 1067 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1068 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1069 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | */ |
| 1071 | STATIC int /* error */ |
| 1072 | xfs_bmap_add_attrfork_local( |
| 1073 | xfs_trans_t *tp, /* transaction pointer */ |
| 1074 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1075 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1076 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | int *flags) /* inode logging flags */ |
| 1078 | { |
| 1079 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
| 1081 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1082 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1083 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1084 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1086 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | dargs.dp = ip; |
| 1088 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1089 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1090 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | dargs.whichfork = XFS_DATA_FORK; |
| 1092 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1093 | return xfs_dir2_sf_to_block(&dargs); |
| 1094 | } |
| 1095 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1096 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1097 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1098 | flags, XFS_DATA_FORK, |
| 1099 | xfs_symlink_local_to_remote); |
| 1100 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1101 | /* should only be called for types that support local format data */ |
| 1102 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1103 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1107 | * Convert inode from non-attributed to attributed. |
| 1108 | * Must not be in a transaction, ip must not be locked. |
| 1109 | */ |
| 1110 | int /* error code */ |
| 1111 | xfs_bmap_add_attrfork( |
| 1112 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1113 | int size, /* space new attribute needs */ |
| 1114 | int rsvd) /* xact may use reserved blks */ |
| 1115 | { |
| 1116 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1117 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1118 | xfs_mount_t *mp; /* mount structure */ |
| 1119 | xfs_trans_t *tp; /* transaction pointer */ |
| 1120 | int blks; /* space reservation */ |
| 1121 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1122 | int logflags; /* logging flags */ |
| 1123 | int error; /* error return value */ |
| 1124 | |
| 1125 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1126 | |
| 1127 | mp = ip->i_mount; |
| 1128 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1129 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1130 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1131 | |
| 1132 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1133 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1134 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1135 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1136 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1137 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1138 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1139 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1140 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1141 | if (error) |
| 1142 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1143 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1144 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1145 | if (ip->i_d.di_anextents != 0) { |
| 1146 | error = -EFSCORRUPTED; |
| 1147 | goto trans_cancel; |
| 1148 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1149 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1150 | /* |
| 1151 | * For inodes coming from pre-6.2 filesystems. |
| 1152 | */ |
| 1153 | ASSERT(ip->i_d.di_aformat == 0); |
| 1154 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1155 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1156 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1157 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1158 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1159 | |
| 1160 | switch (ip->i_d.di_format) { |
| 1161 | case XFS_DINODE_FMT_DEV: |
| 1162 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1163 | break; |
| 1164 | case XFS_DINODE_FMT_UUID: |
| 1165 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 1166 | break; |
| 1167 | case XFS_DINODE_FMT_LOCAL: |
| 1168 | case XFS_DINODE_FMT_EXTENTS: |
| 1169 | case XFS_DINODE_FMT_BTREE: |
| 1170 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1171 | if (!ip->i_d.di_forkoff) |
| 1172 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1173 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1174 | version = 2; |
| 1175 | break; |
| 1176 | default: |
| 1177 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1178 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1179 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | ASSERT(ip->i_afp == NULL); |
| 1183 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1184 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1185 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1186 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1187 | switch (ip->i_d.di_format) { |
| 1188 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1189 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1190 | &logflags); |
| 1191 | break; |
| 1192 | case XFS_DINODE_FMT_EXTENTS: |
| 1193 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1194 | &dfops, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1195 | break; |
| 1196 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1197 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1198 | &logflags); |
| 1199 | break; |
| 1200 | default: |
| 1201 | error = 0; |
| 1202 | break; |
| 1203 | } |
| 1204 | if (logflags) |
| 1205 | xfs_trans_log_inode(tp, ip, logflags); |
| 1206 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1207 | goto bmap_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1208 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1209 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1210 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1211 | |
| 1212 | spin_lock(&mp->m_sb_lock); |
| 1213 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1214 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1215 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1216 | } |
| 1217 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1218 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1219 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1220 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1221 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1222 | if (log_sb) |
| 1223 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1224 | } |
| 1225 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1226 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1227 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1228 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1229 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1230 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1231 | return error; |
| 1232 | |
| 1233 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1234 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1235 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1236 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1237 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1238 | return error; |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * Internal and external extent tree search functions. |
| 1243 | */ |
| 1244 | |
| 1245 | /* |
| 1246 | * Read in the extents to if_extents. |
| 1247 | * All inode fields are set up by caller, we just traverse the btree |
| 1248 | * and copy the records in. If the file system cannot contain unwritten |
| 1249 | * extents, the records are checked for no "state" flags. |
| 1250 | */ |
| 1251 | int /* error */ |
| 1252 | xfs_bmap_read_extents( |
| 1253 | xfs_trans_t *tp, /* transaction pointer */ |
| 1254 | xfs_inode_t *ip, /* incore inode */ |
| 1255 | int whichfork) /* data or attr fork */ |
| 1256 | { |
| 1257 | struct xfs_btree_block *block; /* current btree block */ |
| 1258 | xfs_fsblock_t bno; /* block # of "block" */ |
| 1259 | xfs_buf_t *bp; /* buffer for "block" */ |
| 1260 | int error; /* error return value */ |
| 1261 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
| 1262 | xfs_extnum_t i, j; /* index into the extents list */ |
| 1263 | xfs_ifork_t *ifp; /* fork structure */ |
| 1264 | int level; /* btree level, for checking */ |
| 1265 | xfs_mount_t *mp; /* file system mount structure */ |
| 1266 | __be64 *pp; /* pointer to block address */ |
| 1267 | /* REFERENCED */ |
| 1268 | xfs_extnum_t room; /* number of entries there's room for */ |
| 1269 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1270 | mp = ip->i_mount; |
| 1271 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1272 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 1273 | XFS_EXTFMT_INODE(ip); |
| 1274 | block = ifp->if_broot; |
| 1275 | /* |
| 1276 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1277 | */ |
| 1278 | level = be16_to_cpu(block->bb_level); |
| 1279 | ASSERT(level > 0); |
| 1280 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1281 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1282 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1283 | /* |
| 1284 | * Go down the tree until leaf level is reached, following the first |
| 1285 | * pointer (leftmost) at each level. |
| 1286 | */ |
| 1287 | while (level-- > 0) { |
| 1288 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1289 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1290 | if (error) |
| 1291 | return error; |
| 1292 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1293 | if (level == 0) |
| 1294 | break; |
| 1295 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1296 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1297 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 1298 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1299 | xfs_trans_brelse(tp, bp); |
| 1300 | } |
| 1301 | /* |
| 1302 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1303 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1304 | room = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1305 | i = 0; |
| 1306 | /* |
| 1307 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1308 | */ |
| 1309 | for (;;) { |
| 1310 | xfs_bmbt_rec_t *frp; |
| 1311 | xfs_fsblock_t nextbno; |
| 1312 | xfs_extnum_t num_recs; |
| 1313 | xfs_extnum_t start; |
| 1314 | |
| 1315 | num_recs = xfs_btree_get_numrecs(block); |
| 1316 | if (unlikely(i + num_recs > room)) { |
| 1317 | ASSERT(i + num_recs <= room); |
| 1318 | xfs_warn(ip->i_mount, |
| 1319 | "corrupt dinode %Lu, (btree extents).", |
| 1320 | (unsigned long long) ip->i_ino); |
| 1321 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 1322 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
| 1323 | goto error0; |
| 1324 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1325 | /* |
| 1326 | * Read-ahead the next leaf block, if any. |
| 1327 | */ |
| 1328 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1329 | if (nextbno != NULLFSBLOCK) |
| 1330 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1331 | &xfs_bmbt_buf_ops); |
| 1332 | /* |
| 1333 | * Copy records into the extent records. |
| 1334 | */ |
| 1335 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 1336 | start = i; |
| 1337 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 1338 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
| 1339 | trp->l0 = be64_to_cpu(frp->l0); |
| 1340 | trp->l1 = be64_to_cpu(frp->l1); |
| 1341 | } |
| 1342 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 1343 | /* |
| 1344 | * Check all attribute bmap btree records and |
| 1345 | * any "older" data bmap btree records for a |
| 1346 | * set bit in the "extent flag" position. |
| 1347 | */ |
| 1348 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 1349 | start, num_recs))) { |
| 1350 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 1351 | XFS_ERRLEVEL_LOW, |
| 1352 | ip->i_mount); |
| 1353 | goto error0; |
| 1354 | } |
| 1355 | } |
| 1356 | xfs_trans_brelse(tp, bp); |
| 1357 | bno = nextbno; |
| 1358 | /* |
| 1359 | * If we've reached the end, stop. |
| 1360 | */ |
| 1361 | if (bno == NULLFSBLOCK) |
| 1362 | break; |
| 1363 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1364 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1365 | if (error) |
| 1366 | return error; |
| 1367 | block = XFS_BUF_TO_BLOCK(bp); |
| 1368 | } |
Darrick J. Wong | 356a322 | 2016-12-05 12:36:56 +1100 | [diff] [blame] | 1369 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) |
| 1370 | return -EFSCORRUPTED; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1371 | ASSERT(i == xfs_iext_count(ifp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1372 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
| 1373 | return 0; |
| 1374 | error0: |
| 1375 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1376 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1377 | } |
| 1378 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1379 | /* |
| 1380 | * Returns the file-relative block number of the first unused block(s) |
| 1381 | * in the file with at least "len" logically contiguous blocks free. |
| 1382 | * This is the lowest-address hole if the file has holes, else the first block |
| 1383 | * past the end of file. |
| 1384 | * Return 0 if the file is currently local (in-inode). |
| 1385 | */ |
| 1386 | int /* error */ |
| 1387 | xfs_bmap_first_unused( |
| 1388 | xfs_trans_t *tp, /* transaction pointer */ |
| 1389 | xfs_inode_t *ip, /* incore inode */ |
| 1390 | xfs_extlen_t len, /* size of hole to find */ |
| 1391 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1392 | int whichfork) /* data or attr fork */ |
| 1393 | { |
| 1394 | int error; /* error return value */ |
| 1395 | int idx; /* extent record index */ |
| 1396 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1397 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 1398 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 1399 | xfs_fileoff_t max; /* starting useful block */ |
| 1400 | xfs_fileoff_t off; /* offset for this block */ |
| 1401 | xfs_extnum_t nextents; /* number of extent entries */ |
| 1402 | |
| 1403 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1404 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1405 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 1406 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1407 | *first_unused = 0; |
| 1408 | return 0; |
| 1409 | } |
| 1410 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1411 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1412 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1413 | return error; |
| 1414 | lowest = *first_unused; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1415 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1416 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
| 1417 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
| 1418 | off = xfs_bmbt_get_startoff(ep); |
| 1419 | /* |
| 1420 | * See if the hole before this extent will work. |
| 1421 | */ |
| 1422 | if (off >= lowest + len && off - max >= len) { |
| 1423 | *first_unused = max; |
| 1424 | return 0; |
| 1425 | } |
| 1426 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 1427 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1428 | } |
| 1429 | *first_unused = max; |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
| 1433 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1434 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1435 | * last_block (input value) in the file. |
| 1436 | * This is not based on i_size, it is based on the extent records. |
| 1437 | * Returns 0 for local files, as they do not have extent records. |
| 1438 | */ |
| 1439 | int /* error */ |
| 1440 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1441 | struct xfs_trans *tp, /* transaction pointer */ |
| 1442 | struct xfs_inode *ip, /* incore inode */ |
| 1443 | xfs_fileoff_t *last_block, /* last block */ |
| 1444 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1445 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1446 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1447 | struct xfs_bmbt_irec got; |
| 1448 | xfs_extnum_t idx; |
| 1449 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1450 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1451 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1452 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1453 | *last_block = 0; |
| 1454 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1455 | case XFS_DINODE_FMT_BTREE: |
| 1456 | case XFS_DINODE_FMT_EXTENTS: |
| 1457 | break; |
| 1458 | default: |
| 1459 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1460 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1461 | |
| 1462 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1463 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1464 | if (error) |
| 1465 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1466 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1467 | |
| 1468 | if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) { |
| 1469 | if (got.br_startoff <= *last_block - 1) |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | if (xfs_iext_get_extent(ifp, idx - 1, &got)) { |
| 1474 | *last_block = got.br_startoff + got.br_blockcount; |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1479 | return 0; |
| 1480 | } |
| 1481 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1482 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1483 | xfs_bmap_last_extent( |
| 1484 | struct xfs_trans *tp, |
| 1485 | struct xfs_inode *ip, |
| 1486 | int whichfork, |
| 1487 | struct xfs_bmbt_irec *rec, |
| 1488 | int *is_empty) |
| 1489 | { |
| 1490 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1491 | int error; |
| 1492 | int nextents; |
| 1493 | |
| 1494 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1495 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1496 | if (error) |
| 1497 | return error; |
| 1498 | } |
| 1499 | |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1500 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1501 | if (nextents == 0) { |
| 1502 | *is_empty = 1; |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 1507 | *is_empty = 0; |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | /* |
| 1512 | * Check the last inode extent to determine whether this allocation will result |
| 1513 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1514 | * blocks at the end of the file which do not start at the previous data block, |
| 1515 | * we will try to align the new blocks at stripe unit boundaries. |
| 1516 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1517 | * 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] | 1518 | * at, or past the EOF. |
| 1519 | */ |
| 1520 | STATIC int |
| 1521 | xfs_bmap_isaeof( |
| 1522 | struct xfs_bmalloca *bma, |
| 1523 | int whichfork) |
| 1524 | { |
| 1525 | struct xfs_bmbt_irec rec; |
| 1526 | int is_empty; |
| 1527 | int error; |
| 1528 | |
| 1529 | bma->aeof = 0; |
| 1530 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1531 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1532 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1533 | return error; |
| 1534 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1535 | if (is_empty) { |
| 1536 | bma->aeof = 1; |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1540 | /* |
| 1541 | * Check if we are allocation or past the last extent, or at least into |
| 1542 | * the last delayed allocated extent. |
| 1543 | */ |
| 1544 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1545 | (bma->offset >= rec.br_startoff && |
| 1546 | isnullstartblock(rec.br_startblock)); |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1551 | * Returns the file-relative block number of the first block past eof in |
| 1552 | * the file. This is not based on i_size, it is based on the extent records. |
| 1553 | * Returns 0 for local files, as they do not have extent records. |
| 1554 | */ |
| 1555 | int |
| 1556 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1557 | struct xfs_inode *ip, |
| 1558 | xfs_fileoff_t *last_block, |
| 1559 | int whichfork) |
| 1560 | { |
| 1561 | struct xfs_bmbt_irec rec; |
| 1562 | int is_empty; |
| 1563 | int error; |
| 1564 | |
| 1565 | *last_block = 0; |
| 1566 | |
| 1567 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1568 | return 0; |
| 1569 | |
| 1570 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1571 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1572 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1573 | |
| 1574 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1575 | if (error || is_empty) |
| 1576 | return error; |
| 1577 | |
| 1578 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * Returns whether the selected fork of the inode has exactly one |
| 1584 | * block or not. For the data fork we check this matches di_size, |
| 1585 | * implying the file's range is 0..bsize-1. |
| 1586 | */ |
| 1587 | int /* 1=>1 block, 0=>otherwise */ |
| 1588 | xfs_bmap_one_block( |
| 1589 | xfs_inode_t *ip, /* incore inode */ |
| 1590 | int whichfork) /* data or attr fork */ |
| 1591 | { |
| 1592 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
| 1593 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1594 | int rval; /* return value */ |
| 1595 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 1596 | |
| 1597 | #ifndef DEBUG |
| 1598 | if (whichfork == XFS_DATA_FORK) |
| 1599 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1600 | #endif /* !DEBUG */ |
| 1601 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1602 | return 0; |
| 1603 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1604 | return 0; |
| 1605 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1606 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 1607 | ep = xfs_iext_get_ext(ifp, 0); |
| 1608 | xfs_bmbt_get_all(ep, &s); |
| 1609 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1610 | if (rval && whichfork == XFS_DATA_FORK) |
| 1611 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1612 | return rval; |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Extent tree manipulation functions used during allocation. |
| 1617 | */ |
| 1618 | |
| 1619 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1620 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | */ |
| 1622 | STATIC int /* error */ |
| 1623 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1624 | struct xfs_bmalloca *bma, |
| 1625 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1627 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1629 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1632 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1634 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1635 | /* left is 0, right is 1, prev is 2 */ |
| 1636 | int rval=0; /* return value (logging flags) */ |
| 1637 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1638 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1639 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1640 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 1641 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1643 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1644 | xfs_extnum_t *nextents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1646 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1647 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1648 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1649 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1650 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1651 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1652 | ASSERT(bma->idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1653 | ASSERT(bma->idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1654 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1655 | ASSERT(!bma->cur || |
| 1656 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1657 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1658 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | #define LEFT r[0] |
| 1661 | #define RIGHT r[1] |
| 1662 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1664 | if (whichfork == XFS_COW_FORK) |
| 1665 | state |= BMAP_COWFORK; |
| 1666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | /* |
| 1668 | * Set up a bunch of variables to make the tests simpler. |
| 1669 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1670 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | xfs_bmbt_get_all(ep, &PREV); |
| 1672 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1673 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1674 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1675 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1676 | da_old = startblockval(PREV.br_startblock); |
| 1677 | da_new = 0; |
| 1678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | /* |
| 1680 | * Set flags determining what part of the previous delayed allocation |
| 1681 | * extent is being replaced by a real allocation. |
| 1682 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1683 | if (PREV.br_startoff == new->br_startoff) |
| 1684 | state |= BMAP_LEFT_FILLING; |
| 1685 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1686 | state |= BMAP_RIGHT_FILLING; |
| 1687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | /* |
| 1689 | * Check and set flags if this segment has a left neighbor. |
| 1690 | * Don't set contiguous if the combined extent would be too large. |
| 1691 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1692 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1693 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1694 | 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] | 1695 | |
| 1696 | if (isnullstartblock(LEFT.br_startblock)) |
| 1697 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1699 | |
| 1700 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1701 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1702 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1703 | LEFT.br_state == new->br_state && |
| 1704 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1705 | state |= BMAP_LEFT_CONTIG; |
| 1706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | /* |
| 1708 | * Check and set flags if this segment has a right neighbor. |
| 1709 | * Don't set contiguous if the combined extent would be too large. |
| 1710 | * Also check for all-three-contiguous being too large. |
| 1711 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1712 | if (bma->idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1713 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1714 | 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] | 1715 | |
| 1716 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1717 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1719 | |
| 1720 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1721 | new_endoff == RIGHT.br_startoff && |
| 1722 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1723 | new->br_state == RIGHT.br_state && |
| 1724 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1725 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1726 | BMAP_RIGHT_FILLING)) != |
| 1727 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1728 | BMAP_RIGHT_FILLING) || |
| 1729 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1730 | <= MAXEXTLEN)) |
| 1731 | state |= BMAP_RIGHT_CONTIG; |
| 1732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | error = 0; |
| 1734 | /* |
| 1735 | * Switch out based on the FILLING and CONTIG state bits. |
| 1736 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1737 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1738 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1739 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1740 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | /* |
| 1742 | * Filling in all of a previously delayed allocation extent. |
| 1743 | * The left and right neighbors are both contiguous with new. |
| 1744 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1745 | bma->idx--; |
| 1746 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1747 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1749 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1750 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1751 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1752 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1753 | (*nextents)--; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1754 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1756 | else { |
| 1757 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1758 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1760 | RIGHT.br_blockcount, &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_btree_delete(bma->cur, &i); |
| 1765 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1767 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1768 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1769 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1771 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1772 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | LEFT.br_startblock, |
| 1774 | LEFT.br_blockcount + |
| 1775 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1776 | RIGHT.br_blockcount, LEFT.br_state); |
| 1777 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | goto done; |
| 1779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | break; |
| 1781 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1782 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | /* |
| 1784 | * Filling in all of a previously delayed allocation extent. |
| 1785 | * The left neighbor is contiguous, the right is not. |
| 1786 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1787 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1788 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1789 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1790 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1791 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1792 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1793 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1794 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1795 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | rval = XFS_ILOG_DEXT; |
| 1797 | else { |
| 1798 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1799 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1801 | &i); |
| 1802 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1804 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1805 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | LEFT.br_startblock, |
| 1807 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1808 | PREV.br_blockcount, LEFT.br_state); |
| 1809 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | goto done; |
| 1811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | break; |
| 1813 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1814 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | /* |
| 1816 | * Filling in all of a previously delayed allocation extent. |
| 1817 | * The right neighbor is contiguous, the left is not. |
| 1818 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1819 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 1821 | xfs_bmbt_set_blockcount(ep, |
| 1822 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1823 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1824 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1825 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1826 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | rval = XFS_ILOG_DEXT; |
| 1828 | else { |
| 1829 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1830 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1832 | RIGHT.br_blockcount, &i); |
| 1833 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1835 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1836 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | new->br_startblock, |
| 1838 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1839 | RIGHT.br_blockcount, PREV.br_state); |
| 1840 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | goto done; |
| 1842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | break; |
| 1844 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1845 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | /* |
| 1847 | * Filling in all of a previously delayed allocation extent. |
| 1848 | * Neither the left nor right neighbors are contiguous with |
| 1849 | * the new one. |
| 1850 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1851 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 1853 | xfs_bmbt_set_state(ep, new->br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1854 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1855 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1856 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1857 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1859 | else { |
| 1860 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1861 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1863 | &i); |
| 1864 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1866 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1867 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1868 | error = xfs_btree_insert(bma->cur, &i); |
| 1869 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1871 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | } |
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 | BMAP_LEFT_CONTIG: |
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 contiguous. |
| 1879 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1880 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 1881 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | LEFT.br_blockcount + new->br_blockcount); |
| 1883 | xfs_bmbt_set_startoff(ep, |
| 1884 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1885 | 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] | 1886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1888 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1890 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | rval = XFS_ILOG_DEXT; |
| 1892 | else { |
| 1893 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1894 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1896 | &i); |
| 1897 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1899 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1900 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | LEFT.br_startblock, |
| 1902 | LEFT.br_blockcount + |
| 1903 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1904 | LEFT.br_state); |
| 1905 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | goto done; |
| 1907 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1908 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1909 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1910 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1911 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1912 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1913 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | break; |
| 1915 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1916 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | /* |
| 1918 | * Filling in the first part of a previous delayed allocation. |
| 1919 | * The left neighbor is not contiguous. |
| 1920 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1921 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1923 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1924 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1925 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1926 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1927 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1929 | else { |
| 1930 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1931 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1933 | &i); |
| 1934 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1936 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1937 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1938 | error = xfs_btree_insert(bma->cur, &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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1943 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1944 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1945 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1946 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1947 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | rval |= tmp_rval; |
| 1949 | if (error) |
| 1950 | goto done; |
| 1951 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1952 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1953 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1954 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 1955 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1956 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1957 | 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] | 1958 | break; |
| 1959 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1960 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
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 contiguous with the new allocation. |
| 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 + 1, 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_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1969 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | new->br_blockcount + RIGHT.br_blockcount, |
| 1971 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1972 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 1973 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | rval = XFS_ILOG_DEXT; |
| 1975 | else { |
| 1976 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1977 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1979 | RIGHT.br_blockcount, &i); |
| 1980 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1982 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1983 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | new->br_startblock, |
| 1985 | new->br_blockcount + |
| 1986 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1987 | RIGHT.br_state); |
| 1988 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | goto done; |
| 1990 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1991 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1992 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1993 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1994 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1995 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1996 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1997 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1998 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | break; |
| 2000 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2001 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | /* |
| 2003 | * Filling in the last part of a previous delayed allocation. |
| 2004 | * The right neighbor is not contiguous. |
| 2005 | */ |
| 2006 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2007 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2009 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2010 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2011 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2013 | else { |
| 2014 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2015 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2017 | &i); |
| 2018 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2020 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2021 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2022 | error = xfs_btree_insert(bma->cur, &i); |
| 2023 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2025 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2027 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2028 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2029 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2030 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2031 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | rval |= tmp_rval; |
| 2033 | if (error) |
| 2034 | goto done; |
| 2035 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2036 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2037 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2038 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 2039 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2040 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2041 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2042 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2043 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | break; |
| 2045 | |
| 2046 | case 0: |
| 2047 | /* |
| 2048 | * Filling in the middle part of a previous delayed allocation. |
| 2049 | * Contiguity is impossible here. |
| 2050 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2051 | * |
| 2052 | * We start with a delayed allocation: |
| 2053 | * |
| 2054 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 2055 | * PREV @ idx |
| 2056 | * |
| 2057 | * and we are allocating: |
| 2058 | * +rrrrrrrrrrrrrrrrr+ |
| 2059 | * new |
| 2060 | * |
| 2061 | * and we set it up for insertion as: |
| 2062 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 2063 | * new |
| 2064 | * PREV @ idx LEFT RIGHT |
| 2065 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | */ |
| 2067 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2069 | 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] | 2070 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 2071 | LEFT = *new; |
| 2072 | RIGHT.br_state = PREV.br_state; |
| 2073 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2074 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2075 | RIGHT.br_startoff = new_endoff; |
| 2076 | RIGHT.br_blockcount = temp2; |
| 2077 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2078 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2079 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2080 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2082 | else { |
| 2083 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2084 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2086 | &i); |
| 2087 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2089 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2090 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2091 | error = xfs_btree_insert(bma->cur, &i); |
| 2092 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2094 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2096 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2097 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2098 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2099 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2100 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | rval |= tmp_rval; |
| 2102 | if (error) |
| 2103 | goto done; |
| 2104 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2105 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 2106 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2107 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2108 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2109 | if (diff > 0) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2110 | error = xfs_mod_fdblocks(bma->ip->i_mount, |
| 2111 | -((int64_t)diff), false); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2112 | ASSERT(!error); |
| 2113 | if (error) |
| 2114 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2116 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2117 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2118 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2119 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2120 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 2121 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2122 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2123 | 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] | 2124 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2125 | bma->idx++; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2126 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | break; |
| 2128 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2129 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2130 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2131 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2132 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2133 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2134 | case BMAP_LEFT_CONTIG: |
| 2135 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | /* |
| 2137 | * These cases are all impossible. |
| 2138 | */ |
| 2139 | ASSERT(0); |
| 2140 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2141 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2142 | /* add reverse mapping */ |
| 2143 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 2144 | if (error) |
| 2145 | goto done; |
| 2146 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2147 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2148 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2149 | int tmp_logflags; /* partial log flag return val */ |
| 2150 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2151 | ASSERT(bma->cur == NULL); |
| 2152 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2153 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2154 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2155 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2156 | if (error) |
| 2157 | goto done; |
| 2158 | } |
| 2159 | |
| 2160 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2161 | if (da_old || da_new) { |
| 2162 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2163 | if (bma->cur) |
| 2164 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2165 | ASSERT(temp <= da_old); |
| 2166 | if (temp < da_old) |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2167 | xfs_mod_fdblocks(bma->ip->i_mount, |
| 2168 | (int64_t)(da_old - temp), false); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2172 | if (bma->cur) |
| 2173 | bma->cur->bc_private.b.allocated = 0; |
| 2174 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2175 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2177 | if (whichfork != XFS_COW_FORK) |
| 2178 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | return error; |
| 2180 | #undef LEFT |
| 2181 | #undef RIGHT |
| 2182 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2186 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | */ |
| 2188 | STATIC int /* error */ |
| 2189 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2190 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2192 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2193 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2195 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2196 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2197 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2198 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2201 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2204 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 2206 | xfs_exntst_t newext; /* new extent state */ |
| 2207 | xfs_exntst_t oldext; /* old extent state */ |
| 2208 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2209 | /* left is 0, right is 1, prev is 2 */ |
| 2210 | int rval=0; /* return value (logging flags) */ |
| 2211 | int state = 0;/* state bits, accessed thru macros */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2212 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2214 | *logflagsp = 0; |
| 2215 | |
| 2216 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2217 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2218 | if (whichfork == XFS_COW_FORK) |
| 2219 | state |= BMAP_COWFORK; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2220 | |
| 2221 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2222 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2223 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2224 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2225 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | #define LEFT r[0] |
| 2228 | #define RIGHT r[1] |
| 2229 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | /* |
| 2232 | * Set up a bunch of variables to make the tests simpler. |
| 2233 | */ |
| 2234 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2235 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | xfs_bmbt_get_all(ep, &PREV); |
| 2237 | newext = new->br_state; |
| 2238 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 2239 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 2240 | ASSERT(PREV.br_state == oldext); |
| 2241 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2242 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2243 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | /* |
| 2246 | * Set flags determining what part of the previous oldext allocation |
| 2247 | * extent is being replaced by a newext allocation. |
| 2248 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2249 | if (PREV.br_startoff == new->br_startoff) |
| 2250 | state |= BMAP_LEFT_FILLING; |
| 2251 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2252 | state |= BMAP_RIGHT_FILLING; |
| 2253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | /* |
| 2255 | * Check and set flags if this segment has a left neighbor. |
| 2256 | * Don't set contiguous if the combined extent would be too large. |
| 2257 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2258 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2259 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2260 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2261 | |
| 2262 | if (isnullstartblock(LEFT.br_startblock)) |
| 2263 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2265 | |
| 2266 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2267 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2268 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 2269 | LEFT.br_state == newext && |
| 2270 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2271 | state |= BMAP_LEFT_CONTIG; |
| 2272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | /* |
| 2274 | * Check and set flags if this segment has a right neighbor. |
| 2275 | * Don't set contiguous if the combined extent would be too large. |
| 2276 | * Also check for all-three-contiguous being too large. |
| 2277 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2278 | if (*idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2279 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2280 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2281 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2282 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2284 | |
| 2285 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2286 | new_endoff == RIGHT.br_startoff && |
| 2287 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 2288 | newext == RIGHT.br_state && |
| 2289 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2290 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2291 | BMAP_RIGHT_FILLING)) != |
| 2292 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2293 | BMAP_RIGHT_FILLING) || |
| 2294 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2295 | <= MAXEXTLEN)) |
| 2296 | state |= BMAP_RIGHT_CONTIG; |
| 2297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | /* |
| 2299 | * Switch out based on the FILLING and CONTIG state bits. |
| 2300 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2301 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2302 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2303 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2304 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | /* |
| 2306 | * Setting all of a previous oldext extent to newext. |
| 2307 | * The left and right neighbors are both contiguous with new. |
| 2308 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2309 | --*idx; |
| 2310 | |
| 2311 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2312 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2314 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2315 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2316 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2317 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2318 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2319 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | if (cur == NULL) |
| 2321 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2322 | else { |
| 2323 | rval = XFS_ILOG_CORE; |
| 2324 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2325 | RIGHT.br_startblock, |
| 2326 | RIGHT.br_blockcount, &i))) |
| 2327 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2328 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2329 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2331 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2332 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2334 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2335 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2337 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2338 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2340 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2342 | LEFT.br_startblock, |
| 2343 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2344 | RIGHT.br_blockcount, LEFT.br_state))) |
| 2345 | goto done; |
| 2346 | } |
| 2347 | break; |
| 2348 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2349 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | /* |
| 2351 | * Setting all of a previous oldext extent to newext. |
| 2352 | * The left neighbor is contiguous, the right is not. |
| 2353 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2354 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2355 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2356 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2357 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 2358 | LEFT.br_blockcount + PREV.br_blockcount); |
| 2359 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2360 | |
| 2361 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2362 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2363 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | if (cur == NULL) |
| 2365 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2366 | else { |
| 2367 | rval = XFS_ILOG_CORE; |
| 2368 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2369 | PREV.br_startblock, PREV.br_blockcount, |
| 2370 | &i))) |
| 2371 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2372 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2373 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2375 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2376 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2378 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2380 | LEFT.br_startblock, |
| 2381 | LEFT.br_blockcount + PREV.br_blockcount, |
| 2382 | LEFT.br_state))) |
| 2383 | goto done; |
| 2384 | } |
| 2385 | break; |
| 2386 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2387 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | /* |
| 2389 | * Setting all of a previous oldext extent to newext. |
| 2390 | * The right neighbor is contiguous, the left is not. |
| 2391 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2392 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | xfs_bmbt_set_blockcount(ep, |
| 2394 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 2395 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2396 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2397 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2398 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2399 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | if (cur == NULL) |
| 2401 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2402 | else { |
| 2403 | rval = XFS_ILOG_CORE; |
| 2404 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2405 | RIGHT.br_startblock, |
| 2406 | RIGHT.br_blockcount, &i))) |
| 2407 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2408 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2409 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2411 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2412 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2414 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2416 | new->br_startblock, |
| 2417 | new->br_blockcount + RIGHT.br_blockcount, |
| 2418 | newext))) |
| 2419 | goto done; |
| 2420 | } |
| 2421 | break; |
| 2422 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2423 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | /* |
| 2425 | * Setting all of a previous oldext extent to newext. |
| 2426 | * Neither the left nor right neighbors are contiguous with |
| 2427 | * the new one. |
| 2428 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2429 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2431 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | if (cur == NULL) |
| 2434 | rval = XFS_ILOG_DEXT; |
| 2435 | else { |
| 2436 | rval = 0; |
| 2437 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2438 | new->br_startblock, new->br_blockcount, |
| 2439 | &i))) |
| 2440 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2441 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2443 | new->br_startblock, new->br_blockcount, |
| 2444 | newext))) |
| 2445 | goto done; |
| 2446 | } |
| 2447 | break; |
| 2448 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2449 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | /* |
| 2451 | * Setting the first part of a previous oldext extent to newext. |
| 2452 | * The left neighbor is contiguous. |
| 2453 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2454 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 2455 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | LEFT.br_blockcount + new->br_blockcount); |
| 2457 | xfs_bmbt_set_startoff(ep, |
| 2458 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2459 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 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 | xfs_bmbt_set_startblock(ep, |
| 2463 | new->br_startblock + new->br_blockcount); |
| 2464 | xfs_bmbt_set_blockcount(ep, |
| 2465 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2466 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2467 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2468 | --*idx; |
| 2469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | if (cur == NULL) |
| 2471 | rval = XFS_ILOG_DEXT; |
| 2472 | else { |
| 2473 | rval = 0; |
| 2474 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2475 | PREV.br_startblock, PREV.br_blockcount, |
| 2476 | &i))) |
| 2477 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2478 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | if ((error = xfs_bmbt_update(cur, |
| 2480 | PREV.br_startoff + new->br_blockcount, |
| 2481 | PREV.br_startblock + new->br_blockcount, |
| 2482 | PREV.br_blockcount - new->br_blockcount, |
| 2483 | oldext))) |
| 2484 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2485 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2487 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | LEFT.br_startblock, |
| 2489 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2490 | LEFT.br_state); |
| 2491 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | goto done; |
| 2493 | } |
| 2494 | break; |
| 2495 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2496 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | /* |
| 2498 | * Setting the first part of a previous oldext extent to newext. |
| 2499 | * The left neighbor is not contiguous. |
| 2500 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2501 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 2503 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 2504 | xfs_bmbt_set_blockcount(ep, |
| 2505 | PREV.br_blockcount - new->br_blockcount); |
| 2506 | xfs_bmbt_set_startblock(ep, |
| 2507 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2508 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2509 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2510 | xfs_iext_insert(ip, *idx, 1, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2511 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2512 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | if (cur == NULL) |
| 2514 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2515 | else { |
| 2516 | rval = XFS_ILOG_CORE; |
| 2517 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2518 | PREV.br_startblock, PREV.br_blockcount, |
| 2519 | &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, |
| 2523 | PREV.br_startoff + new->br_blockcount, |
| 2524 | PREV.br_startblock + new->br_blockcount, |
| 2525 | PREV.br_blockcount - new->br_blockcount, |
| 2526 | oldext))) |
| 2527 | goto done; |
| 2528 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2529 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2531 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | } |
| 2533 | break; |
| 2534 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2535 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | /* |
| 2537 | * Setting the last part of a previous oldext extent to newext. |
| 2538 | * The right neighbor is contiguous with the new allocation. |
| 2539 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2540 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | xfs_bmbt_set_blockcount(ep, |
| 2542 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2543 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2544 | |
| 2545 | ++*idx; |
| 2546 | |
| 2547 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2548 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2549 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2551 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | if (cur == NULL) |
| 2554 | rval = XFS_ILOG_DEXT; |
| 2555 | else { |
| 2556 | rval = 0; |
| 2557 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2558 | PREV.br_startblock, |
| 2559 | PREV.br_blockcount, &i))) |
| 2560 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2561 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2563 | PREV.br_startblock, |
| 2564 | PREV.br_blockcount - new->br_blockcount, |
| 2565 | oldext))) |
| 2566 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2567 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | goto done; |
| 2569 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2570 | new->br_startblock, |
| 2571 | new->br_blockcount + RIGHT.br_blockcount, |
| 2572 | newext))) |
| 2573 | goto done; |
| 2574 | } |
| 2575 | break; |
| 2576 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2577 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | /* |
| 2579 | * Setting the last part of a previous oldext extent to newext. |
| 2580 | * The right neighbor is not contiguous. |
| 2581 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2582 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | xfs_bmbt_set_blockcount(ep, |
| 2584 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2585 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2586 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2587 | ++*idx; |
| 2588 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2589 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2590 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2591 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | if (cur == NULL) |
| 2593 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2594 | else { |
| 2595 | rval = XFS_ILOG_CORE; |
| 2596 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2597 | PREV.br_startblock, PREV.br_blockcount, |
| 2598 | &i))) |
| 2599 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2600 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2602 | PREV.br_startblock, |
| 2603 | PREV.br_blockcount - new->br_blockcount, |
| 2604 | oldext))) |
| 2605 | goto done; |
| 2606 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2607 | new->br_startblock, new->br_blockcount, |
| 2608 | &i))) |
| 2609 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2610 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2612 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2614 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | } |
| 2616 | break; |
| 2617 | |
| 2618 | case 0: |
| 2619 | /* |
| 2620 | * Setting the middle part of a previous oldext extent to |
| 2621 | * newext. Contiguity is impossible here. |
| 2622 | * One extent becomes three extents. |
| 2623 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2624 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | xfs_bmbt_set_blockcount(ep, |
| 2626 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2627 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | r[0] = *new; |
| 2630 | r[1].br_startoff = new_endoff; |
| 2631 | r[1].br_blockcount = |
| 2632 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 2633 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 2634 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2635 | |
| 2636 | ++*idx; |
| 2637 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 2638 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2639 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2640 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | if (cur == NULL) |
| 2642 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2643 | else { |
| 2644 | rval = XFS_ILOG_CORE; |
| 2645 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2646 | PREV.br_startblock, PREV.br_blockcount, |
| 2647 | &i))) |
| 2648 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2649 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | /* new right extent - oldext */ |
| 2651 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 2652 | r[1].br_startblock, r[1].br_blockcount, |
| 2653 | r[1].br_state))) |
| 2654 | goto done; |
| 2655 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 2657 | cur->bc_rec.b.br_blockcount = |
| 2658 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2659 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2661 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2662 | /* |
| 2663 | * Reset the cursor to the position of the new extent |
| 2664 | * we are about to insert as we can't trust it after |
| 2665 | * the previous insert. |
| 2666 | */ |
| 2667 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2668 | new->br_startblock, new->br_blockcount, |
| 2669 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2671 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2673 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2674 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2676 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | } |
| 2678 | break; |
| 2679 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2680 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2681 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2682 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2683 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2684 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2685 | case BMAP_LEFT_CONTIG: |
| 2686 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | /* |
| 2688 | * These cases are all impossible. |
| 2689 | */ |
| 2690 | ASSERT(0); |
| 2691 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2692 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2693 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2694 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2695 | if (error) |
| 2696 | goto done; |
| 2697 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2698 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2699 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2700 | int tmp_logflags; /* partial log flag return val */ |
| 2701 | |
| 2702 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2703 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2704 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2705 | *logflagsp |= tmp_logflags; |
| 2706 | if (error) |
| 2707 | goto done; |
| 2708 | } |
| 2709 | |
| 2710 | /* clear out the allocated field, done with it now in any case. */ |
| 2711 | if (cur) { |
| 2712 | cur->bc_private.b.allocated = 0; |
| 2713 | *curp = cur; |
| 2714 | } |
| 2715 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2716 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2718 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | return error; |
| 2720 | #undef LEFT |
| 2721 | #undef RIGHT |
| 2722 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2726 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2728 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | xfs_bmap_add_extent_hole_delay( |
| 2730 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2731 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2732 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2733 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2735 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2737 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2738 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2739 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 2740 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2741 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2743 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | state = 0; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2745 | if (whichfork == XFS_COW_FORK) |
| 2746 | state |= BMAP_COWFORK; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2747 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | /* |
| 2750 | * Check and set flags if this segment has a left neighbor |
| 2751 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2752 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2753 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2754 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2755 | |
| 2756 | if (isnullstartblock(left.br_startblock)) |
| 2757 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | /* |
| 2761 | * Check and set flags if the current (right) segment exists. |
| 2762 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2763 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2764 | if (*idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2765 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2766 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2767 | |
| 2768 | if (isnullstartblock(right.br_startblock)) |
| 2769 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | /* |
| 2773 | * Set contiguity flags on the left and right neighbors. |
| 2774 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2775 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2776 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2777 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2778 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2779 | state |= BMAP_LEFT_CONTIG; |
| 2780 | |
| 2781 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2782 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2783 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2784 | (!(state & BMAP_LEFT_CONTIG) || |
| 2785 | (left.br_blockcount + new->br_blockcount + |
| 2786 | right.br_blockcount <= MAXEXTLEN))) |
| 2787 | state |= BMAP_RIGHT_CONTIG; |
| 2788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | /* |
| 2790 | * Switch out based on the contiguity flags. |
| 2791 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2792 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2793 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | /* |
| 2795 | * New allocation is contiguous with delayed allocations |
| 2796 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2797 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2799 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | temp = left.br_blockcount + new->br_blockcount + |
| 2801 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2802 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2803 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2804 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2805 | oldlen = startblockval(left.br_startblock) + |
| 2806 | startblockval(new->br_startblock) + |
| 2807 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame^] | 2808 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2809 | oldlen); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2810 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2811 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2812 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2813 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2814 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | break; |
| 2816 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2817 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | /* |
| 2819 | * New allocation is contiguous with a delayed allocation |
| 2820 | * on the left. |
| 2821 | * Merge the new allocation with the left neighbor. |
| 2822 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2823 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2825 | |
| 2826 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2827 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2828 | oldlen = startblockval(left.br_startblock) + |
| 2829 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame^] | 2830 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2831 | oldlen); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2832 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2833 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2834 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | break; |
| 2836 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2837 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | /* |
| 2839 | * New allocation is contiguous with a delayed allocation |
| 2840 | * on the right. |
| 2841 | * Merge the new allocation with the right neighbor. |
| 2842 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2843 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2845 | oldlen = startblockval(new->br_startblock) + |
| 2846 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame^] | 2847 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2848 | oldlen); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2849 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 2850 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2851 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2852 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | break; |
| 2854 | |
| 2855 | case 0: |
| 2856 | /* |
| 2857 | * New allocation is not contiguous with another |
| 2858 | * delayed allocation. |
| 2859 | * Insert a new entry. |
| 2860 | */ |
| 2861 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2862 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | break; |
| 2864 | } |
| 2865 | if (oldlen != newlen) { |
| 2866 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2867 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2868 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | /* |
| 2870 | * Nothing to do for disk quota accounting here. |
| 2871 | */ |
| 2872 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2876 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | */ |
| 2878 | STATIC int /* error */ |
| 2879 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2880 | struct xfs_bmalloca *bma, |
| 2881 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | { |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2883 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | int i; /* temp state */ |
| 2886 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2887 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2888 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2889 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | int state; /* state bits, accessed thru macros */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2891 | struct xfs_mount *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 2893 | mp = bma->ip->i_mount; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2894 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2895 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2896 | ASSERT(bma->idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2897 | ASSERT(bma->idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2898 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2899 | ASSERT(!bma->cur || |
| 2900 | !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2901 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2902 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2903 | |
| 2904 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2905 | if (whichfork == XFS_ATTR_FORK) |
| 2906 | state |= BMAP_ATTRFORK; |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 2907 | if (whichfork == XFS_COW_FORK) |
| 2908 | state |= BMAP_COWFORK; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | /* |
| 2911 | * Check and set flags if this segment has a left neighbor. |
| 2912 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2913 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2914 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2915 | 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] | 2916 | if (isnullstartblock(left.br_startblock)) |
| 2917 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | /* |
| 2921 | * Check and set flags if this segment has a current value. |
| 2922 | * Not true if we're inserting into the "hole" at eof. |
| 2923 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2924 | if (bma->idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2925 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2926 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2927 | if (isnullstartblock(right.br_startblock)) |
| 2928 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | /* |
| 2932 | * We're inserting a real allocation between "left" and "right". |
| 2933 | * Set the contiguity flags. Don't let extents get too large. |
| 2934 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2935 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2936 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2937 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2938 | left.br_state == new->br_state && |
| 2939 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2940 | state |= BMAP_LEFT_CONTIG; |
| 2941 | |
| 2942 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2943 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2944 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2945 | new->br_state == right.br_state && |
| 2946 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2947 | (!(state & BMAP_LEFT_CONTIG) || |
| 2948 | left.br_blockcount + new->br_blockcount + |
| 2949 | right.br_blockcount <= MAXEXTLEN)) |
| 2950 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2952 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | /* |
| 2954 | * Select which case we're in here, and implement it. |
| 2955 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2956 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2957 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | /* |
| 2959 | * New allocation is contiguous with real allocations on the |
| 2960 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2961 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2963 | --bma->idx; |
| 2964 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2965 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | left.br_blockcount + new->br_blockcount + |
| 2967 | right.br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2968 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2969 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2970 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2971 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2972 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 2973 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1); |
| 2974 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2975 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2976 | } else { |
| 2977 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2978 | error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff, |
| 2979 | right.br_startblock, right.br_blockcount, |
| 2980 | &i); |
| 2981 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2982 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2983 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2984 | error = xfs_btree_delete(bma->cur, &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 | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2988 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 2989 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2990 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2991 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2992 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2993 | left.br_startblock, |
| 2994 | left.br_blockcount + |
| 2995 | new->br_blockcount + |
| 2996 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2997 | left.br_state); |
| 2998 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2999 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3001 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3003 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3004 | /* |
| 3005 | * New allocation is contiguous with a real allocation |
| 3006 | * on the left. |
| 3007 | * Merge the new allocation with the left neighbor. |
| 3008 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3009 | --bma->idx; |
| 3010 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3011 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3013 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3014 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3015 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3016 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3017 | } else { |
| 3018 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3019 | error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff, |
| 3020 | left.br_startblock, left.br_blockcount, |
| 3021 | &i); |
| 3022 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3023 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3024 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3025 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3026 | left.br_startblock, |
| 3027 | left.br_blockcount + |
| 3028 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3029 | left.br_state); |
| 3030 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3031 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3033 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3035 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | /* |
| 3037 | * New allocation is contiguous with a real allocation |
| 3038 | * on the right. |
| 3039 | * Merge the new allocation with the right neighbor. |
| 3040 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3041 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3042 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 3043 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | new->br_blockcount + right.br_blockcount, |
| 3045 | right.br_state); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3046 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3047 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3048 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3049 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3050 | } else { |
| 3051 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3052 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3053 | right.br_startoff, |
| 3054 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3055 | right.br_blockcount, &i); |
| 3056 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3057 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3058 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3059 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3060 | new->br_startblock, |
| 3061 | new->br_blockcount + |
| 3062 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3063 | right.br_state); |
| 3064 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3065 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3067 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | |
| 3069 | case 0: |
| 3070 | /* |
| 3071 | * New allocation is not contiguous with another |
| 3072 | * real allocation. |
| 3073 | * Insert a new entry. |
| 3074 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3075 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 3076 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 3077 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1); |
| 3078 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3079 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3080 | } else { |
| 3081 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3082 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3083 | new->br_startoff, |
| 3084 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3085 | new->br_blockcount, &i); |
| 3086 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3087 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3088 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3089 | bma->cur->bc_rec.b.br_state = new->br_state; |
| 3090 | error = xfs_btree_insert(bma->cur, &i); |
| 3091 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3092 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3093 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3095 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3097 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 3098 | /* add reverse mapping */ |
| 3099 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 3100 | if (error) |
| 3101 | goto done; |
| 3102 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3103 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3104 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3105 | int tmp_logflags; /* partial log flag return val */ |
| 3106 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3107 | ASSERT(bma->cur == NULL); |
| 3108 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3109 | bma->firstblock, bma->dfops, &bma->cur, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3110 | 0, &tmp_logflags, whichfork); |
| 3111 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3112 | if (error) |
| 3113 | goto done; |
| 3114 | } |
| 3115 | |
| 3116 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3117 | if (bma->cur) |
| 3118 | bma->cur->bc_private.b.allocated = 0; |
| 3119 | |
| 3120 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3121 | done: |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3122 | bma->logflags |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3123 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | } |
| 3125 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3126 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 3127 | * Functions used in the extent read, allocate and remove paths |
| 3128 | */ |
| 3129 | |
| 3130 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3131 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 3132 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3133 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3134 | xfs_bmap_extsize_align( |
| 3135 | xfs_mount_t *mp, |
| 3136 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 3137 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 3138 | xfs_extlen_t extsz, /* align to this extent size */ |
| 3139 | int rt, /* is this a realtime inode? */ |
| 3140 | int eof, /* is extent at end-of-file? */ |
| 3141 | int delay, /* creating delalloc extent? */ |
| 3142 | int convert, /* overwriting unwritten extent? */ |
| 3143 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 3144 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 3145 | { |
| 3146 | xfs_fileoff_t orig_off; /* original offset */ |
| 3147 | xfs_extlen_t orig_alen; /* original length */ |
| 3148 | xfs_fileoff_t orig_end; /* original off+len */ |
| 3149 | xfs_fileoff_t nexto; /* next file offset */ |
| 3150 | xfs_fileoff_t prevo; /* previous file offset */ |
| 3151 | xfs_fileoff_t align_off; /* temp for offset */ |
| 3152 | xfs_extlen_t align_alen; /* temp for length */ |
| 3153 | xfs_extlen_t temp; /* temp for calculations */ |
| 3154 | |
| 3155 | if (convert) |
| 3156 | return 0; |
| 3157 | |
| 3158 | orig_off = align_off = *offp; |
| 3159 | orig_alen = align_alen = *lenp; |
| 3160 | orig_end = orig_off + orig_alen; |
| 3161 | |
| 3162 | /* |
| 3163 | * If this request overlaps an existing extent, then don't |
| 3164 | * attempt to perform any additional alignment. |
| 3165 | */ |
| 3166 | if (!delay && !eof && |
| 3167 | (orig_off >= gotp->br_startoff) && |
| 3168 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 3169 | return 0; |
| 3170 | } |
| 3171 | |
| 3172 | /* |
| 3173 | * If the file offset is unaligned vs. the extent size |
| 3174 | * we need to align it. This will be possible unless |
| 3175 | * the file was previously written with a kernel that didn't |
| 3176 | * perform this alignment, or if a truncate shot us in the |
| 3177 | * foot. |
| 3178 | */ |
| 3179 | temp = do_mod(orig_off, extsz); |
| 3180 | if (temp) { |
| 3181 | align_alen += temp; |
| 3182 | align_off -= temp; |
| 3183 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3184 | |
| 3185 | /* Same adjustment for the end of the requested area. */ |
| 3186 | temp = (align_alen % extsz); |
| 3187 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3188 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3189 | |
| 3190 | /* |
| 3191 | * For large extent hint sizes, the aligned extent might be larger than |
| 3192 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 3193 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 3194 | * short allocation just fine, so it is safe to do this. We only want to |
| 3195 | * do it when we are forced to, though, because it means more allocation |
| 3196 | * operations are required. |
| 3197 | */ |
| 3198 | while (align_alen > MAXEXTLEN) |
| 3199 | align_alen -= extsz; |
| 3200 | ASSERT(align_alen <= MAXEXTLEN); |
| 3201 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3202 | /* |
| 3203 | * If the previous block overlaps with this proposed allocation |
| 3204 | * then move the start forward without adjusting the length. |
| 3205 | */ |
| 3206 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3207 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3208 | prevo = prevp->br_startoff; |
| 3209 | else |
| 3210 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3211 | } else |
| 3212 | prevo = 0; |
| 3213 | if (align_off != orig_off && align_off < prevo) |
| 3214 | align_off = prevo; |
| 3215 | /* |
| 3216 | * If the next block overlaps with this proposed allocation |
| 3217 | * then move the start back without adjusting the length, |
| 3218 | * but not before offset 0. |
| 3219 | * This may of course make the start overlap previous block, |
| 3220 | * and if we hit the offset 0 limit then the next block |
| 3221 | * can still overlap too. |
| 3222 | */ |
| 3223 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3224 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3225 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3226 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3227 | else |
| 3228 | nexto = gotp->br_startoff; |
| 3229 | } else |
| 3230 | nexto = NULLFILEOFF; |
| 3231 | if (!eof && |
| 3232 | align_off + align_alen != orig_end && |
| 3233 | align_off + align_alen > nexto) |
| 3234 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3235 | /* |
| 3236 | * If we're now overlapping the next or previous extent that |
| 3237 | * means we can't fit an extsz piece in this hole. Just move |
| 3238 | * the start forward to the first valid spot and set |
| 3239 | * the length so we hit the end. |
| 3240 | */ |
| 3241 | if (align_off != orig_off && align_off < prevo) |
| 3242 | align_off = prevo; |
| 3243 | if (align_off + align_alen != orig_end && |
| 3244 | align_off + align_alen > nexto && |
| 3245 | nexto != NULLFILEOFF) { |
| 3246 | ASSERT(nexto > prevo); |
| 3247 | align_alen = nexto - align_off; |
| 3248 | } |
| 3249 | |
| 3250 | /* |
| 3251 | * If realtime, and the result isn't a multiple of the realtime |
| 3252 | * extent size we need to remove blocks until it is. |
| 3253 | */ |
| 3254 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3255 | /* |
| 3256 | * We're not covering the original request, or |
| 3257 | * we won't be able to once we fix the length. |
| 3258 | */ |
| 3259 | if (orig_off < align_off || |
| 3260 | orig_end > align_off + align_alen || |
| 3261 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3262 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3263 | /* |
| 3264 | * Try to fix it by moving the start up. |
| 3265 | */ |
| 3266 | if (align_off + temp <= orig_off) { |
| 3267 | align_alen -= temp; |
| 3268 | align_off += temp; |
| 3269 | } |
| 3270 | /* |
| 3271 | * Try to fix it by moving the end in. |
| 3272 | */ |
| 3273 | else if (align_off + align_alen - temp >= orig_end) |
| 3274 | align_alen -= temp; |
| 3275 | /* |
| 3276 | * Set the start to the minimum then trim the length. |
| 3277 | */ |
| 3278 | else { |
| 3279 | align_alen -= orig_off - align_off; |
| 3280 | align_off = orig_off; |
| 3281 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3282 | } |
| 3283 | /* |
| 3284 | * Result doesn't cover the request, fail it. |
| 3285 | */ |
| 3286 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3287 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3288 | } else { |
| 3289 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3290 | /* see MAXEXTLEN handling above */ |
| 3291 | ASSERT(orig_end <= align_off + align_alen || |
| 3292 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | #ifdef DEBUG |
| 3296 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3297 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3298 | if (prevp->br_startoff != NULLFILEOFF) |
| 3299 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3300 | #endif |
| 3301 | |
| 3302 | *lenp = align_alen; |
| 3303 | *offp = align_off; |
| 3304 | return 0; |
| 3305 | } |
| 3306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3308 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3309 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3310 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3311 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | { |
| 3313 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3315 | xfs_mount_t *mp; /* mount point structure */ |
| 3316 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3317 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | |
| 3319 | #define ISVALID(x,y) \ |
| 3320 | (rt ? \ |
| 3321 | (x) < mp->m_sb.sb_rblocks : \ |
| 3322 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3323 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3324 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3327 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3328 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3329 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3330 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | /* |
| 3332 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3333 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3335 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3336 | !isnullstartblock(ap->prev.br_startblock) && |
| 3337 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3338 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3339 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | /* |
| 3341 | * Adjust for the gap between prevp and us. |
| 3342 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3343 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3344 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3346 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3347 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | } |
| 3349 | /* |
| 3350 | * If not at eof, then compare the two neighbor blocks. |
| 3351 | * Figure out whether either one gives us a good starting point, |
| 3352 | * and pick the better one. |
| 3353 | */ |
| 3354 | else if (!ap->eof) { |
| 3355 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3356 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3357 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3358 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3359 | |
| 3360 | /* |
| 3361 | * If there's a previous (left) block, select a requested |
| 3362 | * start block based on it. |
| 3363 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3364 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3365 | !isnullstartblock(ap->prev.br_startblock) && |
| 3366 | (prevbno = ap->prev.br_startblock + |
| 3367 | ap->prev.br_blockcount) && |
| 3368 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | /* |
| 3370 | * Calculate gap to end of previous block. |
| 3371 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3372 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3373 | (ap->prev.br_startoff + |
| 3374 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | /* |
| 3376 | * Figure the startblock based on the previous block's |
| 3377 | * end and the gap size. |
| 3378 | * Heuristic! |
| 3379 | * If the gap is large relative to the piece we're |
| 3380 | * allocating, or using it gives us an invalid block |
| 3381 | * number, then just use the end of the previous block. |
| 3382 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3383 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3385 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | prevbno += adjust; |
| 3387 | else |
| 3388 | prevdiff += adjust; |
| 3389 | /* |
| 3390 | * If the firstblock forbids it, can't use it, |
| 3391 | * must use default. |
| 3392 | */ |
| 3393 | if (!rt && !nullfb && |
| 3394 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3395 | prevbno = NULLFSBLOCK; |
| 3396 | } |
| 3397 | /* |
| 3398 | * No previous block or can't follow it, just default. |
| 3399 | */ |
| 3400 | else |
| 3401 | prevbno = NULLFSBLOCK; |
| 3402 | /* |
| 3403 | * If there's a following (right) block, select a requested |
| 3404 | * start block based on it. |
| 3405 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3406 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | /* |
| 3408 | * Calculate gap to start of next block. |
| 3409 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3410 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | /* |
| 3412 | * Figure the startblock based on the next block's |
| 3413 | * start and the gap size. |
| 3414 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3415 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | /* |
| 3417 | * Heuristic! |
| 3418 | * If the gap is large relative to the piece we're |
| 3419 | * allocating, or using it gives us an invalid block |
| 3420 | * number, then just use the start of the next block |
| 3421 | * offset by our length. |
| 3422 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3423 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3425 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3426 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3427 | gotbno -= ap->length; |
| 3428 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | } else |
| 3430 | gotdiff += adjust; |
| 3431 | /* |
| 3432 | * If the firstblock forbids it, can't use it, |
| 3433 | * must use default. |
| 3434 | */ |
| 3435 | if (!rt && !nullfb && |
| 3436 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3437 | gotbno = NULLFSBLOCK; |
| 3438 | } |
| 3439 | /* |
| 3440 | * No next block, just default. |
| 3441 | */ |
| 3442 | else |
| 3443 | gotbno = NULLFSBLOCK; |
| 3444 | /* |
| 3445 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3446 | * 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] | 3447 | */ |
| 3448 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3449 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3451 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3453 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3455 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3456 | } |
| 3457 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3458 | static int |
| 3459 | xfs_bmap_longest_free_extent( |
| 3460 | struct xfs_trans *tp, |
| 3461 | xfs_agnumber_t ag, |
| 3462 | xfs_extlen_t *blen, |
| 3463 | int *notinit) |
| 3464 | { |
| 3465 | struct xfs_mount *mp = tp->t_mountp; |
| 3466 | struct xfs_perag *pag; |
| 3467 | xfs_extlen_t longest; |
| 3468 | int error = 0; |
| 3469 | |
| 3470 | pag = xfs_perag_get(mp, ag); |
| 3471 | if (!pag->pagf_init) { |
| 3472 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3473 | if (error) |
| 3474 | goto out; |
| 3475 | |
| 3476 | if (!pag->pagf_init) { |
| 3477 | *notinit = 1; |
| 3478 | goto out; |
| 3479 | } |
| 3480 | } |
| 3481 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 3482 | longest = xfs_alloc_longest_free_extent(mp, pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3483 | xfs_alloc_min_freelist(mp, pag), |
| 3484 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3485 | if (*blen < longest) |
| 3486 | *blen = longest; |
| 3487 | |
| 3488 | out: |
| 3489 | xfs_perag_put(pag); |
| 3490 | return error; |
| 3491 | } |
| 3492 | |
| 3493 | static void |
| 3494 | xfs_bmap_select_minlen( |
| 3495 | struct xfs_bmalloca *ap, |
| 3496 | struct xfs_alloc_arg *args, |
| 3497 | xfs_extlen_t *blen, |
| 3498 | int notinit) |
| 3499 | { |
| 3500 | if (notinit || *blen < ap->minlen) { |
| 3501 | /* |
| 3502 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3503 | * there is space for this request. |
| 3504 | */ |
| 3505 | args->minlen = ap->minlen; |
| 3506 | } else if (*blen < args->maxlen) { |
| 3507 | /* |
| 3508 | * If the best seen length is less than the request length, |
| 3509 | * use the best as the minimum. |
| 3510 | */ |
| 3511 | args->minlen = *blen; |
| 3512 | } else { |
| 3513 | /* |
| 3514 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3515 | * as the minimum. |
| 3516 | */ |
| 3517 | args->minlen = args->maxlen; |
| 3518 | } |
| 3519 | } |
| 3520 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3521 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3522 | xfs_bmap_btalloc_nullfb( |
| 3523 | struct xfs_bmalloca *ap, |
| 3524 | struct xfs_alloc_arg *args, |
| 3525 | xfs_extlen_t *blen) |
| 3526 | { |
| 3527 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3528 | xfs_agnumber_t ag, startag; |
| 3529 | int notinit = 0; |
| 3530 | int error; |
| 3531 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3532 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3533 | args->total = ap->total; |
| 3534 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3535 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3536 | if (startag == NULLAGNUMBER) |
| 3537 | startag = ag = 0; |
| 3538 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3539 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3540 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3541 | ¬init); |
| 3542 | if (error) |
| 3543 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3544 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3545 | if (++ag == mp->m_sb.sb_agcount) |
| 3546 | ag = 0; |
| 3547 | if (ag == startag) |
| 3548 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3549 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3550 | |
| 3551 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3552 | return 0; |
| 3553 | } |
| 3554 | |
| 3555 | STATIC int |
| 3556 | xfs_bmap_btalloc_filestreams( |
| 3557 | struct xfs_bmalloca *ap, |
| 3558 | struct xfs_alloc_arg *args, |
| 3559 | xfs_extlen_t *blen) |
| 3560 | { |
| 3561 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3562 | xfs_agnumber_t ag; |
| 3563 | int notinit = 0; |
| 3564 | int error; |
| 3565 | |
| 3566 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3567 | args->total = ap->total; |
| 3568 | |
| 3569 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3570 | if (ag == NULLAGNUMBER) |
| 3571 | ag = 0; |
| 3572 | |
| 3573 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3574 | if (error) |
| 3575 | return error; |
| 3576 | |
| 3577 | if (*blen < args->maxlen) { |
| 3578 | error = xfs_filestream_new_ag(ap, &ag); |
| 3579 | if (error) |
| 3580 | return error; |
| 3581 | |
| 3582 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3583 | ¬init); |
| 3584 | if (error) |
| 3585 | return error; |
| 3586 | |
| 3587 | } |
| 3588 | |
| 3589 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3590 | |
| 3591 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3592 | * Set the failure fallback case to look in the selected AG as stream |
| 3593 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3594 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3595 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3596 | return 0; |
| 3597 | } |
| 3598 | |
| 3599 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3600 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3601 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3602 | { |
| 3603 | xfs_mount_t *mp; /* mount point structure */ |
| 3604 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3605 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3606 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3607 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3608 | xfs_alloc_arg_t args; |
| 3609 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3610 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3611 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3612 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3613 | int tryagain; |
| 3614 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3615 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3616 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3617 | ASSERT(ap->length); |
| 3618 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3619 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3620 | |
| 3621 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3622 | stripe_align = 0; |
| 3623 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3624 | stripe_align = mp->m_swidth; |
| 3625 | else if (mp->m_dalign) |
| 3626 | stripe_align = mp->m_dalign; |
| 3627 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3628 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3629 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3630 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3631 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3632 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3633 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3634 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3635 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3636 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3637 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3638 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3639 | |
| 3640 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3641 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3642 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3643 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3644 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3645 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3646 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3647 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3648 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3649 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3650 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3651 | } |
| 3652 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3653 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3654 | |
| 3655 | xfs_bmap_adjacent(ap); |
| 3656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3658 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3659 | * it's in the right allocation group. |
| 3660 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3661 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | ; |
| 3663 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3664 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | * Normal allocation, done through xfs_alloc_vextent. |
| 3667 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3668 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3669 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3670 | args.tp = ap->tp; |
| 3671 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3672 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3673 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3674 | |
| 3675 | /* Trim the allocation back to the maximum an AG can fit. */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 3676 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3677 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3678 | blen = 0; |
| 3679 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3680 | /* |
| 3681 | * Search for an allocation group with a single extent large |
| 3682 | * enough for the request. If one isn't found, then adjust |
| 3683 | * the minimum allocation size to the largest space found. |
| 3684 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3685 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3686 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3687 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3688 | else |
| 3689 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3690 | if (error) |
| 3691 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3692 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3693 | if (xfs_inode_is_filestream(ap->ip)) |
| 3694 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3695 | else |
| 3696 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3697 | args.total = args.minlen = ap->minlen; |
| 3698 | } else { |
| 3699 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3700 | args.total = ap->total; |
| 3701 | args.minlen = ap->minlen; |
| 3702 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3703 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3704 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3705 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3706 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3707 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3708 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3709 | args.prod = 1; |
| 3710 | args.mod = 0; |
| 3711 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3712 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3713 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3714 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3715 | } |
| 3716 | /* |
| 3717 | * If we are not low on available data blocks, and the |
| 3718 | * underlying logical volume manager is a stripe, and |
| 3719 | * the file offset is zero then try to allocate data |
| 3720 | * blocks on stripe unit boundary. |
| 3721 | * NOTE: ap->aeof is only set if the allocation length |
| 3722 | * is >= the stripe unit and the allocation offset is |
| 3723 | * at the end of file. |
| 3724 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3725 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3726 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3727 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3728 | atype = args.type; |
| 3729 | isaligned = 1; |
| 3730 | /* |
| 3731 | * Adjust for alignment |
| 3732 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3733 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3734 | args.minlen = blen - args.alignment; |
| 3735 | args.minalignslop = 0; |
| 3736 | } else { |
| 3737 | /* |
| 3738 | * First try an exact bno allocation. |
| 3739 | * If it fails then do a near or start bno |
| 3740 | * allocation with alignment turned on. |
| 3741 | */ |
| 3742 | atype = args.type; |
| 3743 | tryagain = 1; |
| 3744 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3745 | args.alignment = 1; |
| 3746 | /* |
| 3747 | * Compute the minlen+alignment for the |
| 3748 | * next case. Set slop so that the value |
| 3749 | * of minlen+alignment+slop doesn't go up |
| 3750 | * between the calls. |
| 3751 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3752 | if (blen > stripe_align && blen <= args.maxlen) |
| 3753 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3754 | else |
| 3755 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3756 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3757 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3758 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3759 | args.minlen - 1; |
| 3760 | else |
| 3761 | args.minalignslop = 0; |
| 3762 | } |
| 3763 | } else { |
| 3764 | args.alignment = 1; |
| 3765 | args.minalignslop = 0; |
| 3766 | } |
| 3767 | args.minleft = ap->minleft; |
| 3768 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3769 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3770 | args.datatype = ap->datatype; |
| 3771 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3772 | args.ip = ap->ip; |
| 3773 | |
| 3774 | error = xfs_alloc_vextent(&args); |
| 3775 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3776 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3777 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3778 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3779 | /* |
| 3780 | * Exact allocation failed. Now try with alignment |
| 3781 | * turned on. |
| 3782 | */ |
| 3783 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3784 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3785 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3786 | args.minlen = nextminlen; |
| 3787 | args.minalignslop = 0; |
| 3788 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | if ((error = xfs_alloc_vextent(&args))) |
| 3790 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3791 | } |
| 3792 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3793 | /* |
| 3794 | * allocation failed, so turn off alignment and |
| 3795 | * try again. |
| 3796 | */ |
| 3797 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3798 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3799 | args.alignment = 0; |
| 3800 | if ((error = xfs_alloc_vextent(&args))) |
| 3801 | return error; |
| 3802 | } |
| 3803 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3804 | args.minlen > ap->minlen) { |
| 3805 | args.minlen = ap->minlen; |
| 3806 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3807 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3808 | if ((error = xfs_alloc_vextent(&args))) |
| 3809 | return error; |
| 3810 | } |
| 3811 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3812 | args.fsbno = 0; |
| 3813 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3814 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3815 | if ((error = xfs_alloc_vextent(&args))) |
| 3816 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3817 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3818 | } |
| 3819 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3820 | /* |
| 3821 | * check the allocation happened at the same or higher AG than |
| 3822 | * the first block that was allocated. |
| 3823 | */ |
| 3824 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
| 3825 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == |
| 3826 | XFS_FSB_TO_AGNO(mp, args.fsbno) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3827 | (ap->dfops->dop_low && |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3828 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < |
| 3829 | XFS_FSB_TO_AGNO(mp, args.fsbno))); |
| 3830 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3831 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3832 | if (*ap->firstblock == NULLFSBLOCK) |
| 3833 | *ap->firstblock = args.fsbno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3834 | ASSERT(nullfb || fb_agno == args.agno || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3835 | (ap->dfops->dop_low && fb_agno < args.agno)); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3836 | ap->length = args.len; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 3837 | if (!(ap->flags & XFS_BMAPI_COWFORK)) |
| 3838 | ap->ip->i_d.di_nblocks += args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3839 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3840 | if (ap->wasdel) |
| 3841 | ap->ip->i_delayed_blks -= args.len; |
| 3842 | /* |
| 3843 | * Adjust the disk quota also. This was reserved |
| 3844 | * earlier. |
| 3845 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3846 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3847 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3848 | XFS_TRANS_DQ_BCOUNT, |
| 3849 | (long) args.len); |
| 3850 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3851 | ap->blkno = NULLFSBLOCK; |
| 3852 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | } |
| 3854 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | /* |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 3858 | * For a remap operation, just "allocate" an extent at the address that the |
| 3859 | * caller passed in, and ensure that the AGFL is the right size. The caller |
| 3860 | * will then map the "allocated" extent into the file somewhere. |
| 3861 | */ |
| 3862 | STATIC int |
| 3863 | xfs_bmap_remap_alloc( |
| 3864 | struct xfs_bmalloca *ap) |
| 3865 | { |
| 3866 | struct xfs_trans *tp = ap->tp; |
| 3867 | struct xfs_mount *mp = tp->t_mountp; |
| 3868 | xfs_agblock_t bno; |
| 3869 | struct xfs_alloc_arg args; |
| 3870 | int error; |
| 3871 | |
| 3872 | /* |
| 3873 | * validate that the block number is legal - the enables us to detect |
| 3874 | * and handle a silent filesystem corruption rather than crashing. |
| 3875 | */ |
| 3876 | memset(&args, 0, sizeof(struct xfs_alloc_arg)); |
| 3877 | args.tp = ap->tp; |
| 3878 | args.mp = ap->tp->t_mountp; |
| 3879 | bno = *ap->firstblock; |
| 3880 | args.agno = XFS_FSB_TO_AGNO(mp, bno); |
| 3881 | args.agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 3882 | if (args.agno >= mp->m_sb.sb_agcount || |
| 3883 | args.agbno >= mp->m_sb.sb_agblocks) |
| 3884 | return -EFSCORRUPTED; |
| 3885 | |
| 3886 | /* "Allocate" the extent from the range we passed in. */ |
| 3887 | trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length); |
| 3888 | ap->blkno = bno; |
| 3889 | ap->ip->i_d.di_nblocks += ap->length; |
| 3890 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3891 | |
| 3892 | /* Fix the freelist, like a real allocator does. */ |
| 3893 | args.datatype = ap->datatype; |
| 3894 | args.pag = xfs_perag_get(args.mp, args.agno); |
| 3895 | ASSERT(args.pag); |
| 3896 | |
| 3897 | /* |
| 3898 | * The freelist fixing code will decline the allocation if |
| 3899 | * the size and shape of the free space doesn't allow for |
| 3900 | * allocating the extent and updating all the metadata that |
| 3901 | * happens during an allocation. We're remapping, not |
| 3902 | * allocating, so skip that check by pretending to be freeing. |
| 3903 | */ |
| 3904 | error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING); |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 3905 | xfs_perag_put(args.pag); |
| 3906 | if (error) |
| 3907 | trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_); |
| 3908 | return error; |
| 3909 | } |
| 3910 | |
| 3911 | /* |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3912 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3913 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3914 | */ |
| 3915 | STATIC int |
| 3916 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3917 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3918 | { |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 3919 | if (ap->flags & XFS_BMAPI_REMAP) |
| 3920 | return xfs_bmap_remap_alloc(ap); |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3921 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3922 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3923 | return xfs_bmap_rtalloc(ap); |
| 3924 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3925 | } |
| 3926 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3927 | /* Trim extent to fit a logical block range. */ |
| 3928 | void |
| 3929 | xfs_trim_extent( |
| 3930 | struct xfs_bmbt_irec *irec, |
| 3931 | xfs_fileoff_t bno, |
| 3932 | xfs_filblks_t len) |
| 3933 | { |
| 3934 | xfs_fileoff_t distance; |
| 3935 | xfs_fileoff_t end = bno + len; |
| 3936 | |
| 3937 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3938 | irec->br_startoff >= end) { |
| 3939 | irec->br_blockcount = 0; |
| 3940 | return; |
| 3941 | } |
| 3942 | |
| 3943 | if (irec->br_startoff < bno) { |
| 3944 | distance = bno - irec->br_startoff; |
| 3945 | if (isnullstartblock(irec->br_startblock)) |
| 3946 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3947 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3948 | irec->br_startblock != HOLESTARTBLOCK) |
| 3949 | irec->br_startblock += distance; |
| 3950 | irec->br_startoff += distance; |
| 3951 | irec->br_blockcount -= distance; |
| 3952 | } |
| 3953 | |
| 3954 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3955 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3956 | irec->br_blockcount -= distance; |
| 3957 | } |
| 3958 | } |
| 3959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3961 | * Trim the returned map to the required bounds |
| 3962 | */ |
| 3963 | STATIC void |
| 3964 | xfs_bmapi_trim_map( |
| 3965 | struct xfs_bmbt_irec *mval, |
| 3966 | struct xfs_bmbt_irec *got, |
| 3967 | xfs_fileoff_t *bno, |
| 3968 | xfs_filblks_t len, |
| 3969 | xfs_fileoff_t obno, |
| 3970 | xfs_fileoff_t end, |
| 3971 | int n, |
| 3972 | int flags) |
| 3973 | { |
| 3974 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3975 | got->br_startoff + got->br_blockcount <= obno) { |
| 3976 | *mval = *got; |
| 3977 | if (isnullstartblock(got->br_startblock)) |
| 3978 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3979 | return; |
| 3980 | } |
| 3981 | |
| 3982 | if (obno > *bno) |
| 3983 | *bno = obno; |
| 3984 | ASSERT((*bno >= obno) || (n == 0)); |
| 3985 | ASSERT(*bno < end); |
| 3986 | mval->br_startoff = *bno; |
| 3987 | if (isnullstartblock(got->br_startblock)) |
| 3988 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3989 | else |
| 3990 | mval->br_startblock = got->br_startblock + |
| 3991 | (*bno - got->br_startoff); |
| 3992 | /* |
| 3993 | * Return the minimum of what we got and what we asked for for |
| 3994 | * the length. We can use the len variable here because it is |
| 3995 | * modified below and we could have been there before coming |
| 3996 | * here if the first part of the allocation didn't overlap what |
| 3997 | * was asked for. |
| 3998 | */ |
| 3999 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 4000 | got->br_blockcount - (*bno - got->br_startoff)); |
| 4001 | mval->br_state = got->br_state; |
| 4002 | ASSERT(mval->br_blockcount <= len); |
| 4003 | return; |
| 4004 | } |
| 4005 | |
| 4006 | /* |
| 4007 | * Update and validate the extent map to return |
| 4008 | */ |
| 4009 | STATIC void |
| 4010 | xfs_bmapi_update_map( |
| 4011 | struct xfs_bmbt_irec **map, |
| 4012 | xfs_fileoff_t *bno, |
| 4013 | xfs_filblks_t *len, |
| 4014 | xfs_fileoff_t obno, |
| 4015 | xfs_fileoff_t end, |
| 4016 | int *n, |
| 4017 | int flags) |
| 4018 | { |
| 4019 | xfs_bmbt_irec_t *mval = *map; |
| 4020 | |
| 4021 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 4022 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 4023 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 4024 | (mval->br_startoff < obno)); |
| 4025 | |
| 4026 | *bno = mval->br_startoff + mval->br_blockcount; |
| 4027 | *len = end - *bno; |
| 4028 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 4029 | /* update previous map with new information */ |
| 4030 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 4031 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 4032 | ASSERT(mval->br_state == mval[-1].br_state); |
| 4033 | mval[-1].br_blockcount = mval->br_blockcount; |
| 4034 | mval[-1].br_state = mval->br_state; |
| 4035 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 4036 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 4037 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 4038 | mval->br_startblock == mval[-1].br_startblock + |
| 4039 | mval[-1].br_blockcount && |
| 4040 | ((flags & XFS_BMAPI_IGSTATE) || |
| 4041 | mval[-1].br_state == mval->br_state)) { |
| 4042 | ASSERT(mval->br_startoff == |
| 4043 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 4044 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4045 | } else if (*n > 0 && |
| 4046 | mval->br_startblock == DELAYSTARTBLOCK && |
| 4047 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 4048 | mval->br_startoff == |
| 4049 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 4050 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4051 | mval[-1].br_state = mval->br_state; |
| 4052 | } else if (!((*n == 0) && |
| 4053 | ((mval->br_startoff + mval->br_blockcount) <= |
| 4054 | obno))) { |
| 4055 | mval++; |
| 4056 | (*n)++; |
| 4057 | } |
| 4058 | *map = mval; |
| 4059 | } |
| 4060 | |
| 4061 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4062 | * Map file blocks to filesystem blocks without allocation. |
| 4063 | */ |
| 4064 | int |
| 4065 | xfs_bmapi_read( |
| 4066 | struct xfs_inode *ip, |
| 4067 | xfs_fileoff_t bno, |
| 4068 | xfs_filblks_t len, |
| 4069 | struct xfs_bmbt_irec *mval, |
| 4070 | int *nmap, |
| 4071 | int flags) |
| 4072 | { |
| 4073 | struct xfs_mount *mp = ip->i_mount; |
| 4074 | struct xfs_ifork *ifp; |
| 4075 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4076 | xfs_fileoff_t obno; |
| 4077 | xfs_fileoff_t end; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4078 | xfs_extnum_t idx; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4079 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4080 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4081 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4082 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4083 | |
| 4084 | ASSERT(*nmap >= 1); |
| 4085 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4086 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4087 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4088 | |
| 4089 | if (unlikely(XFS_TEST_ERROR( |
| 4090 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4091 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 4092 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4093 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4094 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4098 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4099 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4100 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4101 | |
| 4102 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4103 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4104 | /* No CoW fork? Return a hole. */ |
| 4105 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 4106 | mval->br_startoff = bno; |
| 4107 | mval->br_startblock = HOLESTARTBLOCK; |
| 4108 | mval->br_blockcount = len; |
| 4109 | mval->br_state = XFS_EXT_NORM; |
| 4110 | *nmap = 1; |
| 4111 | return 0; |
| 4112 | } |
| 4113 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4114 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4115 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4116 | if (error) |
| 4117 | return error; |
| 4118 | } |
| 4119 | |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4120 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) |
| 4121 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4122 | end = bno + len; |
| 4123 | obno = bno; |
| 4124 | |
| 4125 | while (bno < end && n < *nmap) { |
| 4126 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4127 | if (eof) |
| 4128 | got.br_startoff = end; |
| 4129 | if (got.br_startoff > bno) { |
| 4130 | /* Reading in a hole. */ |
| 4131 | mval->br_startoff = bno; |
| 4132 | mval->br_startblock = HOLESTARTBLOCK; |
| 4133 | mval->br_blockcount = |
| 4134 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4135 | mval->br_state = XFS_EXT_NORM; |
| 4136 | bno += mval->br_blockcount; |
| 4137 | len -= mval->br_blockcount; |
| 4138 | mval++; |
| 4139 | n++; |
| 4140 | continue; |
| 4141 | } |
| 4142 | |
| 4143 | /* set up the extent map to return. */ |
| 4144 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4145 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4146 | |
| 4147 | /* If we're done, stop now. */ |
| 4148 | if (bno >= end || n >= *nmap) |
| 4149 | break; |
| 4150 | |
| 4151 | /* Else go on to the next record. */ |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4152 | if (!xfs_iext_get_extent(ifp, ++idx, &got)) |
| 4153 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4154 | } |
| 4155 | *nmap = n; |
| 4156 | return 0; |
| 4157 | } |
| 4158 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 4159 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4160 | xfs_bmapi_reserve_delalloc( |
| 4161 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4162 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4163 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4164 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4165 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4166 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4167 | xfs_extnum_t *lastx, |
| 4168 | int eof) |
| 4169 | { |
| 4170 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4171 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4172 | xfs_extlen_t alen; |
| 4173 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4174 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4175 | xfs_extlen_t extsz; |
| 4176 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4177 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4178 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4179 | /* |
| 4180 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 4181 | * tag the inode before we return. |
| 4182 | */ |
| 4183 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4184 | if (!eof) |
| 4185 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4186 | if (prealloc && alen >= len) |
| 4187 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4188 | |
| 4189 | /* Figure out the extent size, adjust alen */ |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 4190 | if (whichfork == XFS_COW_FORK) |
| 4191 | extsz = xfs_get_cowextsz_hint(ip); |
| 4192 | else |
| 4193 | extsz = xfs_get_extsz_hint(ip); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4194 | if (extsz) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 4195 | struct xfs_bmbt_irec prev; |
| 4196 | |
| 4197 | if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev)) |
| 4198 | prev.br_startoff = NULLFILEOFF; |
| 4199 | |
| 4200 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4201 | 1, 0, &aoff, &alen); |
| 4202 | ASSERT(!error); |
| 4203 | } |
| 4204 | |
| 4205 | if (rt) |
| 4206 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4207 | |
| 4208 | /* |
| 4209 | * Make a transaction-less quota reservation for delayed allocation |
| 4210 | * blocks. This number gets adjusted later. We return if we haven't |
| 4211 | * allocated blocks already inside this loop. |
| 4212 | */ |
| 4213 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4214 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4215 | if (error) |
| 4216 | return error; |
| 4217 | |
| 4218 | /* |
| 4219 | * Split changing sb for alen and indlen since they could be coming |
| 4220 | * from different places. |
| 4221 | */ |
| 4222 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4223 | ASSERT(indlen > 0); |
| 4224 | |
| 4225 | if (rt) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4226 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4227 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4228 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | if (error) |
| 4232 | goto out_unreserve_quota; |
| 4233 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4234 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4235 | if (error) |
| 4236 | goto out_unreserve_blocks; |
| 4237 | |
| 4238 | |
| 4239 | ip->i_delayed_blks += alen; |
| 4240 | |
| 4241 | got->br_startoff = aoff; |
| 4242 | got->br_startblock = nullstartblock(indlen); |
| 4243 | got->br_blockcount = alen; |
| 4244 | got->br_state = XFS_EXT_NORM; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4245 | xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4246 | |
| 4247 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4248 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay |
| 4249 | * might have merged it into one of the neighbouring ones. |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4250 | */ |
| 4251 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); |
| 4252 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4253 | /* |
| 4254 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 4255 | * preallocation can occur at the start or end of the extent, even when |
| 4256 | * prealloc == 0, so we must also check the aligned offset and length. |
| 4257 | */ |
| 4258 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4259 | xfs_inode_set_eofblocks_tag(ip); |
| 4260 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4261 | xfs_inode_set_cowblocks_tag(ip); |
| 4262 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4263 | ASSERT(got->br_startoff <= aoff); |
| 4264 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); |
| 4265 | ASSERT(isnullstartblock(got->br_startblock)); |
| 4266 | ASSERT(got->br_state == XFS_EXT_NORM); |
| 4267 | return 0; |
| 4268 | |
| 4269 | out_unreserve_blocks: |
| 4270 | if (rt) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4271 | xfs_mod_frextents(mp, extsz); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4272 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4273 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4274 | out_unreserve_quota: |
| 4275 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 4276 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4277 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4278 | return error; |
| 4279 | } |
| 4280 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4281 | static int |
| 4282 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4283 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4284 | { |
| 4285 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4286 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4287 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4288 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4289 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4290 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4291 | ASSERT(bma->length > 0); |
| 4292 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4293 | /* |
| 4294 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4295 | * for in this bmap call but that wouldn't be as good. |
| 4296 | */ |
| 4297 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4298 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4299 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | 0e8d630 | 2016-11-24 11:40:32 +1100 | [diff] [blame] | 4300 | if (bma->idx) { |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4301 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4302 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4303 | } |
| 4304 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4305 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4306 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4307 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4308 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
| 4311 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4312 | * Set the data type being allocated. For the data fork, the first data |
| 4313 | * in the file is treated differently to all other allocations. For the |
| 4314 | * attribute fork, we only need to ensure the allocated range is not on |
| 4315 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4316 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4317 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4318 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4319 | if (whichfork == XFS_DATA_FORK) { |
| 4320 | if (bma->offset == 0) |
| 4321 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4322 | else |
| 4323 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4324 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4325 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4326 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4329 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4330 | |
| 4331 | /* |
| 4332 | * Only want to do the alignment at the eof if it is userdata and |
| 4333 | * allocation length is larger than a stripe unit. |
| 4334 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4335 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4336 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4337 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4338 | if (error) |
| 4339 | return error; |
| 4340 | } |
| 4341 | |
| 4342 | error = xfs_bmap_alloc(bma); |
| 4343 | if (error) |
| 4344 | return error; |
| 4345 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4346 | if (bma->cur) |
| 4347 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4348 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4349 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4350 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4351 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4352 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4353 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4354 | } |
| 4355 | /* |
| 4356 | * Bump the number of extents we've allocated |
| 4357 | * in this call. |
| 4358 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4359 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4360 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4361 | if (bma->cur) |
| 4362 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4363 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4364 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4365 | bma->got.br_startoff = bma->offset; |
| 4366 | bma->got.br_startblock = bma->blkno; |
| 4367 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4368 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4369 | |
| 4370 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4371 | * In the data fork, a wasdelay extent has been initialized, so |
| 4372 | * shouldn't be flagged as unwritten. |
| 4373 | * |
| 4374 | * For the cow fork, however, we convert delalloc reservations |
| 4375 | * (extents allocated for speculative preallocation) to |
| 4376 | * allocated unwritten extents, and only convert the unwritten |
| 4377 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4378 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4379 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4380 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4381 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4382 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4383 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4384 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4385 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4386 | else |
| 4387 | error = xfs_bmap_add_extent_hole_real(bma, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4388 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4389 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4390 | if (error) |
| 4391 | return error; |
| 4392 | |
| 4393 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4394 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4395 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4396 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4397 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4398 | 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] | 4399 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4400 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4401 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4402 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4403 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4404 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4405 | return 0; |
| 4406 | } |
| 4407 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4408 | STATIC int |
| 4409 | xfs_bmapi_convert_unwritten( |
| 4410 | struct xfs_bmalloca *bma, |
| 4411 | struct xfs_bmbt_irec *mval, |
| 4412 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4413 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4414 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4415 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4416 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4417 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4418 | int error; |
| 4419 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4420 | /* check if we need to do unwritten->real conversion */ |
| 4421 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4422 | (flags & XFS_BMAPI_PREALLOC)) |
| 4423 | return 0; |
| 4424 | |
| 4425 | /* check if we need to do real->unwritten conversion */ |
| 4426 | if (mval->br_state == XFS_EXT_NORM && |
| 4427 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4428 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4429 | return 0; |
| 4430 | |
| 4431 | /* |
| 4432 | * Modify (by adding) the state flag, if writing. |
| 4433 | */ |
| 4434 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4435 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4436 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4437 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4438 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4439 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4440 | } |
| 4441 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4442 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4443 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4444 | /* |
| 4445 | * Before insertion into the bmbt, zero the range being converted |
| 4446 | * if required. |
| 4447 | */ |
| 4448 | if (flags & XFS_BMAPI_ZERO) { |
| 4449 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4450 | mval->br_blockcount); |
| 4451 | if (error) |
| 4452 | return error; |
| 4453 | } |
| 4454 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4455 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
| 4456 | &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4457 | &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4458 | /* |
| 4459 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4460 | * path because the conversion might not have done so (e.g., if the |
| 4461 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4462 | * in the transaction for the sake of fsync(), even if nothing has |
| 4463 | * changed, because fsync() will not force the log for this transaction |
| 4464 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4465 | * |
| 4466 | * Note: If we're only converting cow fork extents, there aren't |
| 4467 | * 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] | 4468 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4469 | if (whichfork != XFS_COW_FORK) |
| 4470 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4471 | if (error) |
| 4472 | return error; |
| 4473 | |
| 4474 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4475 | * Update our extent pointer, given that |
| 4476 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4477 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4478 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4479 | 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] | 4480 | |
| 4481 | /* |
| 4482 | * We may have combined previously unwritten space with written space, |
| 4483 | * so generate another request. |
| 4484 | */ |
| 4485 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4486 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4487 | return 0; |
| 4488 | } |
| 4489 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4490 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4491 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4492 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4493 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4494 | * locking problems. |
| 4495 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | * The returned value in "firstblock" from the first call in a transaction |
| 4497 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4498 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4499 | * the first call in "total"; if no allocation group has that many free |
| 4500 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4501 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4502 | int |
| 4503 | xfs_bmapi_write( |
| 4504 | struct xfs_trans *tp, /* transaction pointer */ |
| 4505 | struct xfs_inode *ip, /* incore inode */ |
| 4506 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4507 | xfs_filblks_t len, /* length to map in file */ |
| 4508 | int flags, /* XFS_BMAPI_... */ |
| 4509 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4510 | controls a.g. for allocs */ |
| 4511 | xfs_extlen_t total, /* total blocks needed */ |
| 4512 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4513 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4514 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4516 | struct xfs_mount *mp = ip->i_mount; |
| 4517 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4518 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4519 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4520 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4521 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4522 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4523 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4524 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4527 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4528 | int orig_flags; /* original flags arg value */ |
| 4529 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4530 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4531 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | |
| 4533 | orig_bno = bno; |
| 4534 | orig_len = len; |
| 4535 | orig_flags = flags; |
| 4536 | orig_mval = mval; |
| 4537 | orig_nmap = *nmap; |
| 4538 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4539 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4542 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4543 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4544 | ASSERT(tp != NULL || |
| 4545 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4546 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4547 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4548 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4549 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4550 | ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK); |
| 4551 | ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP)); |
| 4552 | ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4553 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4554 | /* zeroing is for currently only for data extents, not metadata */ |
| 4555 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4556 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4557 | /* |
| 4558 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4559 | * but it makes no sense to do both at once. This would result in |
| 4560 | * zeroing the unwritten extent twice, but it still being an |
| 4561 | * unwritten extent.... |
| 4562 | */ |
| 4563 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4564 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | if (unlikely(XFS_TEST_ERROR( |
| 4567 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4568 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4570 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4571 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4575 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4578 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4579 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4580 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4581 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4583 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4584 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4585 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4586 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4587 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
| 4590 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4591 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4592 | if (error) |
| 4593 | goto error0; |
| 4594 | } |
| 4595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | n = 0; |
| 4597 | end = bno + len; |
| 4598 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4599 | |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4600 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got)) |
| 4601 | eof = true; |
| 4602 | if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev)) |
| 4603 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4604 | bma.tp = tp; |
| 4605 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4606 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4607 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4608 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4609 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4612 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4613 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4614 | /* in hole or beyoned EOF? */ |
| 4615 | if (eof || bma.got.br_startoff > bno) { |
| 4616 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4617 | /* |
| 4618 | * For the COW fork we can reasonably get a |
| 4619 | * request for converting an extent that races |
| 4620 | * with other threads already having converted |
| 4621 | * part of it, as there converting COW to |
| 4622 | * regular blocks is not protected using the |
| 4623 | * IOLOCK. |
| 4624 | */ |
| 4625 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4626 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4627 | error = -EIO; |
| 4628 | goto error0; |
| 4629 | } |
| 4630 | |
| 4631 | if (eof || bno >= end) |
| 4632 | break; |
| 4633 | } else { |
| 4634 | need_alloc = true; |
| 4635 | } |
| 4636 | } else { |
| 4637 | /* |
| 4638 | * Make sure we only reflink into a hole. |
| 4639 | */ |
| 4640 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
| 4641 | if (isnullstartblock(bma.got.br_startblock)) |
| 4642 | wasdelay = true; |
| 4643 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4644 | |
| 4645 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | * First, deal with the hole before the allocated space |
| 4647 | * that we found, if any. |
| 4648 | */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4649 | if (need_alloc || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4650 | bma.eof = eof; |
| 4651 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4652 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4653 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4654 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4655 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4656 | /* |
| 4657 | * There's a 32/64 bit type mismatch between the |
| 4658 | * allocation length request (which can be 64 bits in |
| 4659 | * length) and the bma length request, which is |
| 4660 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4661 | * check for 32-bit overflows and handle them here. |
| 4662 | */ |
| 4663 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4664 | bma.length = MAXEXTLEN; |
| 4665 | else |
| 4666 | bma.length = len; |
| 4667 | |
| 4668 | ASSERT(len > 0); |
| 4669 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4670 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | if (error) |
| 4672 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4673 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4674 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4675 | |
| 4676 | /* |
| 4677 | * If this is a CoW allocation, record the data in |
| 4678 | * the refcount btree for orphan recovery. |
| 4679 | */ |
| 4680 | if (whichfork == XFS_COW_FORK) { |
| 4681 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4682 | bma.blkno, bma.length); |
| 4683 | if (error) |
| 4684 | goto error0; |
| 4685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4687 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4688 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4689 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4690 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4692 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4693 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4694 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4695 | continue; |
| 4696 | if (error) |
| 4697 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4698 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4699 | /* update the extent map to return */ |
| 4700 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | /* |
| 4703 | * If we're done, stop now. Stop when we've allocated |
| 4704 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4705 | * the transaction may get too big. |
| 4706 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4707 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4709 | |
| 4710 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4711 | bma.prev = bma.got; |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4712 | if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got)) |
| 4713 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | /* |
| 4718 | * Transform from btree to extents, give it cur. |
| 4719 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4720 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4721 | int tmp_logflags = 0; |
| 4722 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4723 | ASSERT(bma.cur); |
| 4724 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4726 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | if (error) |
| 4728 | goto error0; |
| 4729 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4732 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4733 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | error0: |
| 4736 | /* |
| 4737 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4738 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4740 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4742 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4743 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4744 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4745 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | /* |
| 4747 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4748 | * detecting a case where the data is changed, there's an error, |
| 4749 | * and it's not logged so we don't shutdown when we should. |
| 4750 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4751 | if (bma.logflags) |
| 4752 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4753 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4754 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | if (!error) { |
| 4756 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4757 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4758 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4759 | bma.cur->bc_private.b.firstblock) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4760 | (dfops->dop_low && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4762 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4763 | bma.cur->bc_private.b.firstblock))); |
| 4764 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4766 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4768 | } |
| 4769 | if (!error) |
| 4770 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4771 | orig_nmap, *nmap); |
| 4772 | return error; |
| 4773 | } |
| 4774 | |
| 4775 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4776 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4777 | * indlen reservation must be shared across the two new extents that are left |
| 4778 | * behind. |
| 4779 | * |
| 4780 | * Given the original reservation and the worst case indlen for the two new |
| 4781 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4782 | * reservation fairly across the two new extents. If necessary, steal available |
| 4783 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4784 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4785 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4786 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4787 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4788 | xfs_bmap_split_indlen( |
| 4789 | xfs_filblks_t ores, /* original res. */ |
| 4790 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4791 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4792 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4793 | { |
| 4794 | xfs_filblks_t len1 = *indlen1; |
| 4795 | xfs_filblks_t len2 = *indlen2; |
| 4796 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4797 | xfs_filblks_t stolen = 0; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4798 | |
| 4799 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4800 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4801 | * indlen for both new extents. |
| 4802 | */ |
| 4803 | while (nres > ores && avail) { |
| 4804 | nres--; |
| 4805 | avail--; |
| 4806 | stolen++; |
| 4807 | } |
| 4808 | |
| 4809 | /* |
| 4810 | * The only blocks available are those reserved for the original |
| 4811 | * extent and what we can steal from the extent being removed. |
| 4812 | * If this still isn't enough to satisfy the combined |
| 4813 | * requirements for the two new extents, skim blocks off of each |
| 4814 | * of the new reservations until they match what is available. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4815 | */ |
| 4816 | while (nres > ores) { |
| 4817 | if (len1) { |
| 4818 | len1--; |
| 4819 | nres--; |
| 4820 | } |
| 4821 | if (nres == ores) |
| 4822 | break; |
| 4823 | if (len2) { |
| 4824 | len2--; |
| 4825 | nres--; |
| 4826 | } |
| 4827 | } |
| 4828 | |
| 4829 | *indlen1 = len1; |
| 4830 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4831 | |
| 4832 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4833 | } |
| 4834 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4835 | int |
| 4836 | xfs_bmap_del_extent_delay( |
| 4837 | struct xfs_inode *ip, |
| 4838 | int whichfork, |
| 4839 | xfs_extnum_t *idx, |
| 4840 | struct xfs_bmbt_irec *got, |
| 4841 | struct xfs_bmbt_irec *del) |
| 4842 | { |
| 4843 | struct xfs_mount *mp = ip->i_mount; |
| 4844 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4845 | struct xfs_bmbt_irec new; |
| 4846 | int64_t da_old, da_new, da_diff = 0; |
| 4847 | xfs_fileoff_t del_endoff, got_endoff; |
| 4848 | xfs_filblks_t got_indlen, new_indlen, stolen; |
| 4849 | int error = 0, state = 0; |
| 4850 | bool isrt; |
| 4851 | |
| 4852 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4853 | |
| 4854 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4855 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4856 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4857 | da_old = startblockval(got->br_startblock); |
| 4858 | da_new = 0; |
| 4859 | |
| 4860 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 4861 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4862 | ASSERT(del->br_blockcount > 0); |
| 4863 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4864 | ASSERT(got_endoff >= del_endoff); |
| 4865 | |
| 4866 | if (isrt) { |
| 4867 | int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
| 4868 | |
| 4869 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4870 | xfs_mod_frextents(mp, rtexts); |
| 4871 | } |
| 4872 | |
| 4873 | /* |
| 4874 | * Update the inode delalloc counter now and wait to update the |
| 4875 | * sb counters as we might have to borrow some blocks for the |
| 4876 | * indirect block accounting. |
| 4877 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4878 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4879 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4880 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4881 | if (error) |
| 4882 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4883 | ip->i_delayed_blks -= del->br_blockcount; |
| 4884 | |
| 4885 | if (whichfork == XFS_COW_FORK) |
| 4886 | state |= BMAP_COWFORK; |
| 4887 | |
| 4888 | if (got->br_startoff == del->br_startoff) |
| 4889 | state |= BMAP_LEFT_CONTIG; |
| 4890 | if (got_endoff == del_endoff) |
| 4891 | state |= BMAP_RIGHT_CONTIG; |
| 4892 | |
| 4893 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 4894 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 4895 | /* |
| 4896 | * Matches the whole extent. Delete the entry. |
| 4897 | */ |
| 4898 | xfs_iext_remove(ip, *idx, 1, state); |
| 4899 | --*idx; |
| 4900 | break; |
| 4901 | case BMAP_LEFT_CONTIG: |
| 4902 | /* |
| 4903 | * Deleting the first part of the extent. |
| 4904 | */ |
| 4905 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4906 | got->br_startoff = del_endoff; |
| 4907 | got->br_blockcount -= del->br_blockcount; |
| 4908 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4909 | got->br_blockcount), da_old); |
| 4910 | got->br_startblock = nullstartblock((int)da_new); |
| 4911 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4912 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4913 | break; |
| 4914 | case BMAP_RIGHT_CONTIG: |
| 4915 | /* |
| 4916 | * Deleting the last part of the extent. |
| 4917 | */ |
| 4918 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4919 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4920 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4921 | got->br_blockcount), da_old); |
| 4922 | got->br_startblock = nullstartblock((int)da_new); |
| 4923 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4924 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4925 | break; |
| 4926 | case 0: |
| 4927 | /* |
| 4928 | * Deleting the middle of the extent. |
| 4929 | * |
| 4930 | * Distribute the original indlen reservation across the two new |
| 4931 | * extents. Steal blocks from the deleted extent if necessary. |
| 4932 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4933 | * Warn if either of the new indlen reservations is zero as this |
| 4934 | * can lead to delalloc problems. |
| 4935 | */ |
| 4936 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4937 | |
| 4938 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4939 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4940 | |
| 4941 | new.br_blockcount = got_endoff - del_endoff; |
| 4942 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4943 | |
| 4944 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4945 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4946 | del->br_blockcount); |
| 4947 | |
| 4948 | got->br_startblock = nullstartblock((int)got_indlen); |
| 4949 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4950 | trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_); |
| 4951 | |
| 4952 | new.br_startoff = del_endoff; |
| 4953 | new.br_state = got->br_state; |
| 4954 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4955 | |
| 4956 | ++*idx; |
| 4957 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 4958 | |
| 4959 | da_new = got_indlen + new_indlen - stolen; |
| 4960 | del->br_blockcount -= stolen; |
| 4961 | break; |
| 4962 | } |
| 4963 | |
| 4964 | ASSERT(da_old >= da_new); |
| 4965 | da_diff = da_old - da_new; |
| 4966 | if (!isrt) |
| 4967 | da_diff += del->br_blockcount; |
| 4968 | if (da_diff) |
| 4969 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4970 | return error; |
| 4971 | } |
| 4972 | |
| 4973 | void |
| 4974 | xfs_bmap_del_extent_cow( |
| 4975 | struct xfs_inode *ip, |
| 4976 | xfs_extnum_t *idx, |
| 4977 | struct xfs_bmbt_irec *got, |
| 4978 | struct xfs_bmbt_irec *del) |
| 4979 | { |
| 4980 | struct xfs_mount *mp = ip->i_mount; |
| 4981 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4982 | struct xfs_bmbt_irec new; |
| 4983 | xfs_fileoff_t del_endoff, got_endoff; |
| 4984 | int state = BMAP_COWFORK; |
| 4985 | |
| 4986 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4987 | |
| 4988 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4989 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4990 | |
| 4991 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 4992 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4993 | ASSERT(del->br_blockcount > 0); |
| 4994 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4995 | ASSERT(got_endoff >= del_endoff); |
| 4996 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4997 | |
| 4998 | if (got->br_startoff == del->br_startoff) |
| 4999 | state |= BMAP_LEFT_CONTIG; |
| 5000 | if (got_endoff == del_endoff) |
| 5001 | state |= BMAP_RIGHT_CONTIG; |
| 5002 | |
| 5003 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 5004 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 5005 | /* |
| 5006 | * Matches the whole extent. Delete the entry. |
| 5007 | */ |
| 5008 | xfs_iext_remove(ip, *idx, 1, state); |
| 5009 | --*idx; |
| 5010 | break; |
| 5011 | case BMAP_LEFT_CONTIG: |
| 5012 | /* |
| 5013 | * Deleting the first part of the extent. |
| 5014 | */ |
| 5015 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5016 | got->br_startoff = del_endoff; |
| 5017 | got->br_blockcount -= del->br_blockcount; |
| 5018 | got->br_startblock = del->br_startblock + del->br_blockcount; |
| 5019 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5020 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5021 | break; |
| 5022 | case BMAP_RIGHT_CONTIG: |
| 5023 | /* |
| 5024 | * Deleting the last part of the extent. |
| 5025 | */ |
| 5026 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5027 | got->br_blockcount -= del->br_blockcount; |
| 5028 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5029 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5030 | break; |
| 5031 | case 0: |
| 5032 | /* |
| 5033 | * Deleting the middle of the extent. |
| 5034 | */ |
| 5035 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5036 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 5037 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5038 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5039 | |
| 5040 | new.br_startoff = del_endoff; |
| 5041 | new.br_blockcount = got_endoff - del_endoff; |
| 5042 | new.br_state = got->br_state; |
| 5043 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 5044 | |
| 5045 | ++*idx; |
| 5046 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 5047 | break; |
| 5048 | } |
| 5049 | } |
| 5050 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5051 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5052 | * Called by xfs_bmapi to update file extent records and the btree |
| 5053 | * after removing space (or undoing a delayed allocation). |
| 5054 | */ |
| 5055 | STATIC int /* error */ |
| 5056 | xfs_bmap_del_extent( |
| 5057 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5058 | xfs_trans_t *tp, /* current transaction pointer */ |
| 5059 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5060 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5061 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 5062 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 5063 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5064 | int whichfork, /* data or attr fork */ |
| 5065 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5066 | { |
| 5067 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 5068 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 5069 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 5070 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 5071 | int delay; /* current block is delayed allocated */ |
| 5072 | int do_fx; /* free extent at end of routine */ |
| 5073 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
| 5074 | int error; /* error return value */ |
| 5075 | int flags; /* inode logging flags */ |
| 5076 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 5077 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 5078 | int i; /* temp state */ |
| 5079 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5080 | xfs_mount_t *mp; /* mount structure */ |
| 5081 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 5082 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 5083 | /* REFERENCED */ |
| 5084 | uint qfield; /* quota field to update */ |
| 5085 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 5086 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 5087 | int state = 0; |
| 5088 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5089 | mp = ip->i_mount; |
| 5090 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5091 | |
| 5092 | if (whichfork == XFS_ATTR_FORK) |
| 5093 | state |= BMAP_ATTRFORK; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5094 | else if (whichfork == XFS_COW_FORK) |
| 5095 | state |= BMAP_COWFORK; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5096 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5097 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5098 | ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp))); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5099 | ASSERT(del->br_blockcount > 0); |
| 5100 | ep = xfs_iext_get_ext(ifp, *idx); |
| 5101 | xfs_bmbt_get_all(ep, &got); |
| 5102 | ASSERT(got.br_startoff <= del->br_startoff); |
| 5103 | del_endoff = del->br_startoff + del->br_blockcount; |
| 5104 | got_endoff = got.br_startoff + got.br_blockcount; |
| 5105 | ASSERT(got_endoff >= del_endoff); |
| 5106 | delay = isnullstartblock(got.br_startblock); |
| 5107 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
| 5108 | flags = 0; |
| 5109 | qfield = 0; |
| 5110 | error = 0; |
| 5111 | /* |
| 5112 | * If deleting a real allocation, must free up the disk space. |
| 5113 | */ |
| 5114 | if (!delay) { |
| 5115 | flags = XFS_ILOG_CORE; |
| 5116 | /* |
| 5117 | * Realtime allocation. Free it and record di_nblocks update. |
| 5118 | */ |
| 5119 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 5120 | xfs_fsblock_t bno; |
| 5121 | xfs_filblks_t len; |
| 5122 | |
| 5123 | ASSERT(do_mod(del->br_blockcount, |
| 5124 | mp->m_sb.sb_rextsize) == 0); |
| 5125 | ASSERT(do_mod(del->br_startblock, |
| 5126 | mp->m_sb.sb_rextsize) == 0); |
| 5127 | bno = del->br_startblock; |
| 5128 | len = del->br_blockcount; |
| 5129 | do_div(bno, mp->m_sb.sb_rextsize); |
| 5130 | do_div(len, mp->m_sb.sb_rextsize); |
| 5131 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 5132 | if (error) |
| 5133 | goto done; |
| 5134 | do_fx = 0; |
| 5135 | nblks = len * mp->m_sb.sb_rextsize; |
| 5136 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 5137 | } |
| 5138 | /* |
| 5139 | * Ordinary allocation. |
| 5140 | */ |
| 5141 | else { |
| 5142 | do_fx = 1; |
| 5143 | nblks = del->br_blockcount; |
| 5144 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 5145 | } |
| 5146 | /* |
| 5147 | * Set up del_endblock and cur for later. |
| 5148 | */ |
| 5149 | del_endblock = del->br_startblock + del->br_blockcount; |
| 5150 | if (cur) { |
| 5151 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 5152 | got.br_startblock, got.br_blockcount, |
| 5153 | &i))) |
| 5154 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5155 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5156 | } |
| 5157 | da_old = da_new = 0; |
| 5158 | } else { |
| 5159 | da_old = startblockval(got.br_startblock); |
| 5160 | da_new = 0; |
| 5161 | nblks = 0; |
| 5162 | do_fx = 0; |
| 5163 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 5164 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5165 | /* |
| 5166 | * Set flag value to use in switch statement. |
| 5167 | * Left-contig is 2, right-contig is 1. |
| 5168 | */ |
| 5169 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 5170 | (got_endoff == del_endoff)) { |
| 5171 | case 3: |
| 5172 | /* |
| 5173 | * Matches the whole extent. Delete the entry. |
| 5174 | */ |
Darrick J. Wong | 4862cfe | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 5175 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5176 | xfs_iext_remove(ip, *idx, 1, |
| 5177 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 5178 | --*idx; |
| 5179 | if (delay) |
| 5180 | break; |
| 5181 | |
| 5182 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5183 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5184 | flags |= XFS_ILOG_CORE; |
| 5185 | if (!cur) { |
| 5186 | flags |= xfs_ilog_fext(whichfork); |
| 5187 | break; |
| 5188 | } |
| 5189 | if ((error = xfs_btree_delete(cur, &i))) |
| 5190 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5191 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5192 | break; |
| 5193 | |
| 5194 | case 2: |
| 5195 | /* |
| 5196 | * Deleting the first part of the extent. |
| 5197 | */ |
| 5198 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5199 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 5200 | temp = got.br_blockcount - del->br_blockcount; |
| 5201 | xfs_bmbt_set_blockcount(ep, temp); |
| 5202 | if (delay) { |
| 5203 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 5204 | da_old); |
| 5205 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 5206 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5207 | da_new = temp; |
| 5208 | break; |
| 5209 | } |
| 5210 | xfs_bmbt_set_startblock(ep, del_endblock); |
| 5211 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5212 | if (!cur) { |
| 5213 | flags |= xfs_ilog_fext(whichfork); |
| 5214 | break; |
| 5215 | } |
| 5216 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 5217 | got.br_blockcount - del->br_blockcount, |
| 5218 | got.br_state))) |
| 5219 | goto done; |
| 5220 | break; |
| 5221 | |
| 5222 | case 1: |
| 5223 | /* |
| 5224 | * Deleting the last part of the extent. |
| 5225 | */ |
| 5226 | temp = got.br_blockcount - del->br_blockcount; |
| 5227 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5228 | xfs_bmbt_set_blockcount(ep, temp); |
| 5229 | if (delay) { |
| 5230 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 5231 | da_old); |
| 5232 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 5233 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5234 | da_new = temp; |
| 5235 | break; |
| 5236 | } |
| 5237 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5238 | if (!cur) { |
| 5239 | flags |= xfs_ilog_fext(whichfork); |
| 5240 | break; |
| 5241 | } |
| 5242 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 5243 | got.br_startblock, |
| 5244 | got.br_blockcount - del->br_blockcount, |
| 5245 | got.br_state))) |
| 5246 | goto done; |
| 5247 | break; |
| 5248 | |
| 5249 | case 0: |
| 5250 | /* |
| 5251 | * Deleting the middle of the extent. |
| 5252 | */ |
| 5253 | temp = del->br_startoff - got.br_startoff; |
| 5254 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5255 | xfs_bmbt_set_blockcount(ep, temp); |
| 5256 | new.br_startoff = del_endoff; |
| 5257 | temp2 = got_endoff - del_endoff; |
| 5258 | new.br_blockcount = temp2; |
| 5259 | new.br_state = got.br_state; |
| 5260 | if (!delay) { |
| 5261 | new.br_startblock = del_endblock; |
| 5262 | flags |= XFS_ILOG_CORE; |
| 5263 | if (cur) { |
| 5264 | if ((error = xfs_bmbt_update(cur, |
| 5265 | got.br_startoff, |
| 5266 | got.br_startblock, temp, |
| 5267 | got.br_state))) |
| 5268 | goto done; |
| 5269 | if ((error = xfs_btree_increment(cur, 0, &i))) |
| 5270 | goto done; |
| 5271 | cur->bc_rec.b = new; |
| 5272 | error = xfs_btree_insert(cur, &i); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5273 | if (error && error != -ENOSPC) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5274 | goto done; |
| 5275 | /* |
| 5276 | * If get no-space back from btree insert, |
| 5277 | * it tried a split, and we have a zero |
| 5278 | * block reservation. |
| 5279 | * Fix up our state and return the error. |
| 5280 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5281 | if (error == -ENOSPC) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5282 | /* |
| 5283 | * Reset the cursor, don't trust |
| 5284 | * it after any insert operation. |
| 5285 | */ |
| 5286 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 5287 | got.br_startoff, |
| 5288 | got.br_startblock, |
| 5289 | temp, &i))) |
| 5290 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5291 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 5292 | i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5293 | /* |
| 5294 | * Update the btree record back |
| 5295 | * to the original value. |
| 5296 | */ |
| 5297 | if ((error = xfs_bmbt_update(cur, |
| 5298 | got.br_startoff, |
| 5299 | got.br_startblock, |
| 5300 | got.br_blockcount, |
| 5301 | got.br_state))) |
| 5302 | goto done; |
| 5303 | /* |
| 5304 | * Reset the extent record back |
| 5305 | * to the original value. |
| 5306 | */ |
| 5307 | xfs_bmbt_set_blockcount(ep, |
| 5308 | got.br_blockcount); |
| 5309 | flags = 0; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5310 | error = -ENOSPC; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5311 | goto done; |
| 5312 | } |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5313 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5314 | } else |
| 5315 | flags |= xfs_ilog_fext(whichfork); |
| 5316 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5317 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5318 | } else { |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5319 | xfs_filblks_t stolen; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5320 | ASSERT(whichfork == XFS_DATA_FORK); |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5321 | |
| 5322 | /* |
| 5323 | * Distribute the original indlen reservation across the |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5324 | * two new extents. Steal blocks from the deleted extent |
| 5325 | * if necessary. Stealing blocks simply fudges the |
| 5326 | * fdblocks accounting in xfs_bunmapi(). |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5327 | */ |
| 5328 | temp = xfs_bmap_worst_indlen(ip, got.br_blockcount); |
| 5329 | temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5330 | stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2, |
| 5331 | del->br_blockcount); |
| 5332 | da_new = temp + temp2 - stolen; |
| 5333 | del->br_blockcount -= stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5334 | |
| 5335 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5336 | * Set the reservation for each extent. Warn if either |
| 5337 | * is zero as this can lead to delalloc problems. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5338 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5339 | WARN_ON_ONCE(!temp || !temp2); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5340 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5341 | new.br_startblock = nullstartblock((int)temp2); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5342 | } |
| 5343 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5344 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 5345 | ++*idx; |
| 5346 | break; |
| 5347 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5348 | |
| 5349 | /* remove reverse mapping */ |
| 5350 | if (!delay) { |
| 5351 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5352 | if (error) |
| 5353 | goto done; |
| 5354 | } |
| 5355 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5356 | /* |
| 5357 | * If we need to, add to list of extents to delete. |
| 5358 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5359 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5360 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5361 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5362 | if (error) |
| 5363 | goto done; |
| 5364 | } else |
| 5365 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5366 | del->br_blockcount, NULL); |
| 5367 | } |
| 5368 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5369 | /* |
| 5370 | * Adjust inode # blocks in the file. |
| 5371 | */ |
| 5372 | if (nblks) |
| 5373 | ip->i_d.di_nblocks -= nblks; |
| 5374 | /* |
| 5375 | * Adjust quota data. |
| 5376 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5377 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5378 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5379 | |
| 5380 | /* |
| 5381 | * Account for change in delayed indirect blocks. |
| 5382 | * Nothing to do for disk quota accounting here. |
| 5383 | */ |
| 5384 | ASSERT(da_old >= da_new); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 5385 | if (da_old > da_new) |
| 5386 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5387 | done: |
| 5388 | *logflagsp = flags; |
| 5389 | return error; |
| 5390 | } |
| 5391 | |
| 5392 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | * Unmap (remove) blocks from a file. |
| 5394 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5395 | * that value. If not all extents in the block range can be removed then |
| 5396 | * *done is set. |
| 5397 | */ |
| 5398 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5399 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | xfs_trans_t *tp, /* transaction pointer */ |
| 5401 | struct xfs_inode *ip, /* incore inode */ |
| 5402 | xfs_fileoff_t bno, /* starting offset to unmap */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5403 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | int flags, /* misc flags */ |
| 5405 | xfs_extnum_t nexts, /* number of extents max */ |
| 5406 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5407 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5408 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | { |
| 5410 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5411 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | int error; /* error return value */ |
| 5413 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5414 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5416 | int isrt; /* freeing in rt area */ |
| 5417 | xfs_extnum_t lastx; /* last extent index used */ |
| 5418 | int logflags; /* transaction logging flags */ |
| 5419 | xfs_extlen_t mod; /* rt extent offset */ |
| 5420 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5422 | int tmp_logflags; /* partial logging flags */ |
| 5423 | int wasdel; /* was a delayed alloc extent */ |
| 5424 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5425 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5426 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5428 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5429 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5430 | whichfork = xfs_bmapi_whichfork(flags); |
| 5431 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5433 | if (unlikely( |
| 5434 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5435 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5436 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5437 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5438 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | } |
| 5440 | mp = ip->i_mount; |
| 5441 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5442 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5443 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5444 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | ASSERT(len > 0); |
| 5446 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5449 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5450 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5451 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5452 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | return 0; |
| 5454 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5455 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5456 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | start = bno; |
| 5458 | bno = start + len - 1; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | /* |
| 5461 | * Check to see if the given block number is past the end of the |
| 5462 | * file, back up to the last block if so... |
| 5463 | */ |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5464 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) { |
| 5465 | ASSERT(lastx > 0); |
| 5466 | xfs_iext_get_extent(ifp, --lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5468 | } |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | logflags = 0; |
| 5471 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5472 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5473 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5475 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | cur->bc_private.b.flags = 0; |
| 5477 | } else |
| 5478 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5479 | |
| 5480 | if (isrt) { |
| 5481 | /* |
| 5482 | * Synchronize by locking the bitmap inode. |
| 5483 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5484 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5485 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5486 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5487 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5488 | } |
| 5489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | extno = 0; |
| 5491 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5492 | (nexts == 0 || extno < nexts)) { |
| 5493 | /* |
| 5494 | * Is the found extent after a hole in which bno lives? |
| 5495 | * Just back up to the previous extent, if so. |
| 5496 | */ |
| 5497 | if (got.br_startoff > bno) { |
| 5498 | if (--lastx < 0) |
| 5499 | break; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5500 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | } |
| 5502 | /* |
| 5503 | * Is the last block of this extent before the range |
| 5504 | * we're supposed to delete? If so, we're done. |
| 5505 | */ |
| 5506 | bno = XFS_FILEOFF_MIN(bno, |
| 5507 | got.br_startoff + got.br_blockcount - 1); |
| 5508 | if (bno < start) |
| 5509 | break; |
| 5510 | /* |
| 5511 | * Then deal with the (possibly delayed) allocated space |
| 5512 | * we found. |
| 5513 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5515 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | if (got.br_startoff < start) { |
| 5517 | del.br_startoff = start; |
| 5518 | del.br_blockcount -= start - got.br_startoff; |
| 5519 | if (!wasdel) |
| 5520 | del.br_startblock += start - got.br_startoff; |
| 5521 | } |
| 5522 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5523 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5524 | sum = del.br_startblock + del.br_blockcount; |
| 5525 | if (isrt && |
| 5526 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5527 | /* |
| 5528 | * Realtime extent not lined up at the end. |
| 5529 | * The extent could have been split into written |
| 5530 | * and unwritten pieces, or we could just be |
| 5531 | * unmapping part of it. But we can't really |
| 5532 | * get rid of part of a realtime extent. |
| 5533 | */ |
| 5534 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5535 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | /* |
| 5537 | * This piece is unwritten, or we're not |
| 5538 | * using unwritten extents. Skip over it. |
| 5539 | */ |
| 5540 | ASSERT(bno >= mod); |
| 5541 | bno -= mod > del.br_blockcount ? |
| 5542 | del.br_blockcount : mod; |
| 5543 | if (bno < got.br_startoff) { |
| 5544 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5545 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5546 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | } |
| 5548 | continue; |
| 5549 | } |
| 5550 | /* |
| 5551 | * It's written, turn it unwritten. |
| 5552 | * This is better than zeroing it. |
| 5553 | */ |
| 5554 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5555 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | /* |
| 5557 | * If this spans a realtime extent boundary, |
| 5558 | * chop it back to the start of the one we end at. |
| 5559 | */ |
| 5560 | if (del.br_blockcount > mod) { |
| 5561 | del.br_startoff += del.br_blockcount - mod; |
| 5562 | del.br_startblock += del.br_blockcount - mod; |
| 5563 | del.br_blockcount = mod; |
| 5564 | } |
| 5565 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5566 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5567 | whichfork, &lastx, &cur, &del, |
| 5568 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | if (error) |
| 5570 | goto error0; |
| 5571 | goto nodelete; |
| 5572 | } |
| 5573 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5574 | /* |
| 5575 | * Realtime extent is lined up at the end but not |
| 5576 | * at the front. We'll get rid of full extents if |
| 5577 | * we can. |
| 5578 | */ |
| 5579 | mod = mp->m_sb.sb_rextsize - mod; |
| 5580 | if (del.br_blockcount > mod) { |
| 5581 | del.br_blockcount -= mod; |
| 5582 | del.br_startoff += mod; |
| 5583 | del.br_startblock += mod; |
| 5584 | } else if ((del.br_startoff == start && |
| 5585 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5586 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5587 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | /* |
| 5589 | * Can't make it unwritten. There isn't |
| 5590 | * a full extent here so just skip it. |
| 5591 | */ |
| 5592 | ASSERT(bno >= del.br_blockcount); |
| 5593 | bno -= del.br_blockcount; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5594 | if (got.br_startoff > bno && --lastx >= 0) |
| 5595 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | continue; |
| 5597 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5598 | struct xfs_bmbt_irec prev; |
| 5599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | /* |
| 5601 | * This one is already unwritten. |
| 5602 | * It must have a written left neighbor. |
| 5603 | * Unwrite the killed part of that one and |
| 5604 | * try again. |
| 5605 | */ |
| 5606 | ASSERT(lastx > 0); |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5607 | xfs_iext_get_extent(ifp, lastx - 1, &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5609 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | ASSERT(del.br_startblock == |
| 5611 | prev.br_startblock + prev.br_blockcount); |
| 5612 | if (prev.br_startoff < start) { |
| 5613 | mod = start - prev.br_startoff; |
| 5614 | prev.br_blockcount -= mod; |
| 5615 | prev.br_startblock += mod; |
| 5616 | prev.br_startoff = start; |
| 5617 | } |
| 5618 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5619 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5620 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5621 | ip, whichfork, &lastx, &cur, |
| 5622 | &prev, firstblock, dfops, |
| 5623 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | if (error) |
| 5625 | goto error0; |
| 5626 | goto nodelete; |
| 5627 | } else { |
| 5628 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5629 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5630 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5631 | ip, whichfork, &lastx, &cur, |
| 5632 | &del, firstblock, dfops, |
| 5633 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | if (error) |
| 5635 | goto error0; |
| 5636 | goto nodelete; |
| 5637 | } |
| 5638 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | /* |
| 5641 | * If it's the case where the directory code is running |
| 5642 | * with no block reservation, and the deleted block is in |
| 5643 | * the middle of its extent, and the resulting insert |
| 5644 | * of an extent would cause transformation to btree format, |
| 5645 | * then reject it. The calling code will then swap |
| 5646 | * blocks around instead. |
| 5647 | * We have to do this now, rather than waiting for the |
| 5648 | * conversion to btree format, since the transaction |
| 5649 | * will be dirty. |
| 5650 | */ |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5651 | if (!wasdel && tp->t_blk_res == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5653 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ |
| 5654 | XFS_IFORK_MAXEXT(ip, whichfork) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | del.br_startoff > got.br_startoff && |
| 5656 | del.br_startoff + del.br_blockcount < |
| 5657 | got.br_startoff + got.br_blockcount) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5658 | error = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | goto error0; |
| 5660 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5661 | |
| 5662 | /* |
| 5663 | * Unreserve quota and update realtime free space, if |
| 5664 | * appropriate. If delayed allocation, update the inode delalloc |
| 5665 | * counter now and wait to update the sb counters as |
| 5666 | * xfs_bmap_del_extent() might need to borrow some blocks. |
| 5667 | */ |
| 5668 | if (wasdel) { |
| 5669 | ASSERT(startblockval(del.br_startblock) > 0); |
| 5670 | if (isrt) { |
| 5671 | xfs_filblks_t rtexts; |
| 5672 | |
| 5673 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5674 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5675 | xfs_mod_frextents(mp, (int64_t)rtexts); |
| 5676 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5677 | ip, -((long)del.br_blockcount), 0, |
| 5678 | XFS_QMOPT_RES_RTBLKS); |
| 5679 | } else { |
| 5680 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5681 | ip, -((long)del.br_blockcount), 0, |
| 5682 | XFS_QMOPT_RES_REGBLKS); |
| 5683 | } |
| 5684 | ip->i_delayed_blks -= del.br_blockcount; |
| 5685 | if (cur) |
| 5686 | cur->bc_private.b.flags |= |
| 5687 | XFS_BTCUR_BPRV_WASDEL; |
| 5688 | } else if (cur) |
| 5689 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5690 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5691 | error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del, |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5692 | &tmp_logflags, whichfork, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | logflags |= tmp_logflags; |
| 5694 | if (error) |
| 5695 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5696 | |
| 5697 | if (!isrt && wasdel) |
| 5698 | xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false); |
| 5699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | bno = del.br_startoff - 1; |
| 5701 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | /* |
| 5703 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5706 | if (lastx >= 0) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5707 | xfs_iext_get_extent(ifp, lastx, &got); |
| 5708 | if (got.br_startoff > bno && --lastx >= 0) |
| 5709 | xfs_iext_get_extent(ifp, lastx, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | extno++; |
| 5712 | } |
| 5713 | } |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5714 | if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0) |
| 5715 | *rlen = 0; |
| 5716 | else |
| 5717 | *rlen = bno - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5719 | /* |
| 5720 | * Convert to a btree if necessary. |
| 5721 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5722 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5724 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | &cur, 0, &tmp_logflags, whichfork); |
| 5726 | logflags |= tmp_logflags; |
| 5727 | if (error) |
| 5728 | goto error0; |
| 5729 | } |
| 5730 | /* |
| 5731 | * transform from btree to extents, give it cur |
| 5732 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5733 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | ASSERT(cur != NULL); |
| 5735 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5736 | whichfork); |
| 5737 | logflags |= tmp_logflags; |
| 5738 | if (error) |
| 5739 | goto error0; |
| 5740 | } |
| 5741 | /* |
| 5742 | * transform from extents to local? |
| 5743 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | error = 0; |
| 5745 | error0: |
| 5746 | /* |
| 5747 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5748 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5750 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5751 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5752 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5753 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5754 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5755 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | /* |
| 5757 | * Log inode even in the error case, if the transaction |
| 5758 | * is dirty we'll need to shut down the filesystem. |
| 5759 | */ |
| 5760 | if (logflags) |
| 5761 | xfs_trans_log_inode(tp, ip, logflags); |
| 5762 | if (cur) { |
| 5763 | if (!error) { |
| 5764 | *firstblock = cur->bc_private.b.firstblock; |
| 5765 | cur->bc_private.b.allocated = 0; |
| 5766 | } |
| 5767 | xfs_btree_del_cursor(cur, |
| 5768 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5769 | } |
| 5770 | return error; |
| 5771 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5772 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5773 | /* Unmap a range of a file. */ |
| 5774 | int |
| 5775 | xfs_bunmapi( |
| 5776 | xfs_trans_t *tp, |
| 5777 | struct xfs_inode *ip, |
| 5778 | xfs_fileoff_t bno, |
| 5779 | xfs_filblks_t len, |
| 5780 | int flags, |
| 5781 | xfs_extnum_t nexts, |
| 5782 | xfs_fsblock_t *firstblock, |
| 5783 | struct xfs_defer_ops *dfops, |
| 5784 | int *done) |
| 5785 | { |
| 5786 | int error; |
| 5787 | |
| 5788 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5789 | dfops); |
| 5790 | *done = (len == 0); |
| 5791 | return error; |
| 5792 | } |
| 5793 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5794 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5795 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5796 | * extent that precedes the target hole of the shift. |
| 5797 | */ |
| 5798 | STATIC bool |
| 5799 | xfs_bmse_can_merge( |
| 5800 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5801 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5802 | xfs_fileoff_t shift) /* shift fsb */ |
| 5803 | { |
| 5804 | xfs_fileoff_t startoff; |
| 5805 | |
| 5806 | startoff = got->br_startoff - shift; |
| 5807 | |
| 5808 | /* |
| 5809 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5810 | * the preceding extent. |
| 5811 | */ |
| 5812 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5813 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5814 | (left->br_state != got->br_state) || |
| 5815 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5816 | return false; |
| 5817 | |
| 5818 | return true; |
| 5819 | } |
| 5820 | |
| 5821 | /* |
| 5822 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5823 | * hole in the file. If an extent shift would result in the extent being fully |
| 5824 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5825 | * the preceding extent rather than do the shift. |
| 5826 | * |
| 5827 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5828 | * with the provided extents via xfs_bmse_can_merge(). |
| 5829 | */ |
| 5830 | STATIC int |
| 5831 | xfs_bmse_merge( |
| 5832 | struct xfs_inode *ip, |
| 5833 | int whichfork, |
| 5834 | xfs_fileoff_t shift, /* shift fsb */ |
| 5835 | int current_ext, /* idx of gotp */ |
| 5836 | struct xfs_bmbt_rec_host *gotp, /* extent to shift */ |
| 5837 | struct xfs_bmbt_rec_host *leftp, /* preceding extent */ |
| 5838 | struct xfs_btree_cur *cur, |
| 5839 | int *logflags) /* output */ |
| 5840 | { |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5841 | struct xfs_bmbt_irec got; |
| 5842 | struct xfs_bmbt_irec left; |
| 5843 | xfs_filblks_t blockcount; |
| 5844 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5845 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5846 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5847 | xfs_bmbt_get_all(gotp, &got); |
| 5848 | xfs_bmbt_get_all(leftp, &left); |
| 5849 | blockcount = left.br_blockcount + got.br_blockcount; |
| 5850 | |
| 5851 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5852 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 5853 | ASSERT(xfs_bmse_can_merge(&left, &got, shift)); |
| 5854 | |
| 5855 | /* |
| 5856 | * Merge the in-core extents. Note that the host record pointers and |
| 5857 | * current_ext index are invalid once the extent has been removed via |
| 5858 | * xfs_iext_remove(). |
| 5859 | */ |
| 5860 | xfs_bmbt_set_blockcount(leftp, blockcount); |
| 5861 | xfs_iext_remove(ip, current_ext, 1, 0); |
| 5862 | |
| 5863 | /* |
| 5864 | * Update the on-disk extent count, the btree if necessary and log the |
| 5865 | * inode. |
| 5866 | */ |
| 5867 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5868 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5869 | *logflags |= XFS_ILOG_CORE; |
| 5870 | if (!cur) { |
| 5871 | *logflags |= XFS_ILOG_DEXT; |
| 5872 | return 0; |
| 5873 | } |
| 5874 | |
| 5875 | /* lookup and remove the extent to merge */ |
| 5876 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5877 | got.br_blockcount, &i); |
| 5878 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5879 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5880 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5881 | |
| 5882 | error = xfs_btree_delete(cur, &i); |
| 5883 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5884 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5885 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5886 | |
| 5887 | /* lookup and update size of the previous extent */ |
| 5888 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock, |
| 5889 | left.br_blockcount, &i); |
| 5890 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5891 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5892 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5893 | |
| 5894 | left.br_blockcount = blockcount; |
| 5895 | |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5896 | return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock, |
| 5897 | left.br_blockcount, left.br_state); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | /* |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5901 | * Shift a single extent. |
| 5902 | */ |
| 5903 | STATIC int |
| 5904 | xfs_bmse_shift_one( |
| 5905 | struct xfs_inode *ip, |
| 5906 | int whichfork, |
| 5907 | xfs_fileoff_t offset_shift_fsb, |
| 5908 | int *current_ext, |
| 5909 | struct xfs_bmbt_rec_host *gotp, |
| 5910 | struct xfs_btree_cur *cur, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5911 | int *logflags, |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5912 | enum shift_direction direction, |
| 5913 | struct xfs_defer_ops *dfops) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5914 | { |
| 5915 | struct xfs_ifork *ifp; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5916 | struct xfs_mount *mp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5917 | xfs_fileoff_t startoff; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5918 | struct xfs_bmbt_rec_host *adj_irecp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5919 | struct xfs_bmbt_irec got; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5920 | struct xfs_bmbt_irec adj_irec; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5921 | int error; |
| 5922 | int i; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5923 | int total_extents; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5924 | |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5925 | mp = ip->i_mount; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5926 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5927 | total_extents = xfs_iext_count(ifp); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5928 | |
| 5929 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5930 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5931 | /* delalloc extents should be prevented by caller */ |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5932 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5933 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5934 | if (direction == SHIFT_LEFT) { |
| 5935 | startoff = got.br_startoff - offset_shift_fsb; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5936 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5937 | /* |
| 5938 | * Check for merge if we've got an extent to the left, |
| 5939 | * otherwise make sure there's enough room at the start |
| 5940 | * of the file for the shift. |
| 5941 | */ |
| 5942 | if (!*current_ext) { |
| 5943 | if (got.br_startoff < offset_shift_fsb) |
| 5944 | return -EINVAL; |
| 5945 | goto update_current_ext; |
| 5946 | } |
| 5947 | /* |
| 5948 | * grab the left extent and check for a large |
| 5949 | * enough hole. |
| 5950 | */ |
| 5951 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1); |
| 5952 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5953 | |
| 5954 | if (startoff < |
| 5955 | adj_irec.br_startoff + adj_irec.br_blockcount) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5956 | return -EINVAL; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5957 | |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5958 | /* check whether to merge the extent or shift it down */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5959 | if (xfs_bmse_can_merge(&adj_irec, &got, |
| 5960 | offset_shift_fsb)) { |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5961 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
| 5962 | *current_ext, gotp, adj_irecp, |
| 5963 | cur, logflags); |
| 5964 | if (error) |
| 5965 | return error; |
| 5966 | adj_irec = got; |
| 5967 | goto update_rmap; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5968 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5969 | } else { |
| 5970 | startoff = got.br_startoff + offset_shift_fsb; |
| 5971 | /* nothing to move if this is the last extent */ |
| 5972 | if (*current_ext >= (total_extents - 1)) |
| 5973 | goto update_current_ext; |
| 5974 | /* |
| 5975 | * If this is not the last extent in the file, make sure there |
| 5976 | * is enough room between current extent and next extent for |
| 5977 | * accommodating the shift. |
| 5978 | */ |
| 5979 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1); |
| 5980 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5981 | if (startoff + got.br_blockcount > adj_irec.br_startoff) |
| 5982 | return -EINVAL; |
| 5983 | /* |
| 5984 | * Unlike a left shift (which involves a hole punch), |
| 5985 | * a right shift does not modify extent neighbors |
| 5986 | * in any way. We should never find mergeable extents |
| 5987 | * in this scenario. Check anyways and warn if we |
| 5988 | * encounter two extents that could be one. |
| 5989 | */ |
| 5990 | if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb)) |
| 5991 | WARN_ON_ONCE(1); |
| 5992 | } |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5993 | /* |
| 5994 | * Increment the extent index for the next iteration, update the start |
| 5995 | * offset of the in-core extent and update the btree if applicable. |
| 5996 | */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5997 | update_current_ext: |
| 5998 | if (direction == SHIFT_LEFT) |
| 5999 | (*current_ext)++; |
| 6000 | else |
| 6001 | (*current_ext)--; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6002 | xfs_bmbt_set_startoff(gotp, startoff); |
| 6003 | *logflags |= XFS_ILOG_CORE; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6004 | adj_irec = got; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6005 | if (!cur) { |
| 6006 | *logflags |= XFS_ILOG_DEXT; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6007 | goto update_rmap; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6008 | } |
| 6009 | |
| 6010 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 6011 | got.br_blockcount, &i); |
| 6012 | if (error) |
| 6013 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 6014 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6015 | |
| 6016 | got.br_startoff = startoff; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6017 | error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock, |
| 6018 | got.br_blockcount, got.br_state); |
| 6019 | if (error) |
| 6020 | return error; |
| 6021 | |
| 6022 | update_rmap: |
| 6023 | /* update reverse mapping */ |
| 6024 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec); |
| 6025 | if (error) |
| 6026 | return error; |
| 6027 | adj_irec.br_startoff = startoff; |
| 6028 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6029 | } |
| 6030 | |
| 6031 | /* |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6032 | * Shift extent records to the left/right to cover/create a hole. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6033 | * |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6034 | * 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] | 6035 | * @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] | 6036 | * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb |
| 6037 | * is the length by which each extent is shifted. If there is no hole to shift |
| 6038 | * the extents into, this will be considered invalid operation and we abort |
| 6039 | * immediately. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6040 | */ |
| 6041 | int |
| 6042 | xfs_bmap_shift_extents( |
| 6043 | struct xfs_trans *tp, |
| 6044 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6045 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6046 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6047 | int *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6048 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6049 | xfs_fsblock_t *firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6050 | struct xfs_defer_ops *dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6051 | enum shift_direction direction, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6052 | int num_exts) |
| 6053 | { |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6054 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6055 | struct xfs_bmbt_rec_host *gotp; |
| 6056 | struct xfs_bmbt_irec got; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6057 | struct xfs_mount *mp = ip->i_mount; |
| 6058 | struct xfs_ifork *ifp; |
| 6059 | xfs_extnum_t nexts = 0; |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6060 | xfs_extnum_t current_ext; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6061 | xfs_extnum_t total_extents; |
| 6062 | xfs_extnum_t stop_extent; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6063 | int error = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6064 | int whichfork = XFS_DATA_FORK; |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6065 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6066 | |
| 6067 | if (unlikely(XFS_TEST_ERROR( |
| 6068 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 6069 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 6070 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 6071 | XFS_ERROR_REPORT("xfs_bmap_shift_extents", |
| 6072 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 6073 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6074 | } |
| 6075 | |
| 6076 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 6077 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6078 | |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6079 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 6080 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6081 | ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT); |
| 6082 | ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6083 | |
| 6084 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6085 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6086 | /* Read in all the extents */ |
| 6087 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6088 | if (error) |
| 6089 | return error; |
| 6090 | } |
| 6091 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6092 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6093 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 6094 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6095 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6096 | cur->bc_private.b.flags = 0; |
| 6097 | } |
| 6098 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6099 | /* |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 6100 | * 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] | 6101 | * are collapsing out, so we cannot use the count of real extents here. |
| 6102 | * Instead we have to calculate it from the incore fork. |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 6103 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 6104 | total_extents = xfs_iext_count(ifp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6105 | if (total_extents == 0) { |
| 6106 | *done = 1; |
| 6107 | goto del_cursor; |
| 6108 | } |
| 6109 | |
| 6110 | /* |
| 6111 | * In case of first right shift, we need to initialize next_fsb |
| 6112 | */ |
| 6113 | if (*next_fsb == NULLFSBLOCK) { |
| 6114 | gotp = xfs_iext_get_ext(ifp, total_extents - 1); |
| 6115 | xfs_bmbt_get_all(gotp, &got); |
| 6116 | *next_fsb = got.br_startoff; |
| 6117 | if (stop_fsb > *next_fsb) { |
| 6118 | *done = 1; |
| 6119 | goto del_cursor; |
| 6120 | } |
| 6121 | } |
| 6122 | |
| 6123 | /* Lookup the extent index at which we have to stop */ |
| 6124 | if (direction == SHIFT_RIGHT) { |
| 6125 | gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent); |
| 6126 | /* Make stop_extent exclusive of shift range */ |
| 6127 | stop_extent--; |
| 6128 | } else |
| 6129 | stop_extent = total_extents; |
| 6130 | |
| 6131 | /* |
| 6132 | * Look up the extent index for the fsb where we start shifting. We can |
| 6133 | * henceforth iterate with current_ext as extent list changes are locked |
| 6134 | * out via ilock. |
| 6135 | * |
| 6136 | * gotp can be null in 2 cases: 1) if there are no extents or 2) |
| 6137 | * *next_fsb lies in a hole beyond which there are no extents. Either |
| 6138 | * way, we are done. |
| 6139 | */ |
| 6140 | gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext); |
| 6141 | if (!gotp) { |
| 6142 | *done = 1; |
| 6143 | goto del_cursor; |
| 6144 | } |
| 6145 | |
| 6146 | /* some sanity checking before we finally start shifting extents */ |
| 6147 | if ((direction == SHIFT_LEFT && current_ext >= stop_extent) || |
| 6148 | (direction == SHIFT_RIGHT && current_ext <= stop_extent)) { |
| 6149 | error = -EIO; |
| 6150 | goto del_cursor; |
| 6151 | } |
| 6152 | |
| 6153 | while (nexts++ < num_exts) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6154 | error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6155 | ¤t_ext, gotp, cur, &logflags, |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6156 | direction, dfops); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6157 | if (error) |
| 6158 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6159 | /* |
| 6160 | * If there was an extent merge during the shift, the extent |
| 6161 | * count can change. Update the total and grade the next record. |
| 6162 | */ |
| 6163 | if (direction == SHIFT_LEFT) { |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 6164 | total_extents = xfs_iext_count(ifp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6165 | stop_extent = total_extents; |
| 6166 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6167 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6168 | if (current_ext == stop_extent) { |
| 6169 | *done = 1; |
| 6170 | *next_fsb = NULLFSBLOCK; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6171 | break; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6172 | } |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6173 | gotp = xfs_iext_get_ext(ifp, current_ext); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6174 | } |
| 6175 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6176 | if (!*done) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6177 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6178 | *next_fsb = got.br_startoff; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6179 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6180 | |
| 6181 | del_cursor: |
| 6182 | if (cur) |
| 6183 | xfs_btree_del_cursor(cur, |
| 6184 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6185 | |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6186 | if (logflags) |
| 6187 | xfs_trans_log_inode(tp, ip, logflags); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6188 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6189 | return error; |
| 6190 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6191 | |
| 6192 | /* |
| 6193 | * Splits an extent into two extents at split_fsb block such that it is |
| 6194 | * the first block of the current_ext. @current_ext is a target extent |
| 6195 | * to be split. @split_fsb is a block where the extents is split. |
| 6196 | * If split_fsb lies in a hole or the first block of extents, just return 0. |
| 6197 | */ |
| 6198 | STATIC int |
| 6199 | xfs_bmap_split_extent_at( |
| 6200 | struct xfs_trans *tp, |
| 6201 | struct xfs_inode *ip, |
| 6202 | xfs_fileoff_t split_fsb, |
| 6203 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6204 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6205 | { |
| 6206 | int whichfork = XFS_DATA_FORK; |
| 6207 | struct xfs_btree_cur *cur = NULL; |
| 6208 | struct xfs_bmbt_rec_host *gotp; |
| 6209 | struct xfs_bmbt_irec got; |
| 6210 | struct xfs_bmbt_irec new; /* split extent */ |
| 6211 | struct xfs_mount *mp = ip->i_mount; |
| 6212 | struct xfs_ifork *ifp; |
| 6213 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
| 6214 | xfs_extnum_t current_ext; |
| 6215 | int error = 0; |
| 6216 | int logflags = 0; |
| 6217 | int i = 0; |
| 6218 | |
| 6219 | if (unlikely(XFS_TEST_ERROR( |
| 6220 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 6221 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 6222 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 6223 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 6224 | XFS_ERRLEVEL_LOW, mp); |
| 6225 | return -EFSCORRUPTED; |
| 6226 | } |
| 6227 | |
| 6228 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 6229 | return -EIO; |
| 6230 | |
| 6231 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6232 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6233 | /* Read in all the extents */ |
| 6234 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6235 | if (error) |
| 6236 | return error; |
| 6237 | } |
| 6238 | |
| 6239 | /* |
| 6240 | * gotp can be null in 2 cases: 1) if there are no extents |
| 6241 | * or 2) split_fsb lies in a hole beyond which there are |
| 6242 | * no extents. Either way, we are done. |
| 6243 | */ |
| 6244 | gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext); |
| 6245 | if (!gotp) |
| 6246 | return 0; |
| 6247 | |
| 6248 | xfs_bmbt_get_all(gotp, &got); |
| 6249 | |
| 6250 | /* |
| 6251 | * Check split_fsb lies in a hole or the start boundary offset |
| 6252 | * of the extent. |
| 6253 | */ |
| 6254 | if (got.br_startoff >= split_fsb) |
| 6255 | return 0; |
| 6256 | |
| 6257 | gotblkcnt = split_fsb - got.br_startoff; |
| 6258 | new.br_startoff = split_fsb; |
| 6259 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 6260 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 6261 | new.br_state = got.br_state; |
| 6262 | |
| 6263 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6264 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 6265 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6266 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6267 | cur->bc_private.b.flags = 0; |
| 6268 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 6269 | got.br_startblock, |
| 6270 | got.br_blockcount, |
| 6271 | &i); |
| 6272 | if (error) |
| 6273 | goto del_cursor; |
| 6274 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6275 | } |
| 6276 | |
| 6277 | xfs_bmbt_set_blockcount(gotp, gotblkcnt); |
| 6278 | got.br_blockcount = gotblkcnt; |
| 6279 | |
| 6280 | logflags = XFS_ILOG_CORE; |
| 6281 | if (cur) { |
| 6282 | error = xfs_bmbt_update(cur, got.br_startoff, |
| 6283 | got.br_startblock, |
| 6284 | got.br_blockcount, |
| 6285 | got.br_state); |
| 6286 | if (error) |
| 6287 | goto del_cursor; |
| 6288 | } else |
| 6289 | logflags |= XFS_ILOG_DEXT; |
| 6290 | |
| 6291 | /* Add new extent */ |
| 6292 | current_ext++; |
| 6293 | xfs_iext_insert(ip, current_ext, 1, &new, 0); |
| 6294 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 6295 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 6296 | |
| 6297 | if (cur) { |
| 6298 | error = xfs_bmbt_lookup_eq(cur, new.br_startoff, |
| 6299 | new.br_startblock, new.br_blockcount, |
| 6300 | &i); |
| 6301 | if (error) |
| 6302 | goto del_cursor; |
| 6303 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
| 6304 | cur->bc_rec.b.br_state = new.br_state; |
| 6305 | |
| 6306 | error = xfs_btree_insert(cur, &i); |
| 6307 | if (error) |
| 6308 | goto del_cursor; |
| 6309 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6310 | } |
| 6311 | |
| 6312 | /* |
| 6313 | * Convert to a btree if necessary. |
| 6314 | */ |
| 6315 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 6316 | int tmp_logflags; /* partial log flag return val */ |
| 6317 | |
| 6318 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6319 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6320 | &cur, 0, &tmp_logflags, whichfork); |
| 6321 | logflags |= tmp_logflags; |
| 6322 | } |
| 6323 | |
| 6324 | del_cursor: |
| 6325 | if (cur) { |
| 6326 | cur->bc_private.b.allocated = 0; |
| 6327 | xfs_btree_del_cursor(cur, |
| 6328 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6329 | } |
| 6330 | |
| 6331 | if (logflags) |
| 6332 | xfs_trans_log_inode(tp, ip, logflags); |
| 6333 | return error; |
| 6334 | } |
| 6335 | |
| 6336 | int |
| 6337 | xfs_bmap_split_extent( |
| 6338 | struct xfs_inode *ip, |
| 6339 | xfs_fileoff_t split_fsb) |
| 6340 | { |
| 6341 | struct xfs_mount *mp = ip->i_mount; |
| 6342 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6343 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6344 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6345 | int error; |
| 6346 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6347 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6348 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6349 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6350 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6351 | |
| 6352 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6353 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6354 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6355 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6356 | |
| 6357 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6358 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6359 | if (error) |
| 6360 | goto out; |
| 6361 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6362 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6363 | if (error) |
| 6364 | goto out; |
| 6365 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6366 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6367 | |
| 6368 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6369 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6370 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6371 | return error; |
| 6372 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6373 | |
| 6374 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6375 | static bool |
| 6376 | xfs_bmap_is_update_needed( |
| 6377 | struct xfs_bmbt_irec *bmap) |
| 6378 | { |
| 6379 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6380 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6381 | } |
| 6382 | |
| 6383 | /* Record a bmap intent. */ |
| 6384 | static int |
| 6385 | __xfs_bmap_add( |
| 6386 | struct xfs_mount *mp, |
| 6387 | struct xfs_defer_ops *dfops, |
| 6388 | enum xfs_bmap_intent_type type, |
| 6389 | struct xfs_inode *ip, |
| 6390 | int whichfork, |
| 6391 | struct xfs_bmbt_irec *bmap) |
| 6392 | { |
| 6393 | int error; |
| 6394 | struct xfs_bmap_intent *bi; |
| 6395 | |
| 6396 | trace_xfs_bmap_defer(mp, |
| 6397 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6398 | type, |
| 6399 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6400 | ip->i_ino, whichfork, |
| 6401 | bmap->br_startoff, |
| 6402 | bmap->br_blockcount, |
| 6403 | bmap->br_state); |
| 6404 | |
| 6405 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6406 | INIT_LIST_HEAD(&bi->bi_list); |
| 6407 | bi->bi_type = type; |
| 6408 | bi->bi_owner = ip; |
| 6409 | bi->bi_whichfork = whichfork; |
| 6410 | bi->bi_bmap = *bmap; |
| 6411 | |
| 6412 | error = xfs_defer_join(dfops, bi->bi_owner); |
| 6413 | if (error) { |
| 6414 | kmem_free(bi); |
| 6415 | return error; |
| 6416 | } |
| 6417 | |
| 6418 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6419 | return 0; |
| 6420 | } |
| 6421 | |
| 6422 | /* Map an extent into a file. */ |
| 6423 | int |
| 6424 | xfs_bmap_map_extent( |
| 6425 | struct xfs_mount *mp, |
| 6426 | struct xfs_defer_ops *dfops, |
| 6427 | struct xfs_inode *ip, |
| 6428 | struct xfs_bmbt_irec *PREV) |
| 6429 | { |
| 6430 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6431 | return 0; |
| 6432 | |
| 6433 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6434 | XFS_DATA_FORK, PREV); |
| 6435 | } |
| 6436 | |
| 6437 | /* Unmap an extent out of a file. */ |
| 6438 | int |
| 6439 | xfs_bmap_unmap_extent( |
| 6440 | struct xfs_mount *mp, |
| 6441 | struct xfs_defer_ops *dfops, |
| 6442 | struct xfs_inode *ip, |
| 6443 | struct xfs_bmbt_irec *PREV) |
| 6444 | { |
| 6445 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6446 | return 0; |
| 6447 | |
| 6448 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6449 | XFS_DATA_FORK, PREV); |
| 6450 | } |
| 6451 | |
| 6452 | /* |
| 6453 | * Process one of the deferred bmap operations. We pass back the |
| 6454 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6455 | */ |
| 6456 | int |
| 6457 | xfs_bmap_finish_one( |
| 6458 | struct xfs_trans *tp, |
| 6459 | struct xfs_defer_ops *dfops, |
| 6460 | struct xfs_inode *ip, |
| 6461 | enum xfs_bmap_intent_type type, |
| 6462 | int whichfork, |
| 6463 | xfs_fileoff_t startoff, |
| 6464 | xfs_fsblock_t startblock, |
| 6465 | xfs_filblks_t blockcount, |
| 6466 | xfs_exntst_t state) |
| 6467 | { |
| 6468 | struct xfs_bmbt_irec bmap; |
| 6469 | int nimaps = 1; |
| 6470 | xfs_fsblock_t firstfsb; |
| 6471 | int flags = XFS_BMAPI_REMAP; |
| 6472 | int done; |
| 6473 | int error = 0; |
| 6474 | |
| 6475 | bmap.br_startblock = startblock; |
| 6476 | bmap.br_startoff = startoff; |
| 6477 | bmap.br_blockcount = blockcount; |
| 6478 | bmap.br_state = state; |
| 6479 | |
| 6480 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6481 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6482 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
| 6483 | ip->i_ino, whichfork, startoff, blockcount, state); |
| 6484 | |
| 6485 | if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) |
| 6486 | return -EFSCORRUPTED; |
| 6487 | if (whichfork == XFS_ATTR_FORK) |
| 6488 | flags |= XFS_BMAPI_ATTRFORK; |
| 6489 | |
| 6490 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
| 6491 | XFS_ERRTAG_BMAP_FINISH_ONE, |
| 6492 | XFS_RANDOM_BMAP_FINISH_ONE)) |
| 6493 | return -EIO; |
| 6494 | |
| 6495 | switch (type) { |
| 6496 | case XFS_BMAP_MAP: |
| 6497 | firstfsb = bmap.br_startblock; |
| 6498 | error = xfs_bmapi_write(tp, ip, bmap.br_startoff, |
| 6499 | bmap.br_blockcount, flags, &firstfsb, |
| 6500 | bmap.br_blockcount, &bmap, &nimaps, |
| 6501 | dfops); |
| 6502 | break; |
| 6503 | case XFS_BMAP_UNMAP: |
| 6504 | error = xfs_bunmapi(tp, ip, bmap.br_startoff, |
| 6505 | bmap.br_blockcount, flags, 1, &firstfsb, |
| 6506 | dfops, &done); |
| 6507 | ASSERT(done); |
| 6508 | break; |
| 6509 | default: |
| 6510 | ASSERT(0); |
| 6511 | error = -EFSCORRUPTED; |
| 6512 | } |
| 6513 | |
| 6514 | return error; |
| 6515 | } |