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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 352 | */ |
Dave Chinner | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 761 | args.firstblock = *firstblock; |
| 762 | if (*firstblock == NULLFSBLOCK) { |
| 763 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 764 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 765 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 766 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 767 | args.fsbno = *firstblock; |
| 768 | } else { |
| 769 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 770 | args.fsbno = *firstblock; |
| 771 | } |
| 772 | args.minlen = args.maxlen = args.prod = 1; |
| 773 | args.wasdel = wasdel; |
| 774 | *logflagsp = 0; |
| 775 | if ((error = xfs_alloc_vextent(&args))) { |
| 776 | xfs_iroot_realloc(ip, -1, whichfork); |
| 777 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 778 | return error; |
| 779 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 780 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 781 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
| 782 | xfs_iroot_realloc(ip, -1, whichfork); |
| 783 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 784 | return -ENOSPC; |
| 785 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 786 | /* |
| 787 | * Allocation can't fail, the space was reserved. |
| 788 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 789 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 790 | args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 791 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 792 | cur->bc_private.b.allocated++; |
| 793 | ip->i_d.di_nblocks++; |
| 794 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 795 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 796 | /* |
| 797 | * Fill in the child block. |
| 798 | */ |
| 799 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 800 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 801 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 802 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 803 | XFS_BTREE_LONG_PTRS); |
| 804 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 805 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 806 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 807 | for (cnt = i = 0; i < nextents; i++) { |
| 808 | ep = xfs_iext_get_ext(ifp, i); |
| 809 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
| 810 | arp->l0 = cpu_to_be64(ep->l0); |
| 811 | arp->l1 = cpu_to_be64(ep->l1); |
| 812 | arp++; cnt++; |
| 813 | } |
| 814 | } |
| 815 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 816 | xfs_btree_set_numrecs(ablock, cnt); |
| 817 | |
| 818 | /* |
| 819 | * Fill in the root key and pointer. |
| 820 | */ |
| 821 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 822 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 823 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 824 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 825 | be16_to_cpu(block->bb_level))); |
| 826 | *pp = cpu_to_be64(args.fsbno); |
| 827 | |
| 828 | /* |
| 829 | * Do all this logging at the end so that |
| 830 | * the root is at the right level. |
| 831 | */ |
| 832 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 833 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 834 | ASSERT(*curp == NULL); |
| 835 | *curp = cur; |
| 836 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Convert a local file to an extents file. |
| 842 | * This code is out of bounds for data forks of regular files, |
| 843 | * since the file data needs to get logged so things will stay consistent. |
| 844 | * (The bmap-level manipulations are ok, though). |
| 845 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 846 | void |
| 847 | xfs_bmap_local_to_extents_empty( |
| 848 | struct xfs_inode *ip, |
| 849 | int whichfork) |
| 850 | { |
| 851 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 852 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 853 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 854 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 855 | ASSERT(ifp->if_bytes == 0); |
| 856 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 857 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 858 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 859 | ifp->if_flags &= ~XFS_IFINLINE; |
| 860 | ifp->if_flags |= XFS_IFEXTENTS; |
| 861 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 862 | } |
| 863 | |
| 864 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 865 | STATIC int /* error */ |
| 866 | xfs_bmap_local_to_extents( |
| 867 | xfs_trans_t *tp, /* transaction pointer */ |
| 868 | xfs_inode_t *ip, /* incore inode pointer */ |
| 869 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 870 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 871 | int *logflagsp, /* inode logging flags */ |
| 872 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 873 | void (*init_fn)(struct xfs_trans *tp, |
| 874 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 875 | struct xfs_inode *ip, |
| 876 | struct xfs_ifork *ifp)) |
| 877 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 878 | int error = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 879 | int flags; /* logging flags returned */ |
| 880 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 881 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 882 | xfs_buf_t *bp; /* buffer for extent block */ |
| 883 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 884 | |
| 885 | /* |
| 886 | * We don't want to deal with the case of keeping inode data inline yet. |
| 887 | * So sending the data fork of a regular inode is invalid. |
| 888 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 889 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 890 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 891 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 892 | |
| 893 | if (!ifp->if_bytes) { |
| 894 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 895 | flags = XFS_ILOG_CORE; |
| 896 | goto done; |
| 897 | } |
| 898 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 899 | flags = 0; |
| 900 | error = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 901 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
| 902 | XFS_IFINLINE); |
| 903 | memset(&args, 0, sizeof(args)); |
| 904 | args.tp = tp; |
| 905 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 906 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 907 | args.firstblock = *firstblock; |
| 908 | /* |
| 909 | * Allocate a block. We know we need only one, since the |
| 910 | * file currently fits in an inode. |
| 911 | */ |
| 912 | if (*firstblock == NULLFSBLOCK) { |
| 913 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 914 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 915 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 916 | args.fsbno = *firstblock; |
| 917 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 918 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 919 | args.total = total; |
| 920 | args.minlen = args.maxlen = args.prod = 1; |
| 921 | error = xfs_alloc_vextent(&args); |
| 922 | if (error) |
| 923 | goto done; |
| 924 | |
| 925 | /* Can't fail, the space was reserved. */ |
| 926 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 927 | ASSERT(args.len == 1); |
| 928 | *firstblock = args.fsbno; |
| 929 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 930 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 931 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 932 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 933 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 934 | * The callout is responsible for logging because the remote format |
| 935 | * might differ from the local format and thus we don't know how much to |
| 936 | * log here. Note that init_fn must also set the buffer log item type |
| 937 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 938 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 939 | init_fn(tp, bp, ip, ifp); |
| 940 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 941 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 942 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 943 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 944 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 945 | |
| 946 | xfs_iext_add(ifp, 0, 1); |
| 947 | ep = xfs_iext_get_ext(ifp, 0); |
| 948 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
| 949 | trace_xfs_bmap_post_update(ip, 0, |
| 950 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 951 | _THIS_IP_); |
| 952 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 953 | ip->i_d.di_nblocks = 1; |
| 954 | xfs_trans_mod_dquot_byino(tp, ip, |
| 955 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 956 | flags |= xfs_ilog_fext(whichfork); |
| 957 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 958 | done: |
| 959 | *logflagsp = flags; |
| 960 | return error; |
| 961 | } |
| 962 | |
| 963 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 965 | */ |
| 966 | STATIC int /* error */ |
| 967 | xfs_bmap_add_attrfork_btree( |
| 968 | xfs_trans_t *tp, /* transaction pointer */ |
| 969 | xfs_inode_t *ip, /* incore inode pointer */ |
| 970 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 971 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | int *flags) /* inode logging flags */ |
| 973 | { |
| 974 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 975 | int error; /* error return value */ |
| 976 | xfs_mount_t *mp; /* file system mount struct */ |
| 977 | int stat; /* newroot status */ |
| 978 | |
| 979 | mp = ip->i_mount; |
| 980 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 981 | *flags |= XFS_ILOG_DBROOT; |
| 982 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 983 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 984 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | cur->bc_private.b.firstblock = *firstblock; |
| 986 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 987 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 988 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 989 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 990 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | goto error0; |
| 992 | if (stat == 0) { |
| 993 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 994 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } |
| 996 | *firstblock = cur->bc_private.b.firstblock; |
| 997 | cur->bc_private.b.allocated = 0; |
| 998 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 999 | } |
| 1000 | return 0; |
| 1001 | error0: |
| 1002 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1003 | return error; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 1008 | */ |
| 1009 | STATIC int /* error */ |
| 1010 | xfs_bmap_add_attrfork_extents( |
| 1011 | xfs_trans_t *tp, /* transaction pointer */ |
| 1012 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1013 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1014 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | int *flags) /* inode logging flags */ |
| 1016 | { |
| 1017 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 1018 | int error; /* error return value */ |
| 1019 | |
| 1020 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 1021 | return 0; |
| 1022 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1023 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | flags, XFS_DATA_FORK); |
| 1025 | if (cur) { |
| 1026 | cur->bc_private.b.allocated = 0; |
| 1027 | xfs_btree_del_cursor(cur, |
| 1028 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 1029 | } |
| 1030 | return error; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1034 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 1035 | * different data fork content type needs a different callout to do the |
| 1036 | * conversion. Some are basic and only require special block initialisation |
| 1037 | * callouts for the data formating, others (directories) are so specialised they |
| 1038 | * handle everything themselves. |
| 1039 | * |
| 1040 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1041 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1042 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | */ |
| 1044 | STATIC int /* error */ |
| 1045 | xfs_bmap_add_attrfork_local( |
| 1046 | xfs_trans_t *tp, /* transaction pointer */ |
| 1047 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1048 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1049 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | int *flags) /* inode logging flags */ |
| 1051 | { |
| 1052 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1055 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1056 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1057 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1059 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | dargs.dp = ip; |
| 1061 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1062 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1063 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | dargs.whichfork = XFS_DATA_FORK; |
| 1065 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1066 | return xfs_dir2_sf_to_block(&dargs); |
| 1067 | } |
| 1068 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1069 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1070 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1071 | flags, XFS_DATA_FORK, |
| 1072 | xfs_symlink_local_to_remote); |
| 1073 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1074 | /* should only be called for types that support local format data */ |
| 1075 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1076 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1080 | * Convert inode from non-attributed to attributed. |
| 1081 | * Must not be in a transaction, ip must not be locked. |
| 1082 | */ |
| 1083 | int /* error code */ |
| 1084 | xfs_bmap_add_attrfork( |
| 1085 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1086 | int size, /* space new attribute needs */ |
| 1087 | int rsvd) /* xact may use reserved blks */ |
| 1088 | { |
| 1089 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1090 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1091 | xfs_mount_t *mp; /* mount structure */ |
| 1092 | xfs_trans_t *tp; /* transaction pointer */ |
| 1093 | int blks; /* space reservation */ |
| 1094 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1095 | int logflags; /* logging flags */ |
| 1096 | int error; /* error return value */ |
| 1097 | |
| 1098 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1099 | |
| 1100 | mp = ip->i_mount; |
| 1101 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1102 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1103 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1104 | |
| 1105 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1106 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1107 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1108 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1109 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1110 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1111 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1112 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1113 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1114 | if (error) |
| 1115 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1116 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1117 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1118 | if (ip->i_d.di_anextents != 0) { |
| 1119 | error = -EFSCORRUPTED; |
| 1120 | goto trans_cancel; |
| 1121 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1122 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1123 | /* |
| 1124 | * For inodes coming from pre-6.2 filesystems. |
| 1125 | */ |
| 1126 | ASSERT(ip->i_d.di_aformat == 0); |
| 1127 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1128 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1129 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1130 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1131 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1132 | |
| 1133 | switch (ip->i_d.di_format) { |
| 1134 | case XFS_DINODE_FMT_DEV: |
| 1135 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1136 | break; |
| 1137 | case XFS_DINODE_FMT_UUID: |
| 1138 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 1139 | break; |
| 1140 | case XFS_DINODE_FMT_LOCAL: |
| 1141 | case XFS_DINODE_FMT_EXTENTS: |
| 1142 | case XFS_DINODE_FMT_BTREE: |
| 1143 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1144 | if (!ip->i_d.di_forkoff) |
| 1145 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1146 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1147 | version = 2; |
| 1148 | break; |
| 1149 | default: |
| 1150 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1151 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1152 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | ASSERT(ip->i_afp == NULL); |
| 1156 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1157 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1158 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1159 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1160 | switch (ip->i_d.di_format) { |
| 1161 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1162 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1163 | &logflags); |
| 1164 | break; |
| 1165 | case XFS_DINODE_FMT_EXTENTS: |
| 1166 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1167 | &dfops, &logflags); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1168 | break; |
| 1169 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1170 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1171 | &logflags); |
| 1172 | break; |
| 1173 | default: |
| 1174 | error = 0; |
| 1175 | break; |
| 1176 | } |
| 1177 | if (logflags) |
| 1178 | xfs_trans_log_inode(tp, ip, logflags); |
| 1179 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1180 | goto bmap_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1181 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1182 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1183 | bool log_sb = false; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1184 | |
| 1185 | spin_lock(&mp->m_sb_lock); |
| 1186 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1187 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1188 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1189 | } |
| 1190 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1191 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1192 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1193 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1194 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1195 | if (log_sb) |
| 1196 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1197 | } |
| 1198 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1199 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1200 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1201 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1202 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1203 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1204 | return error; |
| 1205 | |
| 1206 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1207 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1208 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1209 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1210 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1211 | return error; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * Internal and external extent tree search functions. |
| 1216 | */ |
| 1217 | |
| 1218 | /* |
| 1219 | * Read in the extents to if_extents. |
| 1220 | * All inode fields are set up by caller, we just traverse the btree |
| 1221 | * and copy the records in. If the file system cannot contain unwritten |
| 1222 | * extents, the records are checked for no "state" flags. |
| 1223 | */ |
| 1224 | int /* error */ |
| 1225 | xfs_bmap_read_extents( |
| 1226 | xfs_trans_t *tp, /* transaction pointer */ |
| 1227 | xfs_inode_t *ip, /* incore inode */ |
| 1228 | int whichfork) /* data or attr fork */ |
| 1229 | { |
| 1230 | struct xfs_btree_block *block; /* current btree block */ |
| 1231 | xfs_fsblock_t bno; /* block # of "block" */ |
| 1232 | xfs_buf_t *bp; /* buffer for "block" */ |
| 1233 | int error; /* error return value */ |
| 1234 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
| 1235 | xfs_extnum_t i, j; /* index into the extents list */ |
| 1236 | xfs_ifork_t *ifp; /* fork structure */ |
| 1237 | int level; /* btree level, for checking */ |
| 1238 | xfs_mount_t *mp; /* file system mount structure */ |
| 1239 | __be64 *pp; /* pointer to block address */ |
| 1240 | /* REFERENCED */ |
| 1241 | xfs_extnum_t room; /* number of entries there's room for */ |
| 1242 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1243 | mp = ip->i_mount; |
| 1244 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1245 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 1246 | XFS_EXTFMT_INODE(ip); |
| 1247 | block = ifp->if_broot; |
| 1248 | /* |
| 1249 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1250 | */ |
| 1251 | level = be16_to_cpu(block->bb_level); |
| 1252 | ASSERT(level > 0); |
| 1253 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1254 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1255 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1256 | /* |
| 1257 | * Go down the tree until leaf level is reached, following the first |
| 1258 | * pointer (leftmost) at each level. |
| 1259 | */ |
| 1260 | while (level-- > 0) { |
| 1261 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1262 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1263 | if (error) |
| 1264 | return error; |
| 1265 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1266 | if (level == 0) |
| 1267 | break; |
| 1268 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1269 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1270 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 1271 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1272 | xfs_trans_brelse(tp, bp); |
| 1273 | } |
| 1274 | /* |
| 1275 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1276 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1277 | room = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1278 | i = 0; |
| 1279 | /* |
| 1280 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1281 | */ |
| 1282 | for (;;) { |
| 1283 | xfs_bmbt_rec_t *frp; |
| 1284 | xfs_fsblock_t nextbno; |
| 1285 | xfs_extnum_t num_recs; |
| 1286 | xfs_extnum_t start; |
| 1287 | |
| 1288 | num_recs = xfs_btree_get_numrecs(block); |
| 1289 | if (unlikely(i + num_recs > room)) { |
| 1290 | ASSERT(i + num_recs <= room); |
| 1291 | xfs_warn(ip->i_mount, |
| 1292 | "corrupt dinode %Lu, (btree extents).", |
| 1293 | (unsigned long long) ip->i_ino); |
| 1294 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 1295 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
| 1296 | goto error0; |
| 1297 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1298 | /* |
| 1299 | * Read-ahead the next leaf block, if any. |
| 1300 | */ |
| 1301 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1302 | if (nextbno != NULLFSBLOCK) |
| 1303 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1304 | &xfs_bmbt_buf_ops); |
| 1305 | /* |
| 1306 | * Copy records into the extent records. |
| 1307 | */ |
| 1308 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 1309 | start = i; |
| 1310 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 1311 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
| 1312 | trp->l0 = be64_to_cpu(frp->l0); |
| 1313 | trp->l1 = be64_to_cpu(frp->l1); |
| 1314 | } |
| 1315 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 1316 | /* |
| 1317 | * Check all attribute bmap btree records and |
| 1318 | * any "older" data bmap btree records for a |
| 1319 | * set bit in the "extent flag" position. |
| 1320 | */ |
| 1321 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 1322 | start, num_recs))) { |
| 1323 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 1324 | XFS_ERRLEVEL_LOW, |
| 1325 | ip->i_mount); |
| 1326 | goto error0; |
| 1327 | } |
| 1328 | } |
| 1329 | xfs_trans_brelse(tp, bp); |
| 1330 | bno = nextbno; |
| 1331 | /* |
| 1332 | * If we've reached the end, stop. |
| 1333 | */ |
| 1334 | if (bno == NULLFSBLOCK) |
| 1335 | break; |
| 1336 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1337 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1338 | if (error) |
| 1339 | return error; |
| 1340 | block = XFS_BUF_TO_BLOCK(bp); |
| 1341 | } |
Darrick J. Wong | 356a322 | 2016-12-05 12:36:56 +1100 | [diff] [blame] | 1342 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) |
| 1343 | return -EFSCORRUPTED; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1344 | ASSERT(i == xfs_iext_count(ifp)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1345 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
| 1346 | return 0; |
| 1347 | error0: |
| 1348 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1349 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1350 | } |
| 1351 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1352 | /* |
| 1353 | * Returns the file-relative block number of the first unused block(s) |
| 1354 | * in the file with at least "len" logically contiguous blocks free. |
| 1355 | * This is the lowest-address hole if the file has holes, else the first block |
| 1356 | * past the end of file. |
| 1357 | * Return 0 if the file is currently local (in-inode). |
| 1358 | */ |
| 1359 | int /* error */ |
| 1360 | xfs_bmap_first_unused( |
| 1361 | xfs_trans_t *tp, /* transaction pointer */ |
| 1362 | xfs_inode_t *ip, /* incore inode */ |
| 1363 | xfs_extlen_t len, /* size of hole to find */ |
| 1364 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1365 | int whichfork) /* data or attr fork */ |
| 1366 | { |
| 1367 | int error; /* error return value */ |
| 1368 | int idx; /* extent record index */ |
| 1369 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1370 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 1371 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 1372 | xfs_fileoff_t max; /* starting useful block */ |
| 1373 | xfs_fileoff_t off; /* offset for this block */ |
| 1374 | xfs_extnum_t nextents; /* number of extent entries */ |
| 1375 | |
| 1376 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1377 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1378 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 1379 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1380 | *first_unused = 0; |
| 1381 | return 0; |
| 1382 | } |
| 1383 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1384 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1385 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1386 | return error; |
| 1387 | lowest = *first_unused; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1388 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1389 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
| 1390 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
| 1391 | off = xfs_bmbt_get_startoff(ep); |
| 1392 | /* |
| 1393 | * See if the hole before this extent will work. |
| 1394 | */ |
| 1395 | if (off >= lowest + len && off - max >= len) { |
| 1396 | *first_unused = max; |
| 1397 | return 0; |
| 1398 | } |
| 1399 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 1400 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1401 | } |
| 1402 | *first_unused = max; |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1407 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1408 | * last_block (input value) in the file. |
| 1409 | * This is not based on i_size, it is based on the extent records. |
| 1410 | * Returns 0 for local files, as they do not have extent records. |
| 1411 | */ |
| 1412 | int /* error */ |
| 1413 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1414 | struct xfs_trans *tp, /* transaction pointer */ |
| 1415 | struct xfs_inode *ip, /* incore inode */ |
| 1416 | xfs_fileoff_t *last_block, /* last block */ |
| 1417 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1418 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1419 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1420 | struct xfs_bmbt_irec got; |
| 1421 | xfs_extnum_t idx; |
| 1422 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1423 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1424 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1425 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1426 | *last_block = 0; |
| 1427 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1428 | case XFS_DINODE_FMT_BTREE: |
| 1429 | case XFS_DINODE_FMT_EXTENTS: |
| 1430 | break; |
| 1431 | default: |
| 1432 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1433 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1434 | |
| 1435 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1436 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1437 | if (error) |
| 1438 | return error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1439 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1440 | |
| 1441 | if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) { |
| 1442 | if (got.br_startoff <= *last_block - 1) |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
| 1446 | if (xfs_iext_get_extent(ifp, idx - 1, &got)) { |
| 1447 | *last_block = got.br_startoff + got.br_blockcount; |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | *last_block = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1452 | return 0; |
| 1453 | } |
| 1454 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1455 | int |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1456 | xfs_bmap_last_extent( |
| 1457 | struct xfs_trans *tp, |
| 1458 | struct xfs_inode *ip, |
| 1459 | int whichfork, |
| 1460 | struct xfs_bmbt_irec *rec, |
| 1461 | int *is_empty) |
| 1462 | { |
| 1463 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1464 | int error; |
| 1465 | int nextents; |
| 1466 | |
| 1467 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1468 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1469 | if (error) |
| 1470 | return error; |
| 1471 | } |
| 1472 | |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1473 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1474 | if (nextents == 0) { |
| 1475 | *is_empty = 1; |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 1480 | *is_empty = 0; |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
| 1484 | /* |
| 1485 | * Check the last inode extent to determine whether this allocation will result |
| 1486 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1487 | * blocks at the end of the file which do not start at the previous data block, |
| 1488 | * we will try to align the new blocks at stripe unit boundaries. |
| 1489 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1490 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1491 | * at, or past the EOF. |
| 1492 | */ |
| 1493 | STATIC int |
| 1494 | xfs_bmap_isaeof( |
| 1495 | struct xfs_bmalloca *bma, |
| 1496 | int whichfork) |
| 1497 | { |
| 1498 | struct xfs_bmbt_irec rec; |
| 1499 | int is_empty; |
| 1500 | int error; |
| 1501 | |
| 1502 | bma->aeof = 0; |
| 1503 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1504 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1505 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1506 | return error; |
| 1507 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1508 | if (is_empty) { |
| 1509 | bma->aeof = 1; |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1513 | /* |
| 1514 | * Check if we are allocation or past the last extent, or at least into |
| 1515 | * the last delayed allocated extent. |
| 1516 | */ |
| 1517 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1518 | (bma->offset >= rec.br_startoff && |
| 1519 | isnullstartblock(rec.br_startblock)); |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1524 | * Returns the file-relative block number of the first block past eof in |
| 1525 | * the file. This is not based on i_size, it is based on the extent records. |
| 1526 | * Returns 0 for local files, as they do not have extent records. |
| 1527 | */ |
| 1528 | int |
| 1529 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1530 | struct xfs_inode *ip, |
| 1531 | xfs_fileoff_t *last_block, |
| 1532 | int whichfork) |
| 1533 | { |
| 1534 | struct xfs_bmbt_irec rec; |
| 1535 | int is_empty; |
| 1536 | int error; |
| 1537 | |
| 1538 | *last_block = 0; |
| 1539 | |
| 1540 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1541 | return 0; |
| 1542 | |
| 1543 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1544 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1545 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1546 | |
| 1547 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1548 | if (error || is_empty) |
| 1549 | return error; |
| 1550 | |
| 1551 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
| 1555 | /* |
| 1556 | * Returns whether the selected fork of the inode has exactly one |
| 1557 | * block or not. For the data fork we check this matches di_size, |
| 1558 | * implying the file's range is 0..bsize-1. |
| 1559 | */ |
| 1560 | int /* 1=>1 block, 0=>otherwise */ |
| 1561 | xfs_bmap_one_block( |
| 1562 | xfs_inode_t *ip, /* incore inode */ |
| 1563 | int whichfork) /* data or attr fork */ |
| 1564 | { |
| 1565 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
| 1566 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1567 | int rval; /* return value */ |
| 1568 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 1569 | |
| 1570 | #ifndef DEBUG |
| 1571 | if (whichfork == XFS_DATA_FORK) |
| 1572 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1573 | #endif /* !DEBUG */ |
| 1574 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1575 | return 0; |
| 1576 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1577 | return 0; |
| 1578 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1579 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 1580 | ep = xfs_iext_get_ext(ifp, 0); |
| 1581 | xfs_bmbt_get_all(ep, &s); |
| 1582 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1583 | if (rval && whichfork == XFS_DATA_FORK) |
| 1584 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1585 | return rval; |
| 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * Extent tree manipulation functions used during allocation. |
| 1590 | */ |
| 1591 | |
| 1592 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1593 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | */ |
| 1595 | STATIC int /* error */ |
| 1596 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1597 | struct xfs_bmalloca *bma, |
| 1598 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1600 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1602 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1605 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1607 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1608 | /* left is 0, right is 1, prev is 2 */ |
| 1609 | int rval=0; /* return value (logging flags) */ |
| 1610 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1611 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1612 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1613 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 1614 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1616 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1617 | xfs_extnum_t *nextents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1619 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1620 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1621 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1622 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1623 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1624 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1625 | ASSERT(bma->idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1626 | ASSERT(bma->idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1627 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1628 | ASSERT(!bma->cur || |
| 1629 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1630 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1631 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | #define LEFT r[0] |
| 1634 | #define RIGHT r[1] |
| 1635 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1637 | if (whichfork == XFS_COW_FORK) |
| 1638 | state |= BMAP_COWFORK; |
| 1639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | /* |
| 1641 | * Set up a bunch of variables to make the tests simpler. |
| 1642 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1643 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | xfs_bmbt_get_all(ep, &PREV); |
| 1645 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1646 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1647 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1648 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1649 | da_old = startblockval(PREV.br_startblock); |
| 1650 | da_new = 0; |
| 1651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | /* |
| 1653 | * Set flags determining what part of the previous delayed allocation |
| 1654 | * extent is being replaced by a real allocation. |
| 1655 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1656 | if (PREV.br_startoff == new->br_startoff) |
| 1657 | state |= BMAP_LEFT_FILLING; |
| 1658 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1659 | state |= BMAP_RIGHT_FILLING; |
| 1660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | /* |
| 1662 | * Check and set flags if this segment has a left neighbor. |
| 1663 | * Don't set contiguous if the combined extent would be too large. |
| 1664 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1665 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1666 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1667 | 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] | 1668 | |
| 1669 | if (isnullstartblock(LEFT.br_startblock)) |
| 1670 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1672 | |
| 1673 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1674 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1675 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1676 | LEFT.br_state == new->br_state && |
| 1677 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1678 | state |= BMAP_LEFT_CONTIG; |
| 1679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | /* |
| 1681 | * Check and set flags if this segment has a right neighbor. |
| 1682 | * Don't set contiguous if the combined extent would be too large. |
| 1683 | * Also check for all-three-contiguous being too large. |
| 1684 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1685 | if (bma->idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1686 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1687 | 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] | 1688 | |
| 1689 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1690 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1692 | |
| 1693 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1694 | new_endoff == RIGHT.br_startoff && |
| 1695 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1696 | new->br_state == RIGHT.br_state && |
| 1697 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1698 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1699 | BMAP_RIGHT_FILLING)) != |
| 1700 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1701 | BMAP_RIGHT_FILLING) || |
| 1702 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1703 | <= MAXEXTLEN)) |
| 1704 | state |= BMAP_RIGHT_CONTIG; |
| 1705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | error = 0; |
| 1707 | /* |
| 1708 | * Switch out based on the FILLING and CONTIG state bits. |
| 1709 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1710 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1711 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1712 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1713 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | /* |
| 1715 | * Filling in all of a previously delayed allocation extent. |
| 1716 | * The left and right neighbors are both contiguous with new. |
| 1717 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1718 | bma->idx--; |
| 1719 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1720 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1722 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1723 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1724 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1725 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1726 | (*nextents)--; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1727 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1729 | else { |
| 1730 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1731 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1733 | RIGHT.br_blockcount, &i); |
| 1734 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1736 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1737 | error = xfs_btree_delete(bma->cur, &i); |
| 1738 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1740 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1741 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1742 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1744 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1745 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | LEFT.br_startblock, |
| 1747 | LEFT.br_blockcount + |
| 1748 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1749 | RIGHT.br_blockcount, LEFT.br_state); |
| 1750 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | goto done; |
| 1752 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | break; |
| 1754 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1755 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | /* |
| 1757 | * Filling in all of a previously delayed allocation extent. |
| 1758 | * The left neighbor is contiguous, the right is not. |
| 1759 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1760 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1761 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1762 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1763 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1764 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1765 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1766 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1767 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1768 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | rval = XFS_ILOG_DEXT; |
| 1770 | else { |
| 1771 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1772 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1774 | &i); |
| 1775 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1777 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1778 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | LEFT.br_startblock, |
| 1780 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1781 | PREV.br_blockcount, LEFT.br_state); |
| 1782 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | goto done; |
| 1784 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | break; |
| 1786 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1787 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | /* |
| 1789 | * Filling in all of a previously delayed allocation extent. |
| 1790 | * The right neighbor is contiguous, the left is not. |
| 1791 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1792 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 1794 | xfs_bmbt_set_blockcount(ep, |
| 1795 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1796 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1797 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1798 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1799 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | rval = XFS_ILOG_DEXT; |
| 1801 | else { |
| 1802 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1803 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1805 | RIGHT.br_blockcount, &i); |
| 1806 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1808 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1809 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | new->br_startblock, |
| 1811 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1812 | RIGHT.br_blockcount, PREV.br_state); |
| 1813 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | goto done; |
| 1815 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | break; |
| 1817 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1818 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | /* |
| 1820 | * Filling in all of a previously delayed allocation extent. |
| 1821 | * Neither the left nor right neighbors are contiguous with |
| 1822 | * the new one. |
| 1823 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1824 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 1826 | xfs_bmbt_set_state(ep, new->br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1827 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1828 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1829 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1830 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1832 | else { |
| 1833 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1834 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1836 | &i); |
| 1837 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1839 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1840 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1841 | error = xfs_btree_insert(bma->cur, &i); |
| 1842 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1844 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | break; |
| 1847 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1848 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | /* |
| 1850 | * Filling in the first part of a previous delayed allocation. |
| 1851 | * The left neighbor is contiguous. |
| 1852 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1853 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 1854 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | LEFT.br_blockcount + new->br_blockcount); |
| 1856 | xfs_bmbt_set_startoff(ep, |
| 1857 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1858 | 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] | 1859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1861 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1863 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | rval = XFS_ILOG_DEXT; |
| 1865 | else { |
| 1866 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1867 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1869 | &i); |
| 1870 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1872 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1873 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | LEFT.br_startblock, |
| 1875 | LEFT.br_blockcount + |
| 1876 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1877 | LEFT.br_state); |
| 1878 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | goto done; |
| 1880 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1881 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1882 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1883 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1884 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1885 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1886 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | break; |
| 1888 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1889 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | /* |
| 1891 | * Filling in the first part of a previous delayed allocation. |
| 1892 | * The left neighbor is not contiguous. |
| 1893 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1894 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1896 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1897 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1898 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1899 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1900 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1902 | else { |
| 1903 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1904 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1906 | &i); |
| 1907 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1909 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1910 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1911 | error = xfs_btree_insert(bma->cur, &i); |
| 1912 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1914 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1916 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1917 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1918 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1919 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1920 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | rval |= tmp_rval; |
| 1922 | if (error) |
| 1923 | goto done; |
| 1924 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1925 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1926 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1927 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 1928 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1929 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1930 | 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] | 1931 | break; |
| 1932 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1933 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | /* |
| 1935 | * Filling in the last part of a previous delayed allocation. |
| 1936 | * The right neighbor is contiguous with the new allocation. |
| 1937 | */ |
| 1938 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1939 | 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] | 1940 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1941 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1942 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | new->br_blockcount + RIGHT.br_blockcount, |
| 1944 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1945 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 1946 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | rval = XFS_ILOG_DEXT; |
| 1948 | else { |
| 1949 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1950 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1952 | RIGHT.br_blockcount, &i); |
| 1953 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1955 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1956 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | new->br_startblock, |
| 1958 | new->br_blockcount + |
| 1959 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1960 | RIGHT.br_state); |
| 1961 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | goto done; |
| 1963 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1964 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1965 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1966 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1967 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1968 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1969 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1970 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1971 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | break; |
| 1973 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1974 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | /* |
| 1976 | * Filling in the last part of a previous delayed allocation. |
| 1977 | * The right neighbor is not contiguous. |
| 1978 | */ |
| 1979 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1980 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1982 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1983 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1984 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1986 | else { |
| 1987 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1988 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1990 | &i); |
| 1991 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1993 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1994 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1995 | error = xfs_btree_insert(bma->cur, &i); |
| 1996 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1998 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2000 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2001 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2002 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2003 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2004 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | rval |= tmp_rval; |
| 2006 | if (error) |
| 2007 | goto done; |
| 2008 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2009 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2010 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2011 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 2012 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2013 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2014 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2015 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2016 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | break; |
| 2018 | |
| 2019 | case 0: |
| 2020 | /* |
| 2021 | * Filling in the middle part of a previous delayed allocation. |
| 2022 | * Contiguity is impossible here. |
| 2023 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2024 | * |
| 2025 | * We start with a delayed allocation: |
| 2026 | * |
| 2027 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 2028 | * PREV @ idx |
| 2029 | * |
| 2030 | * and we are allocating: |
| 2031 | * +rrrrrrrrrrrrrrrrr+ |
| 2032 | * new |
| 2033 | * |
| 2034 | * and we set it up for insertion as: |
| 2035 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 2036 | * new |
| 2037 | * PREV @ idx LEFT RIGHT |
| 2038 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | */ |
| 2040 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2042 | 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] | 2043 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 2044 | LEFT = *new; |
| 2045 | RIGHT.br_state = PREV.br_state; |
| 2046 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2047 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2048 | RIGHT.br_startoff = new_endoff; |
| 2049 | RIGHT.br_blockcount = temp2; |
| 2050 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2051 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2052 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2053 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2055 | else { |
| 2056 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2057 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2059 | &i); |
| 2060 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2062 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2063 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2064 | error = xfs_btree_insert(bma->cur, &i); |
| 2065 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2067 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2069 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2070 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2071 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2072 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2073 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | rval |= tmp_rval; |
| 2075 | if (error) |
| 2076 | goto done; |
| 2077 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2078 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 2079 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2080 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2081 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2082 | if (diff > 0) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2083 | error = xfs_mod_fdblocks(bma->ip->i_mount, |
| 2084 | -((int64_t)diff), false); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2085 | ASSERT(!error); |
| 2086 | if (error) |
| 2087 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2089 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2090 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2091 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2092 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2093 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 2094 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2095 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2096 | 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] | 2097 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2098 | bma->idx++; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2099 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | break; |
| 2101 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2102 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2103 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2104 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2105 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2106 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2107 | case BMAP_LEFT_CONTIG: |
| 2108 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | /* |
| 2110 | * These cases are all impossible. |
| 2111 | */ |
| 2112 | ASSERT(0); |
| 2113 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2114 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2115 | /* add reverse mapping */ |
| 2116 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 2117 | if (error) |
| 2118 | goto done; |
| 2119 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2120 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2121 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2122 | int tmp_logflags; /* partial log flag return val */ |
| 2123 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2124 | ASSERT(bma->cur == NULL); |
| 2125 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2126 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2127 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2128 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2129 | if (error) |
| 2130 | goto done; |
| 2131 | } |
| 2132 | |
| 2133 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2134 | if (da_old || da_new) { |
| 2135 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2136 | if (bma->cur) |
| 2137 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2138 | ASSERT(temp <= da_old); |
| 2139 | if (temp < da_old) |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2140 | xfs_mod_fdblocks(bma->ip->i_mount, |
| 2141 | (int64_t)(da_old - temp), false); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2145 | if (bma->cur) |
| 2146 | bma->cur->bc_private.b.allocated = 0; |
| 2147 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2148 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2150 | if (whichfork != XFS_COW_FORK) |
| 2151 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | return error; |
| 2153 | #undef LEFT |
| 2154 | #undef RIGHT |
| 2155 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2159 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | */ |
| 2161 | STATIC int /* error */ |
| 2162 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2163 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2165 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2166 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2168 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2169 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2170 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2171 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2174 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2177 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 2179 | xfs_exntst_t newext; /* new extent state */ |
| 2180 | xfs_exntst_t oldext; /* old extent state */ |
| 2181 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2182 | /* left is 0, right is 1, prev is 2 */ |
| 2183 | int rval=0; /* return value (logging flags) */ |
| 2184 | int state = 0;/* state bits, accessed thru macros */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2185 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2187 | *logflagsp = 0; |
| 2188 | |
| 2189 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2190 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2191 | if (whichfork == XFS_COW_FORK) |
| 2192 | state |= BMAP_COWFORK; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2193 | |
| 2194 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2195 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2196 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2197 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2198 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | #define LEFT r[0] |
| 2201 | #define RIGHT r[1] |
| 2202 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | /* |
| 2205 | * Set up a bunch of variables to make the tests simpler. |
| 2206 | */ |
| 2207 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2208 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | xfs_bmbt_get_all(ep, &PREV); |
| 2210 | newext = new->br_state; |
| 2211 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 2212 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 2213 | ASSERT(PREV.br_state == oldext); |
| 2214 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2215 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2216 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | /* |
| 2219 | * Set flags determining what part of the previous oldext allocation |
| 2220 | * extent is being replaced by a newext allocation. |
| 2221 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2222 | if (PREV.br_startoff == new->br_startoff) |
| 2223 | state |= BMAP_LEFT_FILLING; |
| 2224 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2225 | state |= BMAP_RIGHT_FILLING; |
| 2226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | /* |
| 2228 | * Check and set flags if this segment has a left neighbor. |
| 2229 | * Don't set contiguous if the combined extent would be too large. |
| 2230 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2231 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2232 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2233 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2234 | |
| 2235 | if (isnullstartblock(LEFT.br_startblock)) |
| 2236 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2238 | |
| 2239 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2240 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2241 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 2242 | LEFT.br_state == newext && |
| 2243 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2244 | state |= BMAP_LEFT_CONTIG; |
| 2245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | /* |
| 2247 | * Check and set flags if this segment has a right neighbor. |
| 2248 | * Don't set contiguous if the combined extent would be too large. |
| 2249 | * Also check for all-three-contiguous being too large. |
| 2250 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2251 | if (*idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2252 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2253 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2254 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2255 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2257 | |
| 2258 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2259 | new_endoff == RIGHT.br_startoff && |
| 2260 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 2261 | newext == RIGHT.br_state && |
| 2262 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2263 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2264 | BMAP_RIGHT_FILLING)) != |
| 2265 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2266 | BMAP_RIGHT_FILLING) || |
| 2267 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2268 | <= MAXEXTLEN)) |
| 2269 | state |= BMAP_RIGHT_CONTIG; |
| 2270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | /* |
| 2272 | * Switch out based on the FILLING and CONTIG state bits. |
| 2273 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2274 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2275 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2276 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2277 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | /* |
| 2279 | * Setting all of a previous oldext extent to newext. |
| 2280 | * The left and right neighbors are both contiguous with new. |
| 2281 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2282 | --*idx; |
| 2283 | |
| 2284 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2285 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2287 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2288 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2289 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2290 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2291 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2292 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | if (cur == NULL) |
| 2294 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2295 | else { |
| 2296 | rval = XFS_ILOG_CORE; |
| 2297 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2298 | RIGHT.br_startblock, |
| 2299 | RIGHT.br_blockcount, &i))) |
| 2300 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2301 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2302 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2304 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2305 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2307 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2308 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2310 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2311 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2313 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2315 | LEFT.br_startblock, |
| 2316 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2317 | RIGHT.br_blockcount, LEFT.br_state))) |
| 2318 | goto done; |
| 2319 | } |
| 2320 | break; |
| 2321 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2322 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | /* |
| 2324 | * Setting all of a previous oldext extent to newext. |
| 2325 | * The left neighbor is contiguous, the right is not. |
| 2326 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2327 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2328 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2329 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2330 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 2331 | LEFT.br_blockcount + PREV.br_blockcount); |
| 2332 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2333 | |
| 2334 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2335 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2336 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | if (cur == NULL) |
| 2338 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2339 | else { |
| 2340 | rval = XFS_ILOG_CORE; |
| 2341 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2342 | PREV.br_startblock, PREV.br_blockcount, |
| 2343 | &i))) |
| 2344 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2345 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2346 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2348 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2349 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2351 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2353 | LEFT.br_startblock, |
| 2354 | LEFT.br_blockcount + PREV.br_blockcount, |
| 2355 | LEFT.br_state))) |
| 2356 | goto done; |
| 2357 | } |
| 2358 | break; |
| 2359 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2360 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | /* |
| 2362 | * Setting all of a previous oldext extent to newext. |
| 2363 | * The right neighbor is contiguous, the left is not. |
| 2364 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2365 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | xfs_bmbt_set_blockcount(ep, |
| 2367 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 2368 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2369 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2370 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2371 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2372 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | if (cur == NULL) |
| 2374 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2375 | else { |
| 2376 | rval = XFS_ILOG_CORE; |
| 2377 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2378 | RIGHT.br_startblock, |
| 2379 | RIGHT.br_blockcount, &i))) |
| 2380 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2381 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2382 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2384 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2385 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2387 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2389 | new->br_startblock, |
| 2390 | new->br_blockcount + RIGHT.br_blockcount, |
| 2391 | newext))) |
| 2392 | goto done; |
| 2393 | } |
| 2394 | break; |
| 2395 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2396 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | /* |
| 2398 | * Setting all of a previous oldext extent to newext. |
| 2399 | * Neither the left nor right neighbors are contiguous with |
| 2400 | * the new one. |
| 2401 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2402 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2404 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | if (cur == NULL) |
| 2407 | rval = XFS_ILOG_DEXT; |
| 2408 | else { |
| 2409 | rval = 0; |
| 2410 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2411 | new->br_startblock, new->br_blockcount, |
| 2412 | &i))) |
| 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, new->br_blockcount, |
| 2417 | newext))) |
| 2418 | goto done; |
| 2419 | } |
| 2420 | break; |
| 2421 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2422 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | /* |
| 2424 | * Setting the first part of a previous oldext extent to newext. |
| 2425 | * The left neighbor is contiguous. |
| 2426 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2427 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 2428 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | LEFT.br_blockcount + new->br_blockcount); |
| 2430 | xfs_bmbt_set_startoff(ep, |
| 2431 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2432 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2433 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2434 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | xfs_bmbt_set_startblock(ep, |
| 2436 | new->br_startblock + new->br_blockcount); |
| 2437 | xfs_bmbt_set_blockcount(ep, |
| 2438 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2439 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2440 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2441 | --*idx; |
| 2442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | if (cur == NULL) |
| 2444 | rval = XFS_ILOG_DEXT; |
| 2445 | else { |
| 2446 | rval = 0; |
| 2447 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2448 | PREV.br_startblock, PREV.br_blockcount, |
| 2449 | &i))) |
| 2450 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2451 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | if ((error = xfs_bmbt_update(cur, |
| 2453 | PREV.br_startoff + new->br_blockcount, |
| 2454 | PREV.br_startblock + new->br_blockcount, |
| 2455 | PREV.br_blockcount - new->br_blockcount, |
| 2456 | oldext))) |
| 2457 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2458 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2460 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | LEFT.br_startblock, |
| 2462 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2463 | LEFT.br_state); |
| 2464 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | goto done; |
| 2466 | } |
| 2467 | break; |
| 2468 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2469 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | /* |
| 2471 | * Setting the first part of a previous oldext extent to newext. |
| 2472 | * The left neighbor is not contiguous. |
| 2473 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2474 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 2476 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 2477 | xfs_bmbt_set_blockcount(ep, |
| 2478 | PREV.br_blockcount - new->br_blockcount); |
| 2479 | xfs_bmbt_set_startblock(ep, |
| 2480 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2481 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2482 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2483 | xfs_iext_insert(ip, *idx, 1, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2484 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2485 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | if (cur == NULL) |
| 2487 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2488 | else { |
| 2489 | rval = XFS_ILOG_CORE; |
| 2490 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2491 | PREV.br_startblock, PREV.br_blockcount, |
| 2492 | &i))) |
| 2493 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2494 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | if ((error = xfs_bmbt_update(cur, |
| 2496 | PREV.br_startoff + new->br_blockcount, |
| 2497 | PREV.br_startblock + new->br_blockcount, |
| 2498 | PREV.br_blockcount - new->br_blockcount, |
| 2499 | oldext))) |
| 2500 | goto done; |
| 2501 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2502 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2504 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | } |
| 2506 | break; |
| 2507 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2508 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | /* |
| 2510 | * Setting the last part of a previous oldext extent to newext. |
| 2511 | * The right neighbor is contiguous with the new allocation. |
| 2512 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2513 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | xfs_bmbt_set_blockcount(ep, |
| 2515 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2516 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2517 | |
| 2518 | ++*idx; |
| 2519 | |
| 2520 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2521 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2522 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2524 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | if (cur == NULL) |
| 2527 | rval = XFS_ILOG_DEXT; |
| 2528 | else { |
| 2529 | rval = 0; |
| 2530 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2531 | PREV.br_startblock, |
| 2532 | PREV.br_blockcount, &i))) |
| 2533 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2534 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2536 | PREV.br_startblock, |
| 2537 | PREV.br_blockcount - new->br_blockcount, |
| 2538 | oldext))) |
| 2539 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2540 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | goto done; |
| 2542 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2543 | new->br_startblock, |
| 2544 | new->br_blockcount + RIGHT.br_blockcount, |
| 2545 | newext))) |
| 2546 | goto done; |
| 2547 | } |
| 2548 | break; |
| 2549 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2550 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | /* |
| 2552 | * Setting the last part of a previous oldext extent to newext. |
| 2553 | * The right neighbor is not contiguous. |
| 2554 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2555 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | xfs_bmbt_set_blockcount(ep, |
| 2557 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2558 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2559 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2560 | ++*idx; |
| 2561 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2562 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2563 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2564 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | if (cur == NULL) |
| 2566 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2567 | else { |
| 2568 | rval = XFS_ILOG_CORE; |
| 2569 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2570 | PREV.br_startblock, PREV.br_blockcount, |
| 2571 | &i))) |
| 2572 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2573 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2575 | PREV.br_startblock, |
| 2576 | PREV.br_blockcount - new->br_blockcount, |
| 2577 | oldext))) |
| 2578 | goto done; |
| 2579 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2580 | new->br_startblock, new->br_blockcount, |
| 2581 | &i))) |
| 2582 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2583 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2585 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2587 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | } |
| 2589 | break; |
| 2590 | |
| 2591 | case 0: |
| 2592 | /* |
| 2593 | * Setting the middle part of a previous oldext extent to |
| 2594 | * newext. Contiguity is impossible here. |
| 2595 | * One extent becomes three extents. |
| 2596 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2597 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | xfs_bmbt_set_blockcount(ep, |
| 2599 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2600 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | r[0] = *new; |
| 2603 | r[1].br_startoff = new_endoff; |
| 2604 | r[1].br_blockcount = |
| 2605 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 2606 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 2607 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2608 | |
| 2609 | ++*idx; |
| 2610 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 2611 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2612 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2613 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | if (cur == NULL) |
| 2615 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2616 | else { |
| 2617 | rval = XFS_ILOG_CORE; |
| 2618 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2619 | PREV.br_startblock, PREV.br_blockcount, |
| 2620 | &i))) |
| 2621 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2622 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | /* new right extent - oldext */ |
| 2624 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 2625 | r[1].br_startblock, r[1].br_blockcount, |
| 2626 | r[1].br_state))) |
| 2627 | goto done; |
| 2628 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 2630 | cur->bc_rec.b.br_blockcount = |
| 2631 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2632 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2634 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2635 | /* |
| 2636 | * Reset the cursor to the position of the new extent |
| 2637 | * we are about to insert as we can't trust it after |
| 2638 | * the previous insert. |
| 2639 | */ |
| 2640 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2641 | new->br_startblock, new->br_blockcount, |
| 2642 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2644 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2646 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2647 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
| 2651 | break; |
| 2652 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2653 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2654 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2655 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2656 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2657 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2658 | case BMAP_LEFT_CONTIG: |
| 2659 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | /* |
| 2661 | * These cases are all impossible. |
| 2662 | */ |
| 2663 | ASSERT(0); |
| 2664 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2665 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2666 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2667 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2668 | if (error) |
| 2669 | goto done; |
| 2670 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2671 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2672 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2673 | int tmp_logflags; /* partial log flag return val */ |
| 2674 | |
| 2675 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2676 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2677 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2678 | *logflagsp |= tmp_logflags; |
| 2679 | if (error) |
| 2680 | goto done; |
| 2681 | } |
| 2682 | |
| 2683 | /* clear out the allocated field, done with it now in any case. */ |
| 2684 | if (cur) { |
| 2685 | cur->bc_private.b.allocated = 0; |
| 2686 | *curp = cur; |
| 2687 | } |
| 2688 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2689 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2691 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | return error; |
| 2693 | #undef LEFT |
| 2694 | #undef RIGHT |
| 2695 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2699 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2701 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | xfs_bmap_add_extent_hole_delay( |
| 2703 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2704 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2705 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2706 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2708 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2710 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2711 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2712 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 2713 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2714 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2716 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | state = 0; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2718 | if (whichfork == XFS_COW_FORK) |
| 2719 | state |= BMAP_COWFORK; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2720 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | /* |
| 2723 | * Check and set flags if this segment has a left neighbor |
| 2724 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2725 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2726 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2727 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2728 | |
| 2729 | if (isnullstartblock(left.br_startblock)) |
| 2730 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | /* |
| 2734 | * Check and set flags if the current (right) segment exists. |
| 2735 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2736 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2737 | if (*idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2738 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2739 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2740 | |
| 2741 | if (isnullstartblock(right.br_startblock)) |
| 2742 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | /* |
| 2746 | * Set contiguity flags on the left and right neighbors. |
| 2747 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2748 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2749 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2750 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2751 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2752 | state |= BMAP_LEFT_CONTIG; |
| 2753 | |
| 2754 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2755 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2756 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2757 | (!(state & BMAP_LEFT_CONTIG) || |
| 2758 | (left.br_blockcount + new->br_blockcount + |
| 2759 | right.br_blockcount <= MAXEXTLEN))) |
| 2760 | state |= BMAP_RIGHT_CONTIG; |
| 2761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | /* |
| 2763 | * Switch out based on the contiguity flags. |
| 2764 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2765 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2766 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | /* |
| 2768 | * New allocation is contiguous with delayed allocations |
| 2769 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2770 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2772 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | temp = left.br_blockcount + new->br_blockcount + |
| 2774 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2775 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2776 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2777 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2778 | oldlen = startblockval(left.br_startblock) + |
| 2779 | startblockval(new->br_startblock) + |
| 2780 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2781 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2782 | oldlen); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2783 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2784 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2785 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2786 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2787 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | break; |
| 2789 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2790 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | /* |
| 2792 | * New allocation is contiguous with a delayed allocation |
| 2793 | * on the left. |
| 2794 | * Merge the new allocation with the left neighbor. |
| 2795 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2796 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2798 | |
| 2799 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2800 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2801 | oldlen = startblockval(left.br_startblock) + |
| 2802 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2803 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2804 | oldlen); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2805 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2806 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2807 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | break; |
| 2809 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2810 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | /* |
| 2812 | * New allocation is contiguous with a delayed allocation |
| 2813 | * on the right. |
| 2814 | * Merge the new allocation with the right neighbor. |
| 2815 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2816 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2818 | oldlen = startblockval(new->br_startblock) + |
| 2819 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2820 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2821 | oldlen); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2822 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 2823 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2824 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2825 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | break; |
| 2827 | |
| 2828 | case 0: |
| 2829 | /* |
| 2830 | * New allocation is not contiguous with another |
| 2831 | * delayed allocation. |
| 2832 | * Insert a new entry. |
| 2833 | */ |
| 2834 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2835 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | break; |
| 2837 | } |
| 2838 | if (oldlen != newlen) { |
| 2839 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2840 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2841 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | /* |
| 2843 | * Nothing to do for disk quota accounting here. |
| 2844 | */ |
| 2845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2849 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | */ |
| 2851 | STATIC int /* error */ |
| 2852 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2853 | struct xfs_trans *tp, |
| 2854 | struct xfs_inode *ip, |
| 2855 | int whichfork, |
| 2856 | xfs_extnum_t *idx, |
| 2857 | struct xfs_btree_cur **curp, |
| 2858 | struct xfs_bmbt_irec *new, |
| 2859 | xfs_fsblock_t *first, |
| 2860 | struct xfs_defer_ops *dfops, |
| 2861 | int *logflagsp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2863 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2864 | struct xfs_mount *mp = ip->i_mount; |
| 2865 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2869 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2870 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | int state; /* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2873 | ASSERT(*idx >= 0); |
| 2874 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2875 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2876 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2877 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2878 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2879 | |
| 2880 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2881 | if (whichfork == XFS_ATTR_FORK) |
| 2882 | state |= BMAP_ATTRFORK; |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 2883 | if (whichfork == XFS_COW_FORK) |
| 2884 | state |= BMAP_COWFORK; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | /* |
| 2887 | * Check and set flags if this segment has a left neighbor. |
| 2888 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2889 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2890 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2891 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2892 | if (isnullstartblock(left.br_startblock)) |
| 2893 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | /* |
| 2897 | * Check and set flags if this segment has a current value. |
| 2898 | * Not true if we're inserting into the "hole" at eof. |
| 2899 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2900 | if (*idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2901 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2902 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2903 | if (isnullstartblock(right.br_startblock)) |
| 2904 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | /* |
| 2908 | * We're inserting a real allocation between "left" and "right". |
| 2909 | * Set the contiguity flags. Don't let extents get too large. |
| 2910 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2911 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2912 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2913 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2914 | left.br_state == new->br_state && |
| 2915 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2916 | state |= BMAP_LEFT_CONTIG; |
| 2917 | |
| 2918 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2919 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2920 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2921 | new->br_state == right.br_state && |
| 2922 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2923 | (!(state & BMAP_LEFT_CONTIG) || |
| 2924 | left.br_blockcount + new->br_blockcount + |
| 2925 | right.br_blockcount <= MAXEXTLEN)) |
| 2926 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2927 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2928 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | /* |
| 2930 | * Select which case we're in here, and implement it. |
| 2931 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2932 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2933 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | /* |
| 2935 | * New allocation is contiguous with real allocations on the |
| 2936 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2937 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2939 | --*idx; |
| 2940 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2941 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | left.br_blockcount + new->br_blockcount + |
| 2943 | right.br_blockcount); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2944 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2945 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2946 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2947 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2948 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2949 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2950 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2951 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2952 | } else { |
| 2953 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2954 | error = xfs_bmbt_lookup_eq(cur, right.br_startoff, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2955 | right.br_startblock, right.br_blockcount, |
| 2956 | &i); |
| 2957 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2958 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2959 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2960 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2961 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2962 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2963 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2964 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2965 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2966 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2967 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2968 | error = xfs_bmbt_update(cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2969 | left.br_startblock, |
| 2970 | left.br_blockcount + |
| 2971 | new->br_blockcount + |
| 2972 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2973 | left.br_state); |
| 2974 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2975 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2977 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2979 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | /* |
| 2981 | * New allocation is contiguous with a real allocation |
| 2982 | * on the left. |
| 2983 | * Merge the new allocation with the left neighbor. |
| 2984 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2985 | --*idx; |
| 2986 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2987 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2989 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2990 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2991 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2992 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2993 | } else { |
| 2994 | rval = 0; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2995 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2996 | left.br_startblock, left.br_blockcount, |
| 2997 | &i); |
| 2998 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2999 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3000 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3001 | error = xfs_bmbt_update(cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3002 | left.br_startblock, |
| 3003 | left.br_blockcount + |
| 3004 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3005 | left.br_state); |
| 3006 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3007 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3009 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3011 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | /* |
| 3013 | * New allocation is contiguous with a real allocation |
| 3014 | * on the right. |
| 3015 | * Merge the new allocation with the right neighbor. |
| 3016 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3017 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 3018 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 3019 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | new->br_blockcount + right.br_blockcount, |
| 3021 | right.br_state); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3022 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3023 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3024 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3025 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3026 | } else { |
| 3027 | rval = 0; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3028 | error = xfs_bmbt_lookup_eq(cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3029 | right.br_startoff, |
| 3030 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3031 | right.br_blockcount, &i); |
| 3032 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3033 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3034 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3035 | error = xfs_bmbt_update(cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3036 | new->br_startblock, |
| 3037 | new->br_blockcount + |
| 3038 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3039 | right.br_state); |
| 3040 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3041 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3043 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | |
| 3045 | case 0: |
| 3046 | /* |
| 3047 | * New allocation is not contiguous with another |
| 3048 | * real allocation. |
| 3049 | * Insert a new entry. |
| 3050 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3051 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 3052 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3053 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 3054 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3055 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3056 | } else { |
| 3057 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3058 | error = xfs_bmbt_lookup_eq(cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3059 | new->br_startoff, |
| 3060 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3061 | new->br_blockcount, &i); |
| 3062 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3063 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3064 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3065 | cur->bc_rec.b.br_state = new->br_state; |
| 3066 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3067 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3068 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3069 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3071 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3072 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3073 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 3074 | /* add reverse mapping */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3075 | error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 3076 | if (error) |
| 3077 | goto done; |
| 3078 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3079 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3080 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3081 | int tmp_logflags; /* partial log flag return val */ |
| 3082 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3083 | ASSERT(cur == NULL); |
| 3084 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3085 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3086 | *logflagsp |= tmp_logflags; |
| 3087 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3088 | if (error) |
| 3089 | goto done; |
| 3090 | } |
| 3091 | |
| 3092 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3093 | if (cur) |
| 3094 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3095 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3096 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3097 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3098 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3099 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | } |
| 3101 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3102 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 3103 | * Functions used in the extent read, allocate and remove paths |
| 3104 | */ |
| 3105 | |
| 3106 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3107 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 3108 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3109 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3110 | xfs_bmap_extsize_align( |
| 3111 | xfs_mount_t *mp, |
| 3112 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 3113 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 3114 | xfs_extlen_t extsz, /* align to this extent size */ |
| 3115 | int rt, /* is this a realtime inode? */ |
| 3116 | int eof, /* is extent at end-of-file? */ |
| 3117 | int delay, /* creating delalloc extent? */ |
| 3118 | int convert, /* overwriting unwritten extent? */ |
| 3119 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 3120 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 3121 | { |
| 3122 | xfs_fileoff_t orig_off; /* original offset */ |
| 3123 | xfs_extlen_t orig_alen; /* original length */ |
| 3124 | xfs_fileoff_t orig_end; /* original off+len */ |
| 3125 | xfs_fileoff_t nexto; /* next file offset */ |
| 3126 | xfs_fileoff_t prevo; /* previous file offset */ |
| 3127 | xfs_fileoff_t align_off; /* temp for offset */ |
| 3128 | xfs_extlen_t align_alen; /* temp for length */ |
| 3129 | xfs_extlen_t temp; /* temp for calculations */ |
| 3130 | |
| 3131 | if (convert) |
| 3132 | return 0; |
| 3133 | |
| 3134 | orig_off = align_off = *offp; |
| 3135 | orig_alen = align_alen = *lenp; |
| 3136 | orig_end = orig_off + orig_alen; |
| 3137 | |
| 3138 | /* |
| 3139 | * If this request overlaps an existing extent, then don't |
| 3140 | * attempt to perform any additional alignment. |
| 3141 | */ |
| 3142 | if (!delay && !eof && |
| 3143 | (orig_off >= gotp->br_startoff) && |
| 3144 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 3145 | return 0; |
| 3146 | } |
| 3147 | |
| 3148 | /* |
| 3149 | * If the file offset is unaligned vs. the extent size |
| 3150 | * we need to align it. This will be possible unless |
| 3151 | * the file was previously written with a kernel that didn't |
| 3152 | * perform this alignment, or if a truncate shot us in the |
| 3153 | * foot. |
| 3154 | */ |
| 3155 | temp = do_mod(orig_off, extsz); |
| 3156 | if (temp) { |
| 3157 | align_alen += temp; |
| 3158 | align_off -= temp; |
| 3159 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3160 | |
| 3161 | /* Same adjustment for the end of the requested area. */ |
| 3162 | temp = (align_alen % extsz); |
| 3163 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3164 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3165 | |
| 3166 | /* |
| 3167 | * For large extent hint sizes, the aligned extent might be larger than |
| 3168 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 3169 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 3170 | * short allocation just fine, so it is safe to do this. We only want to |
| 3171 | * do it when we are forced to, though, because it means more allocation |
| 3172 | * operations are required. |
| 3173 | */ |
| 3174 | while (align_alen > MAXEXTLEN) |
| 3175 | align_alen -= extsz; |
| 3176 | ASSERT(align_alen <= MAXEXTLEN); |
| 3177 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3178 | /* |
| 3179 | * If the previous block overlaps with this proposed allocation |
| 3180 | * then move the start forward without adjusting the length. |
| 3181 | */ |
| 3182 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3183 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3184 | prevo = prevp->br_startoff; |
| 3185 | else |
| 3186 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3187 | } else |
| 3188 | prevo = 0; |
| 3189 | if (align_off != orig_off && align_off < prevo) |
| 3190 | align_off = prevo; |
| 3191 | /* |
| 3192 | * If the next block overlaps with this proposed allocation |
| 3193 | * then move the start back without adjusting the length, |
| 3194 | * but not before offset 0. |
| 3195 | * This may of course make the start overlap previous block, |
| 3196 | * and if we hit the offset 0 limit then the next block |
| 3197 | * can still overlap too. |
| 3198 | */ |
| 3199 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3200 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3201 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3202 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3203 | else |
| 3204 | nexto = gotp->br_startoff; |
| 3205 | } else |
| 3206 | nexto = NULLFILEOFF; |
| 3207 | if (!eof && |
| 3208 | align_off + align_alen != orig_end && |
| 3209 | align_off + align_alen > nexto) |
| 3210 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3211 | /* |
| 3212 | * If we're now overlapping the next or previous extent that |
| 3213 | * means we can't fit an extsz piece in this hole. Just move |
| 3214 | * the start forward to the first valid spot and set |
| 3215 | * the length so we hit the end. |
| 3216 | */ |
| 3217 | if (align_off != orig_off && align_off < prevo) |
| 3218 | align_off = prevo; |
| 3219 | if (align_off + align_alen != orig_end && |
| 3220 | align_off + align_alen > nexto && |
| 3221 | nexto != NULLFILEOFF) { |
| 3222 | ASSERT(nexto > prevo); |
| 3223 | align_alen = nexto - align_off; |
| 3224 | } |
| 3225 | |
| 3226 | /* |
| 3227 | * If realtime, and the result isn't a multiple of the realtime |
| 3228 | * extent size we need to remove blocks until it is. |
| 3229 | */ |
| 3230 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3231 | /* |
| 3232 | * We're not covering the original request, or |
| 3233 | * we won't be able to once we fix the length. |
| 3234 | */ |
| 3235 | if (orig_off < align_off || |
| 3236 | orig_end > align_off + align_alen || |
| 3237 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3238 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3239 | /* |
| 3240 | * Try to fix it by moving the start up. |
| 3241 | */ |
| 3242 | if (align_off + temp <= orig_off) { |
| 3243 | align_alen -= temp; |
| 3244 | align_off += temp; |
| 3245 | } |
| 3246 | /* |
| 3247 | * Try to fix it by moving the end in. |
| 3248 | */ |
| 3249 | else if (align_off + align_alen - temp >= orig_end) |
| 3250 | align_alen -= temp; |
| 3251 | /* |
| 3252 | * Set the start to the minimum then trim the length. |
| 3253 | */ |
| 3254 | else { |
| 3255 | align_alen -= orig_off - align_off; |
| 3256 | align_off = orig_off; |
| 3257 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3258 | } |
| 3259 | /* |
| 3260 | * Result doesn't cover the request, fail it. |
| 3261 | */ |
| 3262 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3263 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3264 | } else { |
| 3265 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3266 | /* see MAXEXTLEN handling above */ |
| 3267 | ASSERT(orig_end <= align_off + align_alen || |
| 3268 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | #ifdef DEBUG |
| 3272 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3273 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3274 | if (prevp->br_startoff != NULLFILEOFF) |
| 3275 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3276 | #endif |
| 3277 | |
| 3278 | *lenp = align_alen; |
| 3279 | *offp = align_off; |
| 3280 | return 0; |
| 3281 | } |
| 3282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3284 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3285 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3286 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3287 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | { |
| 3289 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3291 | xfs_mount_t *mp; /* mount point structure */ |
| 3292 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3293 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | |
| 3295 | #define ISVALID(x,y) \ |
| 3296 | (rt ? \ |
| 3297 | (x) < mp->m_sb.sb_rblocks : \ |
| 3298 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3299 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3300 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3303 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3304 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3305 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3306 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | /* |
| 3308 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3309 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3311 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3312 | !isnullstartblock(ap->prev.br_startblock) && |
| 3313 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3314 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3315 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | /* |
| 3317 | * Adjust for the gap between prevp and us. |
| 3318 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3319 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3320 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3322 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3323 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | } |
| 3325 | /* |
| 3326 | * If not at eof, then compare the two neighbor blocks. |
| 3327 | * Figure out whether either one gives us a good starting point, |
| 3328 | * and pick the better one. |
| 3329 | */ |
| 3330 | else if (!ap->eof) { |
| 3331 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3332 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3333 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3334 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3335 | |
| 3336 | /* |
| 3337 | * If there's a previous (left) block, select a requested |
| 3338 | * start block based on it. |
| 3339 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3340 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3341 | !isnullstartblock(ap->prev.br_startblock) && |
| 3342 | (prevbno = ap->prev.br_startblock + |
| 3343 | ap->prev.br_blockcount) && |
| 3344 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | /* |
| 3346 | * Calculate gap to end of previous block. |
| 3347 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3348 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3349 | (ap->prev.br_startoff + |
| 3350 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3351 | /* |
| 3352 | * Figure the startblock based on the previous block's |
| 3353 | * end and the gap size. |
| 3354 | * Heuristic! |
| 3355 | * If the gap is large relative to the piece we're |
| 3356 | * allocating, or using it gives us an invalid block |
| 3357 | * number, then just use the end of the previous block. |
| 3358 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3359 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3361 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | prevbno += adjust; |
| 3363 | else |
| 3364 | prevdiff += adjust; |
| 3365 | /* |
| 3366 | * If the firstblock forbids it, can't use it, |
| 3367 | * must use default. |
| 3368 | */ |
| 3369 | if (!rt && !nullfb && |
| 3370 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3371 | prevbno = NULLFSBLOCK; |
| 3372 | } |
| 3373 | /* |
| 3374 | * No previous block or can't follow it, just default. |
| 3375 | */ |
| 3376 | else |
| 3377 | prevbno = NULLFSBLOCK; |
| 3378 | /* |
| 3379 | * If there's a following (right) block, select a requested |
| 3380 | * start block based on it. |
| 3381 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3382 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | /* |
| 3384 | * Calculate gap to start of next block. |
| 3385 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3386 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3387 | /* |
| 3388 | * Figure the startblock based on the next block's |
| 3389 | * start and the gap size. |
| 3390 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3391 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | /* |
| 3393 | * Heuristic! |
| 3394 | * If the gap is large relative to the piece we're |
| 3395 | * allocating, or using it gives us an invalid block |
| 3396 | * number, then just use the start of the next block |
| 3397 | * offset by our length. |
| 3398 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3399 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3401 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3402 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3403 | gotbno -= ap->length; |
| 3404 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | } else |
| 3406 | gotdiff += adjust; |
| 3407 | /* |
| 3408 | * If the firstblock forbids it, can't use it, |
| 3409 | * must use default. |
| 3410 | */ |
| 3411 | if (!rt && !nullfb && |
| 3412 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3413 | gotbno = NULLFSBLOCK; |
| 3414 | } |
| 3415 | /* |
| 3416 | * No next block, just default. |
| 3417 | */ |
| 3418 | else |
| 3419 | gotbno = NULLFSBLOCK; |
| 3420 | /* |
| 3421 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3422 | * 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] | 3423 | */ |
| 3424 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3425 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3427 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3429 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3431 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3432 | } |
| 3433 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3434 | static int |
| 3435 | xfs_bmap_longest_free_extent( |
| 3436 | struct xfs_trans *tp, |
| 3437 | xfs_agnumber_t ag, |
| 3438 | xfs_extlen_t *blen, |
| 3439 | int *notinit) |
| 3440 | { |
| 3441 | struct xfs_mount *mp = tp->t_mountp; |
| 3442 | struct xfs_perag *pag; |
| 3443 | xfs_extlen_t longest; |
| 3444 | int error = 0; |
| 3445 | |
| 3446 | pag = xfs_perag_get(mp, ag); |
| 3447 | if (!pag->pagf_init) { |
| 3448 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3449 | if (error) |
| 3450 | goto out; |
| 3451 | |
| 3452 | if (!pag->pagf_init) { |
| 3453 | *notinit = 1; |
| 3454 | goto out; |
| 3455 | } |
| 3456 | } |
| 3457 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 3458 | longest = xfs_alloc_longest_free_extent(mp, pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3459 | xfs_alloc_min_freelist(mp, pag), |
| 3460 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3461 | if (*blen < longest) |
| 3462 | *blen = longest; |
| 3463 | |
| 3464 | out: |
| 3465 | xfs_perag_put(pag); |
| 3466 | return error; |
| 3467 | } |
| 3468 | |
| 3469 | static void |
| 3470 | xfs_bmap_select_minlen( |
| 3471 | struct xfs_bmalloca *ap, |
| 3472 | struct xfs_alloc_arg *args, |
| 3473 | xfs_extlen_t *blen, |
| 3474 | int notinit) |
| 3475 | { |
| 3476 | if (notinit || *blen < ap->minlen) { |
| 3477 | /* |
| 3478 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3479 | * there is space for this request. |
| 3480 | */ |
| 3481 | args->minlen = ap->minlen; |
| 3482 | } else if (*blen < args->maxlen) { |
| 3483 | /* |
| 3484 | * If the best seen length is less than the request length, |
| 3485 | * use the best as the minimum. |
| 3486 | */ |
| 3487 | args->minlen = *blen; |
| 3488 | } else { |
| 3489 | /* |
| 3490 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3491 | * as the minimum. |
| 3492 | */ |
| 3493 | args->minlen = args->maxlen; |
| 3494 | } |
| 3495 | } |
| 3496 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3497 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3498 | xfs_bmap_btalloc_nullfb( |
| 3499 | struct xfs_bmalloca *ap, |
| 3500 | struct xfs_alloc_arg *args, |
| 3501 | xfs_extlen_t *blen) |
| 3502 | { |
| 3503 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3504 | xfs_agnumber_t ag, startag; |
| 3505 | int notinit = 0; |
| 3506 | int error; |
| 3507 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3508 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3509 | args->total = ap->total; |
| 3510 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3511 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3512 | if (startag == NULLAGNUMBER) |
| 3513 | startag = ag = 0; |
| 3514 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3515 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3516 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3517 | ¬init); |
| 3518 | if (error) |
| 3519 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3520 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3521 | if (++ag == mp->m_sb.sb_agcount) |
| 3522 | ag = 0; |
| 3523 | if (ag == startag) |
| 3524 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3525 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3526 | |
| 3527 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3528 | return 0; |
| 3529 | } |
| 3530 | |
| 3531 | STATIC int |
| 3532 | xfs_bmap_btalloc_filestreams( |
| 3533 | struct xfs_bmalloca *ap, |
| 3534 | struct xfs_alloc_arg *args, |
| 3535 | xfs_extlen_t *blen) |
| 3536 | { |
| 3537 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3538 | xfs_agnumber_t ag; |
| 3539 | int notinit = 0; |
| 3540 | int error; |
| 3541 | |
| 3542 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3543 | args->total = ap->total; |
| 3544 | |
| 3545 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3546 | if (ag == NULLAGNUMBER) |
| 3547 | ag = 0; |
| 3548 | |
| 3549 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3550 | if (error) |
| 3551 | return error; |
| 3552 | |
| 3553 | if (*blen < args->maxlen) { |
| 3554 | error = xfs_filestream_new_ag(ap, &ag); |
| 3555 | if (error) |
| 3556 | return error; |
| 3557 | |
| 3558 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3559 | ¬init); |
| 3560 | if (error) |
| 3561 | return error; |
| 3562 | |
| 3563 | } |
| 3564 | |
| 3565 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3566 | |
| 3567 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3568 | * Set the failure fallback case to look in the selected AG as stream |
| 3569 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3570 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3571 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3572 | return 0; |
| 3573 | } |
| 3574 | |
| 3575 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3576 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3577 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3578 | { |
| 3579 | xfs_mount_t *mp; /* mount point structure */ |
| 3580 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3581 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3582 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3583 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3584 | xfs_alloc_arg_t args; |
| 3585 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3586 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3587 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3588 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3589 | int tryagain; |
| 3590 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3591 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3592 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3593 | ASSERT(ap->length); |
| 3594 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3595 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3596 | |
| 3597 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3598 | stripe_align = 0; |
| 3599 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3600 | stripe_align = mp->m_swidth; |
| 3601 | else if (mp->m_dalign) |
| 3602 | stripe_align = mp->m_dalign; |
| 3603 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3604 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3605 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3606 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3607 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3608 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3609 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3610 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3611 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3612 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3613 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3614 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3615 | |
| 3616 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3617 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3618 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3619 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3620 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3621 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3622 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3623 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3624 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3625 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3626 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3627 | } |
| 3628 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3629 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3630 | |
| 3631 | xfs_bmap_adjacent(ap); |
| 3632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3634 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | * it's in the right allocation group. |
| 3636 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3637 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3638 | ; |
| 3639 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3640 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3641 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | * Normal allocation, done through xfs_alloc_vextent. |
| 3643 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3644 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3645 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3646 | args.tp = ap->tp; |
| 3647 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3648 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3649 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3650 | |
| 3651 | /* Trim the allocation back to the maximum an AG can fit. */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 3652 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3653 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3654 | blen = 0; |
| 3655 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3656 | /* |
| 3657 | * Search for an allocation group with a single extent large |
| 3658 | * enough for the request. If one isn't found, then adjust |
| 3659 | * the minimum allocation size to the largest space found. |
| 3660 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3661 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3662 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3663 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3664 | else |
| 3665 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3666 | if (error) |
| 3667 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3668 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3669 | if (xfs_inode_is_filestream(ap->ip)) |
| 3670 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3671 | else |
| 3672 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3673 | args.total = args.minlen = ap->minlen; |
| 3674 | } else { |
| 3675 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3676 | args.total = ap->total; |
| 3677 | args.minlen = ap->minlen; |
| 3678 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3679 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3680 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3681 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3682 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3683 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3684 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3685 | args.prod = 1; |
| 3686 | args.mod = 0; |
| 3687 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3688 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3689 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3690 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3691 | } |
| 3692 | /* |
| 3693 | * If we are not low on available data blocks, and the |
| 3694 | * underlying logical volume manager is a stripe, and |
| 3695 | * the file offset is zero then try to allocate data |
| 3696 | * blocks on stripe unit boundary. |
| 3697 | * NOTE: ap->aeof is only set if the allocation length |
| 3698 | * is >= the stripe unit and the allocation offset is |
| 3699 | * at the end of file. |
| 3700 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3701 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3702 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3703 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3704 | atype = args.type; |
| 3705 | isaligned = 1; |
| 3706 | /* |
| 3707 | * Adjust for alignment |
| 3708 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3709 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3710 | args.minlen = blen - args.alignment; |
| 3711 | args.minalignslop = 0; |
| 3712 | } else { |
| 3713 | /* |
| 3714 | * First try an exact bno allocation. |
| 3715 | * If it fails then do a near or start bno |
| 3716 | * allocation with alignment turned on. |
| 3717 | */ |
| 3718 | atype = args.type; |
| 3719 | tryagain = 1; |
| 3720 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3721 | args.alignment = 1; |
| 3722 | /* |
| 3723 | * Compute the minlen+alignment for the |
| 3724 | * next case. Set slop so that the value |
| 3725 | * of minlen+alignment+slop doesn't go up |
| 3726 | * between the calls. |
| 3727 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3728 | if (blen > stripe_align && blen <= args.maxlen) |
| 3729 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3730 | else |
| 3731 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3732 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3733 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3734 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3735 | args.minlen - 1; |
| 3736 | else |
| 3737 | args.minalignslop = 0; |
| 3738 | } |
| 3739 | } else { |
| 3740 | args.alignment = 1; |
| 3741 | args.minalignslop = 0; |
| 3742 | } |
| 3743 | args.minleft = ap->minleft; |
| 3744 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3745 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3746 | args.datatype = ap->datatype; |
| 3747 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3748 | args.ip = ap->ip; |
| 3749 | |
| 3750 | error = xfs_alloc_vextent(&args); |
| 3751 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3752 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3753 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3754 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3755 | /* |
| 3756 | * Exact allocation failed. Now try with alignment |
| 3757 | * turned on. |
| 3758 | */ |
| 3759 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3760 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3761 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3762 | args.minlen = nextminlen; |
| 3763 | args.minalignslop = 0; |
| 3764 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | if ((error = xfs_alloc_vextent(&args))) |
| 3766 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3767 | } |
| 3768 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3769 | /* |
| 3770 | * allocation failed, so turn off alignment and |
| 3771 | * try again. |
| 3772 | */ |
| 3773 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3774 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3775 | args.alignment = 0; |
| 3776 | if ((error = xfs_alloc_vextent(&args))) |
| 3777 | return error; |
| 3778 | } |
| 3779 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3780 | args.minlen > ap->minlen) { |
| 3781 | args.minlen = ap->minlen; |
| 3782 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3783 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3784 | if ((error = xfs_alloc_vextent(&args))) |
| 3785 | return error; |
| 3786 | } |
| 3787 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3788 | args.fsbno = 0; |
| 3789 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3790 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3791 | if ((error = xfs_alloc_vextent(&args))) |
| 3792 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3793 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3794 | } |
| 3795 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3796 | /* |
| 3797 | * check the allocation happened at the same or higher AG than |
| 3798 | * the first block that was allocated. |
| 3799 | */ |
| 3800 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3801 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) <= |
| 3802 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3803 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3804 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3805 | if (*ap->firstblock == NULLFSBLOCK) |
| 3806 | *ap->firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3807 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3808 | ap->length = args.len; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 3809 | if (!(ap->flags & XFS_BMAPI_COWFORK)) |
| 3810 | ap->ip->i_d.di_nblocks += args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3811 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3812 | if (ap->wasdel) |
| 3813 | ap->ip->i_delayed_blks -= args.len; |
| 3814 | /* |
| 3815 | * Adjust the disk quota also. This was reserved |
| 3816 | * earlier. |
| 3817 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3818 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3819 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3820 | XFS_TRANS_DQ_BCOUNT, |
| 3821 | (long) args.len); |
| 3822 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3823 | ap->blkno = NULLFSBLOCK; |
| 3824 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | } |
| 3826 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | /* |
| 3830 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3831 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3832 | */ |
| 3833 | STATIC int |
| 3834 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3835 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3836 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3837 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3838 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3839 | return xfs_bmap_rtalloc(ap); |
| 3840 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3841 | } |
| 3842 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3843 | /* Trim extent to fit a logical block range. */ |
| 3844 | void |
| 3845 | xfs_trim_extent( |
| 3846 | struct xfs_bmbt_irec *irec, |
| 3847 | xfs_fileoff_t bno, |
| 3848 | xfs_filblks_t len) |
| 3849 | { |
| 3850 | xfs_fileoff_t distance; |
| 3851 | xfs_fileoff_t end = bno + len; |
| 3852 | |
| 3853 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3854 | irec->br_startoff >= end) { |
| 3855 | irec->br_blockcount = 0; |
| 3856 | return; |
| 3857 | } |
| 3858 | |
| 3859 | if (irec->br_startoff < bno) { |
| 3860 | distance = bno - irec->br_startoff; |
| 3861 | if (isnullstartblock(irec->br_startblock)) |
| 3862 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3863 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3864 | irec->br_startblock != HOLESTARTBLOCK) |
| 3865 | irec->br_startblock += distance; |
| 3866 | irec->br_startoff += distance; |
| 3867 | irec->br_blockcount -= distance; |
| 3868 | } |
| 3869 | |
| 3870 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3871 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3872 | irec->br_blockcount -= distance; |
| 3873 | } |
| 3874 | } |
| 3875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3876 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3877 | * Trim the returned map to the required bounds |
| 3878 | */ |
| 3879 | STATIC void |
| 3880 | xfs_bmapi_trim_map( |
| 3881 | struct xfs_bmbt_irec *mval, |
| 3882 | struct xfs_bmbt_irec *got, |
| 3883 | xfs_fileoff_t *bno, |
| 3884 | xfs_filblks_t len, |
| 3885 | xfs_fileoff_t obno, |
| 3886 | xfs_fileoff_t end, |
| 3887 | int n, |
| 3888 | int flags) |
| 3889 | { |
| 3890 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3891 | got->br_startoff + got->br_blockcount <= obno) { |
| 3892 | *mval = *got; |
| 3893 | if (isnullstartblock(got->br_startblock)) |
| 3894 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3895 | return; |
| 3896 | } |
| 3897 | |
| 3898 | if (obno > *bno) |
| 3899 | *bno = obno; |
| 3900 | ASSERT((*bno >= obno) || (n == 0)); |
| 3901 | ASSERT(*bno < end); |
| 3902 | mval->br_startoff = *bno; |
| 3903 | if (isnullstartblock(got->br_startblock)) |
| 3904 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3905 | else |
| 3906 | mval->br_startblock = got->br_startblock + |
| 3907 | (*bno - got->br_startoff); |
| 3908 | /* |
| 3909 | * Return the minimum of what we got and what we asked for for |
| 3910 | * the length. We can use the len variable here because it is |
| 3911 | * modified below and we could have been there before coming |
| 3912 | * here if the first part of the allocation didn't overlap what |
| 3913 | * was asked for. |
| 3914 | */ |
| 3915 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3916 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3917 | mval->br_state = got->br_state; |
| 3918 | ASSERT(mval->br_blockcount <= len); |
| 3919 | return; |
| 3920 | } |
| 3921 | |
| 3922 | /* |
| 3923 | * Update and validate the extent map to return |
| 3924 | */ |
| 3925 | STATIC void |
| 3926 | xfs_bmapi_update_map( |
| 3927 | struct xfs_bmbt_irec **map, |
| 3928 | xfs_fileoff_t *bno, |
| 3929 | xfs_filblks_t *len, |
| 3930 | xfs_fileoff_t obno, |
| 3931 | xfs_fileoff_t end, |
| 3932 | int *n, |
| 3933 | int flags) |
| 3934 | { |
| 3935 | xfs_bmbt_irec_t *mval = *map; |
| 3936 | |
| 3937 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3938 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3939 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3940 | (mval->br_startoff < obno)); |
| 3941 | |
| 3942 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3943 | *len = end - *bno; |
| 3944 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3945 | /* update previous map with new information */ |
| 3946 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3947 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3948 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3949 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3950 | mval[-1].br_state = mval->br_state; |
| 3951 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3952 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3953 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3954 | mval->br_startblock == mval[-1].br_startblock + |
| 3955 | mval[-1].br_blockcount && |
| 3956 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3957 | mval[-1].br_state == mval->br_state)) { |
| 3958 | ASSERT(mval->br_startoff == |
| 3959 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3960 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3961 | } else if (*n > 0 && |
| 3962 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3963 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3964 | mval->br_startoff == |
| 3965 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3966 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3967 | mval[-1].br_state = mval->br_state; |
| 3968 | } else if (!((*n == 0) && |
| 3969 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3970 | obno))) { |
| 3971 | mval++; |
| 3972 | (*n)++; |
| 3973 | } |
| 3974 | *map = mval; |
| 3975 | } |
| 3976 | |
| 3977 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3978 | * Map file blocks to filesystem blocks without allocation. |
| 3979 | */ |
| 3980 | int |
| 3981 | xfs_bmapi_read( |
| 3982 | struct xfs_inode *ip, |
| 3983 | xfs_fileoff_t bno, |
| 3984 | xfs_filblks_t len, |
| 3985 | struct xfs_bmbt_irec *mval, |
| 3986 | int *nmap, |
| 3987 | int flags) |
| 3988 | { |
| 3989 | struct xfs_mount *mp = ip->i_mount; |
| 3990 | struct xfs_ifork *ifp; |
| 3991 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3992 | xfs_fileoff_t obno; |
| 3993 | xfs_fileoff_t end; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3994 | xfs_extnum_t idx; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3995 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3996 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3997 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3998 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3999 | |
| 4000 | ASSERT(*nmap >= 1); |
| 4001 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4002 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4003 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4004 | |
| 4005 | if (unlikely(XFS_TEST_ERROR( |
| 4006 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4007 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 4008 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4009 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4010 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
| 4013 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4014 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4015 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4016 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4017 | |
| 4018 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4019 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4020 | /* No CoW fork? Return a hole. */ |
| 4021 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 4022 | mval->br_startoff = bno; |
| 4023 | mval->br_startblock = HOLESTARTBLOCK; |
| 4024 | mval->br_blockcount = len; |
| 4025 | mval->br_state = XFS_EXT_NORM; |
| 4026 | *nmap = 1; |
| 4027 | return 0; |
| 4028 | } |
| 4029 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4030 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4031 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4032 | if (error) |
| 4033 | return error; |
| 4034 | } |
| 4035 | |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4036 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) |
| 4037 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4038 | end = bno + len; |
| 4039 | obno = bno; |
| 4040 | |
| 4041 | while (bno < end && n < *nmap) { |
| 4042 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4043 | if (eof) |
| 4044 | got.br_startoff = end; |
| 4045 | if (got.br_startoff > bno) { |
| 4046 | /* Reading in a hole. */ |
| 4047 | mval->br_startoff = bno; |
| 4048 | mval->br_startblock = HOLESTARTBLOCK; |
| 4049 | mval->br_blockcount = |
| 4050 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4051 | mval->br_state = XFS_EXT_NORM; |
| 4052 | bno += mval->br_blockcount; |
| 4053 | len -= mval->br_blockcount; |
| 4054 | mval++; |
| 4055 | n++; |
| 4056 | continue; |
| 4057 | } |
| 4058 | |
| 4059 | /* set up the extent map to return. */ |
| 4060 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4061 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4062 | |
| 4063 | /* If we're done, stop now. */ |
| 4064 | if (bno >= end || n >= *nmap) |
| 4065 | break; |
| 4066 | |
| 4067 | /* Else go on to the next record. */ |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4068 | if (!xfs_iext_get_extent(ifp, ++idx, &got)) |
| 4069 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4070 | } |
| 4071 | *nmap = n; |
| 4072 | return 0; |
| 4073 | } |
| 4074 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 4075 | /* |
| 4076 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 4077 | * global pool and the extent inserted into the inode in-core extent tree. |
| 4078 | * |
| 4079 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 4080 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 4081 | * to the extent record that was inserted to the inode fork. |
| 4082 | * |
| 4083 | * Note that the allocated extent may have been merged with contiguous extents |
| 4084 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 4085 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 4086 | * look up the updated record in the inode fork. |
| 4087 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 4088 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4089 | xfs_bmapi_reserve_delalloc( |
| 4090 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4091 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4092 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4093 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4094 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4095 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4096 | xfs_extnum_t *lastx, |
| 4097 | int eof) |
| 4098 | { |
| 4099 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4100 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4101 | xfs_extlen_t alen; |
| 4102 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4103 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4104 | xfs_extlen_t extsz; |
| 4105 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4106 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4107 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4108 | /* |
| 4109 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 4110 | * tag the inode before we return. |
| 4111 | */ |
| 4112 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4113 | if (!eof) |
| 4114 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4115 | if (prealloc && alen >= len) |
| 4116 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4117 | |
| 4118 | /* Figure out the extent size, adjust alen */ |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 4119 | if (whichfork == XFS_COW_FORK) |
| 4120 | extsz = xfs_get_cowextsz_hint(ip); |
| 4121 | else |
| 4122 | extsz = xfs_get_extsz_hint(ip); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4123 | if (extsz) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 4124 | struct xfs_bmbt_irec prev; |
| 4125 | |
| 4126 | if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev)) |
| 4127 | prev.br_startoff = NULLFILEOFF; |
| 4128 | |
| 4129 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4130 | 1, 0, &aoff, &alen); |
| 4131 | ASSERT(!error); |
| 4132 | } |
| 4133 | |
| 4134 | if (rt) |
| 4135 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4136 | |
| 4137 | /* |
| 4138 | * Make a transaction-less quota reservation for delayed allocation |
| 4139 | * blocks. This number gets adjusted later. We return if we haven't |
| 4140 | * allocated blocks already inside this loop. |
| 4141 | */ |
| 4142 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4143 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4144 | if (error) |
| 4145 | return error; |
| 4146 | |
| 4147 | /* |
| 4148 | * Split changing sb for alen and indlen since they could be coming |
| 4149 | * from different places. |
| 4150 | */ |
| 4151 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4152 | ASSERT(indlen > 0); |
| 4153 | |
| 4154 | if (rt) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4155 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4156 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4157 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | if (error) |
| 4161 | goto out_unreserve_quota; |
| 4162 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4163 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4164 | if (error) |
| 4165 | goto out_unreserve_blocks; |
| 4166 | |
| 4167 | |
| 4168 | ip->i_delayed_blks += alen; |
| 4169 | |
| 4170 | got->br_startoff = aoff; |
| 4171 | got->br_startblock = nullstartblock(indlen); |
| 4172 | got->br_blockcount = alen; |
| 4173 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4174 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 4175 | xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4176 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4177 | /* |
| 4178 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 4179 | * preallocation can occur at the start or end of the extent, even when |
| 4180 | * prealloc == 0, so we must also check the aligned offset and length. |
| 4181 | */ |
| 4182 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4183 | xfs_inode_set_eofblocks_tag(ip); |
| 4184 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4185 | xfs_inode_set_cowblocks_tag(ip); |
| 4186 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4187 | return 0; |
| 4188 | |
| 4189 | out_unreserve_blocks: |
| 4190 | if (rt) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4191 | xfs_mod_frextents(mp, extsz); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4192 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4193 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4194 | out_unreserve_quota: |
| 4195 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 4196 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4197 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4198 | return error; |
| 4199 | } |
| 4200 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4201 | static int |
| 4202 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4203 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4204 | { |
| 4205 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4206 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4207 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4208 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4209 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4210 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4211 | ASSERT(bma->length > 0); |
| 4212 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4213 | /* |
| 4214 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4215 | * for in this bmap call but that wouldn't be as good. |
| 4216 | */ |
| 4217 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4218 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4219 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | 0e8d630 | 2016-11-24 11:40:32 +1100 | [diff] [blame] | 4220 | if (bma->idx) { |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4221 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4222 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4223 | } |
| 4224 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4225 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4226 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4227 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4228 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4232 | * Set the data type being allocated. For the data fork, the first data |
| 4233 | * in the file is treated differently to all other allocations. For the |
| 4234 | * attribute fork, we only need to ensure the allocated range is not on |
| 4235 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4236 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4237 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4238 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4239 | if (whichfork == XFS_DATA_FORK) { |
| 4240 | if (bma->offset == 0) |
| 4241 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4242 | else |
| 4243 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4244 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4245 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4246 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4249 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4250 | |
| 4251 | /* |
| 4252 | * Only want to do the alignment at the eof if it is userdata and |
| 4253 | * allocation length is larger than a stripe unit. |
| 4254 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4255 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4256 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4257 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4258 | if (error) |
| 4259 | return error; |
| 4260 | } |
| 4261 | |
| 4262 | error = xfs_bmap_alloc(bma); |
| 4263 | if (error) |
| 4264 | return error; |
| 4265 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4266 | if (bma->cur) |
| 4267 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4268 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4269 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4270 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4271 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4272 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4273 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4274 | } |
| 4275 | /* |
| 4276 | * Bump the number of extents we've allocated |
| 4277 | * in this call. |
| 4278 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4279 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4280 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4281 | if (bma->cur) |
| 4282 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4283 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4284 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4285 | bma->got.br_startoff = bma->offset; |
| 4286 | bma->got.br_startblock = bma->blkno; |
| 4287 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4288 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4289 | |
| 4290 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4291 | * In the data fork, a wasdelay extent has been initialized, so |
| 4292 | * shouldn't be flagged as unwritten. |
| 4293 | * |
| 4294 | * For the cow fork, however, we convert delalloc reservations |
| 4295 | * (extents allocated for speculative preallocation) to |
| 4296 | * allocated unwritten extents, and only convert the unwritten |
| 4297 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4298 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4299 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4300 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4301 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4302 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4303 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4304 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4305 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4306 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4307 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
| 4308 | whichfork, &bma->idx, &bma->cur, &bma->got, |
| 4309 | bma->firstblock, bma->dfops, &bma->logflags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4310 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4311 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4312 | if (error) |
| 4313 | return error; |
| 4314 | |
| 4315 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4316 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4317 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4318 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4319 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4320 | 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] | 4321 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4322 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4323 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4324 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4325 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4326 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4327 | return 0; |
| 4328 | } |
| 4329 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4330 | STATIC int |
| 4331 | xfs_bmapi_convert_unwritten( |
| 4332 | struct xfs_bmalloca *bma, |
| 4333 | struct xfs_bmbt_irec *mval, |
| 4334 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4335 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4336 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4337 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4338 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4339 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4340 | int error; |
| 4341 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4342 | /* check if we need to do unwritten->real conversion */ |
| 4343 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4344 | (flags & XFS_BMAPI_PREALLOC)) |
| 4345 | return 0; |
| 4346 | |
| 4347 | /* check if we need to do real->unwritten conversion */ |
| 4348 | if (mval->br_state == XFS_EXT_NORM && |
| 4349 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4350 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4351 | return 0; |
| 4352 | |
| 4353 | /* |
| 4354 | * Modify (by adding) the state flag, if writing. |
| 4355 | */ |
| 4356 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4357 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4358 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4359 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4360 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4361 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4362 | } |
| 4363 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4364 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4365 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4366 | /* |
| 4367 | * Before insertion into the bmbt, zero the range being converted |
| 4368 | * if required. |
| 4369 | */ |
| 4370 | if (flags & XFS_BMAPI_ZERO) { |
| 4371 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4372 | mval->br_blockcount); |
| 4373 | if (error) |
| 4374 | return error; |
| 4375 | } |
| 4376 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4377 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
| 4378 | &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4379 | &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4380 | /* |
| 4381 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4382 | * path because the conversion might not have done so (e.g., if the |
| 4383 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4384 | * in the transaction for the sake of fsync(), even if nothing has |
| 4385 | * changed, because fsync() will not force the log for this transaction |
| 4386 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4387 | * |
| 4388 | * Note: If we're only converting cow fork extents, there aren't |
| 4389 | * 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] | 4390 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4391 | if (whichfork != XFS_COW_FORK) |
| 4392 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4393 | if (error) |
| 4394 | return error; |
| 4395 | |
| 4396 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4397 | * Update our extent pointer, given that |
| 4398 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4399 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4400 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4401 | 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] | 4402 | |
| 4403 | /* |
| 4404 | * We may have combined previously unwritten space with written space, |
| 4405 | * so generate another request. |
| 4406 | */ |
| 4407 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4408 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4409 | return 0; |
| 4410 | } |
| 4411 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4412 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4413 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4414 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4415 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4416 | * locking problems. |
| 4417 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | * The returned value in "firstblock" from the first call in a transaction |
| 4419 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4420 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4421 | * the first call in "total"; if no allocation group has that many free |
| 4422 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4423 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4424 | int |
| 4425 | xfs_bmapi_write( |
| 4426 | struct xfs_trans *tp, /* transaction pointer */ |
| 4427 | struct xfs_inode *ip, /* incore inode */ |
| 4428 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4429 | xfs_filblks_t len, /* length to map in file */ |
| 4430 | int flags, /* XFS_BMAPI_... */ |
| 4431 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4432 | controls a.g. for allocs */ |
| 4433 | xfs_extlen_t total, /* total blocks needed */ |
| 4434 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4435 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4436 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4438 | struct xfs_mount *mp = ip->i_mount; |
| 4439 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4440 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4441 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4442 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4443 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4444 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4445 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4446 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4449 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4450 | int orig_flags; /* original flags arg value */ |
| 4451 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4452 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4453 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | |
| 4455 | orig_bno = bno; |
| 4456 | orig_len = len; |
| 4457 | orig_flags = flags; |
| 4458 | orig_mval = mval; |
| 4459 | orig_nmap = *nmap; |
| 4460 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4461 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4464 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4465 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4466 | ASSERT(tp != NULL || |
| 4467 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4468 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4469 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4470 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4471 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4472 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4473 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4474 | /* zeroing is for currently only for data extents, not metadata */ |
| 4475 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4476 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4477 | /* |
| 4478 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4479 | * but it makes no sense to do both at once. This would result in |
| 4480 | * zeroing the unwritten extent twice, but it still being an |
| 4481 | * unwritten extent.... |
| 4482 | */ |
| 4483 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4484 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | if (unlikely(XFS_TEST_ERROR( |
| 4487 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4488 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4490 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4491 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4495 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4498 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4499 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4500 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4501 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4503 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4505 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4506 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4507 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4511 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4512 | if (error) |
| 4513 | goto error0; |
| 4514 | } |
| 4515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | n = 0; |
| 4517 | end = bno + len; |
| 4518 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4519 | |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4520 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got)) |
| 4521 | eof = true; |
| 4522 | if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev)) |
| 4523 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4524 | bma.tp = tp; |
| 4525 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4526 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4527 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4528 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4529 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4532 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4533 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4534 | /* in hole or beyoned EOF? */ |
| 4535 | if (eof || bma.got.br_startoff > bno) { |
| 4536 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4537 | /* |
| 4538 | * For the COW fork we can reasonably get a |
| 4539 | * request for converting an extent that races |
| 4540 | * with other threads already having converted |
| 4541 | * part of it, as there converting COW to |
| 4542 | * regular blocks is not protected using the |
| 4543 | * IOLOCK. |
| 4544 | */ |
| 4545 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4546 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4547 | error = -EIO; |
| 4548 | goto error0; |
| 4549 | } |
| 4550 | |
| 4551 | if (eof || bno >= end) |
| 4552 | break; |
| 4553 | } else { |
| 4554 | need_alloc = true; |
| 4555 | } |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4556 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4557 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4558 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4559 | |
| 4560 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | * First, deal with the hole before the allocated space |
| 4562 | * that we found, if any. |
| 4563 | */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4564 | if (need_alloc || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4565 | bma.eof = eof; |
| 4566 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4567 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4568 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4569 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4570 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4571 | /* |
| 4572 | * There's a 32/64 bit type mismatch between the |
| 4573 | * allocation length request (which can be 64 bits in |
| 4574 | * length) and the bma length request, which is |
| 4575 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4576 | * check for 32-bit overflows and handle them here. |
| 4577 | */ |
| 4578 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4579 | bma.length = MAXEXTLEN; |
| 4580 | else |
| 4581 | bma.length = len; |
| 4582 | |
| 4583 | ASSERT(len > 0); |
| 4584 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4585 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | if (error) |
| 4587 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4588 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4589 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4590 | |
| 4591 | /* |
| 4592 | * If this is a CoW allocation, record the data in |
| 4593 | * the refcount btree for orphan recovery. |
| 4594 | */ |
| 4595 | if (whichfork == XFS_COW_FORK) { |
| 4596 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4597 | bma.blkno, bma.length); |
| 4598 | if (error) |
| 4599 | goto error0; |
| 4600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4602 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4603 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4604 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4605 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4607 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4608 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4609 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4610 | continue; |
| 4611 | if (error) |
| 4612 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4614 | /* update the extent map to return */ |
| 4615 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | /* |
| 4618 | * If we're done, stop now. Stop when we've allocated |
| 4619 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4620 | * the transaction may get too big. |
| 4621 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4622 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4624 | |
| 4625 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4626 | bma.prev = bma.got; |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4627 | if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got)) |
| 4628 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | /* |
| 4633 | * Transform from btree to extents, give it cur. |
| 4634 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4635 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4636 | int tmp_logflags = 0; |
| 4637 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4638 | ASSERT(bma.cur); |
| 4639 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4641 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | if (error) |
| 4643 | goto error0; |
| 4644 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4647 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4648 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | error0: |
| 4651 | /* |
| 4652 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4653 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4655 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4657 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4658 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4660 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | /* |
| 4662 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4663 | * detecting a case where the data is changed, there's an error, |
| 4664 | * and it's not logged so we don't shutdown when we should. |
| 4665 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4666 | if (bma.logflags) |
| 4667 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4668 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4669 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | if (!error) { |
| 4671 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4672 | XFS_FSB_TO_AGNO(mp, *firstblock) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | XFS_FSB_TO_AGNO(mp, |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4674 | bma.cur->bc_private.b.firstblock)); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4675 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4677 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4679 | } |
| 4680 | if (!error) |
| 4681 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4682 | orig_nmap, *nmap); |
| 4683 | return error; |
| 4684 | } |
| 4685 | |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4686 | static int |
| 4687 | xfs_bmapi_remap( |
| 4688 | struct xfs_trans *tp, |
| 4689 | struct xfs_inode *ip, |
| 4690 | xfs_fileoff_t bno, |
| 4691 | xfs_filblks_t len, |
| 4692 | xfs_fsblock_t startblock, |
| 4693 | struct xfs_defer_ops *dfops) |
| 4694 | { |
| 4695 | struct xfs_mount *mp = ip->i_mount; |
| 4696 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4697 | struct xfs_btree_cur *cur = NULL; |
| 4698 | xfs_fsblock_t firstblock = NULLFSBLOCK; |
| 4699 | struct xfs_bmbt_irec got; |
| 4700 | xfs_extnum_t idx; |
| 4701 | int logflags = 0, error; |
| 4702 | |
| 4703 | ASSERT(len > 0); |
| 4704 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4705 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 4706 | |
| 4707 | if (unlikely(XFS_TEST_ERROR( |
| 4708 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4709 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 4710 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4711 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4712 | return -EFSCORRUPTED; |
| 4713 | } |
| 4714 | |
| 4715 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4716 | return -EIO; |
| 4717 | |
| 4718 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4719 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4720 | if (error) |
| 4721 | return error; |
| 4722 | } |
| 4723 | |
| 4724 | if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) { |
| 4725 | /* make sure we only reflink into a hole. */ |
| 4726 | ASSERT(got.br_startoff > bno); |
| 4727 | ASSERT(got.br_startoff - bno >= len); |
| 4728 | } |
| 4729 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4730 | ip->i_d.di_nblocks += len; |
| 4731 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4732 | |
| 4733 | if (ifp->if_flags & XFS_IFBROOT) { |
| 4734 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
| 4735 | cur->bc_private.b.firstblock = firstblock; |
| 4736 | cur->bc_private.b.dfops = dfops; |
| 4737 | cur->bc_private.b.flags = 0; |
| 4738 | } |
| 4739 | |
| 4740 | got.br_startoff = bno; |
| 4741 | got.br_startblock = startblock; |
| 4742 | got.br_blockcount = len; |
| 4743 | got.br_state = XFS_EXT_NORM; |
| 4744 | |
| 4745 | error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur, |
| 4746 | &got, &firstblock, dfops, &logflags); |
| 4747 | if (error) |
| 4748 | goto error0; |
| 4749 | |
| 4750 | if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) { |
| 4751 | int tmp_logflags = 0; |
| 4752 | |
| 4753 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4754 | &tmp_logflags, XFS_DATA_FORK); |
| 4755 | logflags |= tmp_logflags; |
| 4756 | } |
| 4757 | |
| 4758 | error0: |
| 4759 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4760 | logflags &= ~XFS_ILOG_DEXT; |
| 4761 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4762 | logflags &= ~XFS_ILOG_DBROOT; |
| 4763 | |
| 4764 | if (logflags) |
| 4765 | xfs_trans_log_inode(tp, ip, logflags); |
| 4766 | if (cur) { |
| 4767 | xfs_btree_del_cursor(cur, |
| 4768 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4769 | } |
| 4770 | return error; |
| 4771 | } |
| 4772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4774 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4775 | * indlen reservation must be shared across the two new extents that are left |
| 4776 | * behind. |
| 4777 | * |
| 4778 | * Given the original reservation and the worst case indlen for the two new |
| 4779 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4780 | * reservation fairly across the two new extents. If necessary, steal available |
| 4781 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4782 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4783 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4784 | */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4785 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4786 | xfs_bmap_split_indlen( |
| 4787 | xfs_filblks_t ores, /* original res. */ |
| 4788 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4789 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4790 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4791 | { |
| 4792 | xfs_filblks_t len1 = *indlen1; |
| 4793 | xfs_filblks_t len2 = *indlen2; |
| 4794 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4795 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4796 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4797 | |
| 4798 | /* |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4799 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4800 | * indlen for both new extents. |
| 4801 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4802 | if (ores < nres && avail) |
| 4803 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4804 | ores += stolen; |
| 4805 | |
| 4806 | /* nothing else to do if we've satisfied the new reservation */ |
| 4807 | if (ores >= nres) |
| 4808 | return stolen; |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4809 | |
| 4810 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4811 | * We can't meet the total required reservation for the two extents. |
| 4812 | * Calculate the percent of the overall shortage between both extents |
| 4813 | * and apply this percentage to each of the requested indlen values. |
| 4814 | * This distributes the shortage fairly and reduces the chances that one |
| 4815 | * of the two extents is left with nothing when extents are repeatedly |
| 4816 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4817 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4818 | resfactor = (ores * 100); |
| 4819 | do_div(resfactor, nres); |
| 4820 | len1 *= resfactor; |
| 4821 | do_div(len1, 100); |
| 4822 | len2 *= resfactor; |
| 4823 | do_div(len2, 100); |
| 4824 | ASSERT(len1 + len2 <= ores); |
| 4825 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4826 | |
| 4827 | /* |
| 4828 | * Hand out the remainder to each extent. If one of the two reservations |
| 4829 | * is zero, we want to make sure that one gets a block first. The loop |
| 4830 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4831 | * is zero. |
| 4832 | */ |
| 4833 | ores -= (len1 + len2); |
| 4834 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4835 | if (ores && !len2 && *indlen2) { |
| 4836 | len2++; |
| 4837 | ores--; |
| 4838 | } |
| 4839 | while (ores) { |
| 4840 | if (len1 < *indlen1) { |
| 4841 | len1++; |
| 4842 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4843 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4844 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4845 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4846 | if (len2 < *indlen2) { |
| 4847 | len2++; |
| 4848 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4849 | } |
| 4850 | } |
| 4851 | |
| 4852 | *indlen1 = len1; |
| 4853 | *indlen2 = len2; |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4854 | |
| 4855 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4856 | } |
| 4857 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4858 | int |
| 4859 | xfs_bmap_del_extent_delay( |
| 4860 | struct xfs_inode *ip, |
| 4861 | int whichfork, |
| 4862 | xfs_extnum_t *idx, |
| 4863 | struct xfs_bmbt_irec *got, |
| 4864 | struct xfs_bmbt_irec *del) |
| 4865 | { |
| 4866 | struct xfs_mount *mp = ip->i_mount; |
| 4867 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4868 | struct xfs_bmbt_irec new; |
| 4869 | int64_t da_old, da_new, da_diff = 0; |
| 4870 | xfs_fileoff_t del_endoff, got_endoff; |
| 4871 | xfs_filblks_t got_indlen, new_indlen, stolen; |
| 4872 | int error = 0, state = 0; |
| 4873 | bool isrt; |
| 4874 | |
| 4875 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4876 | |
| 4877 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4878 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4879 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4880 | da_old = startblockval(got->br_startblock); |
| 4881 | da_new = 0; |
| 4882 | |
| 4883 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 4884 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4885 | ASSERT(del->br_blockcount > 0); |
| 4886 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4887 | ASSERT(got_endoff >= del_endoff); |
| 4888 | |
| 4889 | if (isrt) { |
| 4890 | int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
| 4891 | |
| 4892 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4893 | xfs_mod_frextents(mp, rtexts); |
| 4894 | } |
| 4895 | |
| 4896 | /* |
| 4897 | * Update the inode delalloc counter now and wait to update the |
| 4898 | * sb counters as we might have to borrow some blocks for the |
| 4899 | * indirect block accounting. |
| 4900 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4901 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4902 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4903 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4904 | if (error) |
| 4905 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4906 | ip->i_delayed_blks -= del->br_blockcount; |
| 4907 | |
| 4908 | if (whichfork == XFS_COW_FORK) |
| 4909 | state |= BMAP_COWFORK; |
| 4910 | |
| 4911 | if (got->br_startoff == del->br_startoff) |
| 4912 | state |= BMAP_LEFT_CONTIG; |
| 4913 | if (got_endoff == del_endoff) |
| 4914 | state |= BMAP_RIGHT_CONTIG; |
| 4915 | |
| 4916 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 4917 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 4918 | /* |
| 4919 | * Matches the whole extent. Delete the entry. |
| 4920 | */ |
| 4921 | xfs_iext_remove(ip, *idx, 1, state); |
| 4922 | --*idx; |
| 4923 | break; |
| 4924 | case BMAP_LEFT_CONTIG: |
| 4925 | /* |
| 4926 | * Deleting the first part of the extent. |
| 4927 | */ |
| 4928 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4929 | got->br_startoff = del_endoff; |
| 4930 | got->br_blockcount -= del->br_blockcount; |
| 4931 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4932 | got->br_blockcount), da_old); |
| 4933 | got->br_startblock = nullstartblock((int)da_new); |
| 4934 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4935 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4936 | break; |
| 4937 | case BMAP_RIGHT_CONTIG: |
| 4938 | /* |
| 4939 | * Deleting the last part of the extent. |
| 4940 | */ |
| 4941 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4942 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4943 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4944 | got->br_blockcount), da_old); |
| 4945 | got->br_startblock = nullstartblock((int)da_new); |
| 4946 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4947 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4948 | break; |
| 4949 | case 0: |
| 4950 | /* |
| 4951 | * Deleting the middle of the extent. |
| 4952 | * |
| 4953 | * Distribute the original indlen reservation across the two new |
| 4954 | * extents. Steal blocks from the deleted extent if necessary. |
| 4955 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4956 | * Warn if either of the new indlen reservations is zero as this |
| 4957 | * can lead to delalloc problems. |
| 4958 | */ |
| 4959 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4960 | |
| 4961 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4962 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4963 | |
| 4964 | new.br_blockcount = got_endoff - del_endoff; |
| 4965 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4966 | |
| 4967 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4968 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4969 | del->br_blockcount); |
| 4970 | |
| 4971 | got->br_startblock = nullstartblock((int)got_indlen); |
| 4972 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4973 | trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_); |
| 4974 | |
| 4975 | new.br_startoff = del_endoff; |
| 4976 | new.br_state = got->br_state; |
| 4977 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4978 | |
| 4979 | ++*idx; |
| 4980 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 4981 | |
| 4982 | da_new = got_indlen + new_indlen - stolen; |
| 4983 | del->br_blockcount -= stolen; |
| 4984 | break; |
| 4985 | } |
| 4986 | |
| 4987 | ASSERT(da_old >= da_new); |
| 4988 | da_diff = da_old - da_new; |
| 4989 | if (!isrt) |
| 4990 | da_diff += del->br_blockcount; |
| 4991 | if (da_diff) |
| 4992 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4993 | return error; |
| 4994 | } |
| 4995 | |
| 4996 | void |
| 4997 | xfs_bmap_del_extent_cow( |
| 4998 | struct xfs_inode *ip, |
| 4999 | xfs_extnum_t *idx, |
| 5000 | struct xfs_bmbt_irec *got, |
| 5001 | struct xfs_bmbt_irec *del) |
| 5002 | { |
| 5003 | struct xfs_mount *mp = ip->i_mount; |
| 5004 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 5005 | struct xfs_bmbt_irec new; |
| 5006 | xfs_fileoff_t del_endoff, got_endoff; |
| 5007 | int state = BMAP_COWFORK; |
| 5008 | |
| 5009 | XFS_STATS_INC(mp, xs_del_exlist); |
| 5010 | |
| 5011 | del_endoff = del->br_startoff + del->br_blockcount; |
| 5012 | got_endoff = got->br_startoff + got->br_blockcount; |
| 5013 | |
| 5014 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5015 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 5016 | ASSERT(del->br_blockcount > 0); |
| 5017 | ASSERT(got->br_startoff <= del->br_startoff); |
| 5018 | ASSERT(got_endoff >= del_endoff); |
| 5019 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 5020 | |
| 5021 | if (got->br_startoff == del->br_startoff) |
| 5022 | state |= BMAP_LEFT_CONTIG; |
| 5023 | if (got_endoff == del_endoff) |
| 5024 | state |= BMAP_RIGHT_CONTIG; |
| 5025 | |
| 5026 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 5027 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 5028 | /* |
| 5029 | * Matches the whole extent. Delete the entry. |
| 5030 | */ |
| 5031 | xfs_iext_remove(ip, *idx, 1, state); |
| 5032 | --*idx; |
| 5033 | break; |
| 5034 | case BMAP_LEFT_CONTIG: |
| 5035 | /* |
| 5036 | * Deleting the first part of the extent. |
| 5037 | */ |
| 5038 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5039 | got->br_startoff = del_endoff; |
| 5040 | got->br_blockcount -= del->br_blockcount; |
| 5041 | got->br_startblock = del->br_startblock + del->br_blockcount; |
| 5042 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5043 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5044 | break; |
| 5045 | case BMAP_RIGHT_CONTIG: |
| 5046 | /* |
| 5047 | * Deleting the last part of the extent. |
| 5048 | */ |
| 5049 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5050 | got->br_blockcount -= del->br_blockcount; |
| 5051 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5052 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5053 | break; |
| 5054 | case 0: |
| 5055 | /* |
| 5056 | * Deleting the middle of the extent. |
| 5057 | */ |
| 5058 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5059 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 5060 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5061 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5062 | |
| 5063 | new.br_startoff = del_endoff; |
| 5064 | new.br_blockcount = got_endoff - del_endoff; |
| 5065 | new.br_state = got->br_state; |
| 5066 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 5067 | |
| 5068 | ++*idx; |
| 5069 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 5070 | break; |
| 5071 | } |
| 5072 | } |
| 5073 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5074 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5075 | * Called by xfs_bmapi to update file extent records and the btree |
| 5076 | * after removing space (or undoing a delayed allocation). |
| 5077 | */ |
| 5078 | STATIC int /* error */ |
| 5079 | xfs_bmap_del_extent( |
| 5080 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5081 | xfs_trans_t *tp, /* current transaction pointer */ |
| 5082 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5083 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5084 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 5085 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 5086 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5087 | int whichfork, /* data or attr fork */ |
| 5088 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5089 | { |
| 5090 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 5091 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 5092 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 5093 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 5094 | int delay; /* current block is delayed allocated */ |
| 5095 | int do_fx; /* free extent at end of routine */ |
| 5096 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
| 5097 | int error; /* error return value */ |
| 5098 | int flags; /* inode logging flags */ |
| 5099 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 5100 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 5101 | int i; /* temp state */ |
| 5102 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5103 | xfs_mount_t *mp; /* mount structure */ |
| 5104 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 5105 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 5106 | /* REFERENCED */ |
| 5107 | uint qfield; /* quota field to update */ |
| 5108 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 5109 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 5110 | int state = 0; |
| 5111 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5112 | mp = ip->i_mount; |
| 5113 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5114 | |
| 5115 | if (whichfork == XFS_ATTR_FORK) |
| 5116 | state |= BMAP_ATTRFORK; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5117 | else if (whichfork == XFS_COW_FORK) |
| 5118 | state |= BMAP_COWFORK; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5119 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5120 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5121 | ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp))); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5122 | ASSERT(del->br_blockcount > 0); |
| 5123 | ep = xfs_iext_get_ext(ifp, *idx); |
| 5124 | xfs_bmbt_get_all(ep, &got); |
| 5125 | ASSERT(got.br_startoff <= del->br_startoff); |
| 5126 | del_endoff = del->br_startoff + del->br_blockcount; |
| 5127 | got_endoff = got.br_startoff + got.br_blockcount; |
| 5128 | ASSERT(got_endoff >= del_endoff); |
| 5129 | delay = isnullstartblock(got.br_startblock); |
| 5130 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
| 5131 | flags = 0; |
| 5132 | qfield = 0; |
| 5133 | error = 0; |
| 5134 | /* |
| 5135 | * If deleting a real allocation, must free up the disk space. |
| 5136 | */ |
| 5137 | if (!delay) { |
| 5138 | flags = XFS_ILOG_CORE; |
| 5139 | /* |
| 5140 | * Realtime allocation. Free it and record di_nblocks update. |
| 5141 | */ |
| 5142 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 5143 | xfs_fsblock_t bno; |
| 5144 | xfs_filblks_t len; |
| 5145 | |
| 5146 | ASSERT(do_mod(del->br_blockcount, |
| 5147 | mp->m_sb.sb_rextsize) == 0); |
| 5148 | ASSERT(do_mod(del->br_startblock, |
| 5149 | mp->m_sb.sb_rextsize) == 0); |
| 5150 | bno = del->br_startblock; |
| 5151 | len = del->br_blockcount; |
| 5152 | do_div(bno, mp->m_sb.sb_rextsize); |
| 5153 | do_div(len, mp->m_sb.sb_rextsize); |
| 5154 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 5155 | if (error) |
| 5156 | goto done; |
| 5157 | do_fx = 0; |
| 5158 | nblks = len * mp->m_sb.sb_rextsize; |
| 5159 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 5160 | } |
| 5161 | /* |
| 5162 | * Ordinary allocation. |
| 5163 | */ |
| 5164 | else { |
| 5165 | do_fx = 1; |
| 5166 | nblks = del->br_blockcount; |
| 5167 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 5168 | } |
| 5169 | /* |
| 5170 | * Set up del_endblock and cur for later. |
| 5171 | */ |
| 5172 | del_endblock = del->br_startblock + del->br_blockcount; |
| 5173 | if (cur) { |
| 5174 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 5175 | got.br_startblock, got.br_blockcount, |
| 5176 | &i))) |
| 5177 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5178 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5179 | } |
| 5180 | da_old = da_new = 0; |
| 5181 | } else { |
| 5182 | da_old = startblockval(got.br_startblock); |
| 5183 | da_new = 0; |
| 5184 | nblks = 0; |
| 5185 | do_fx = 0; |
| 5186 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 5187 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5188 | /* |
| 5189 | * Set flag value to use in switch statement. |
| 5190 | * Left-contig is 2, right-contig is 1. |
| 5191 | */ |
| 5192 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 5193 | (got_endoff == del_endoff)) { |
| 5194 | case 3: |
| 5195 | /* |
| 5196 | * Matches the whole extent. Delete the entry. |
| 5197 | */ |
Darrick J. Wong | 4862cfe | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 5198 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5199 | xfs_iext_remove(ip, *idx, 1, |
| 5200 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 5201 | --*idx; |
| 5202 | if (delay) |
| 5203 | break; |
| 5204 | |
| 5205 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5206 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5207 | flags |= XFS_ILOG_CORE; |
| 5208 | if (!cur) { |
| 5209 | flags |= xfs_ilog_fext(whichfork); |
| 5210 | break; |
| 5211 | } |
| 5212 | if ((error = xfs_btree_delete(cur, &i))) |
| 5213 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5214 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5215 | break; |
| 5216 | |
| 5217 | case 2: |
| 5218 | /* |
| 5219 | * Deleting the first part of the extent. |
| 5220 | */ |
| 5221 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5222 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 5223 | temp = got.br_blockcount - del->br_blockcount; |
| 5224 | xfs_bmbt_set_blockcount(ep, temp); |
| 5225 | if (delay) { |
| 5226 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 5227 | da_old); |
| 5228 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 5229 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5230 | da_new = temp; |
| 5231 | break; |
| 5232 | } |
| 5233 | xfs_bmbt_set_startblock(ep, del_endblock); |
| 5234 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5235 | if (!cur) { |
| 5236 | flags |= xfs_ilog_fext(whichfork); |
| 5237 | break; |
| 5238 | } |
| 5239 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 5240 | got.br_blockcount - del->br_blockcount, |
| 5241 | got.br_state))) |
| 5242 | goto done; |
| 5243 | break; |
| 5244 | |
| 5245 | case 1: |
| 5246 | /* |
| 5247 | * Deleting the last part of the extent. |
| 5248 | */ |
| 5249 | temp = got.br_blockcount - del->br_blockcount; |
| 5250 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5251 | xfs_bmbt_set_blockcount(ep, temp); |
| 5252 | if (delay) { |
| 5253 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 5254 | da_old); |
| 5255 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 5256 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5257 | da_new = temp; |
| 5258 | break; |
| 5259 | } |
| 5260 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5261 | if (!cur) { |
| 5262 | flags |= xfs_ilog_fext(whichfork); |
| 5263 | break; |
| 5264 | } |
| 5265 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 5266 | got.br_startblock, |
| 5267 | got.br_blockcount - del->br_blockcount, |
| 5268 | got.br_state))) |
| 5269 | goto done; |
| 5270 | break; |
| 5271 | |
| 5272 | case 0: |
| 5273 | /* |
| 5274 | * Deleting the middle of the extent. |
| 5275 | */ |
| 5276 | temp = del->br_startoff - got.br_startoff; |
| 5277 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5278 | xfs_bmbt_set_blockcount(ep, temp); |
| 5279 | new.br_startoff = del_endoff; |
| 5280 | temp2 = got_endoff - del_endoff; |
| 5281 | new.br_blockcount = temp2; |
| 5282 | new.br_state = got.br_state; |
| 5283 | if (!delay) { |
| 5284 | new.br_startblock = del_endblock; |
| 5285 | flags |= XFS_ILOG_CORE; |
| 5286 | if (cur) { |
| 5287 | if ((error = xfs_bmbt_update(cur, |
| 5288 | got.br_startoff, |
| 5289 | got.br_startblock, temp, |
| 5290 | got.br_state))) |
| 5291 | goto done; |
| 5292 | if ((error = xfs_btree_increment(cur, 0, &i))) |
| 5293 | goto done; |
| 5294 | cur->bc_rec.b = new; |
| 5295 | error = xfs_btree_insert(cur, &i); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5296 | if (error && error != -ENOSPC) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5297 | goto done; |
| 5298 | /* |
| 5299 | * If get no-space back from btree insert, |
| 5300 | * it tried a split, and we have a zero |
| 5301 | * block reservation. |
| 5302 | * Fix up our state and return the error. |
| 5303 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5304 | if (error == -ENOSPC) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5305 | /* |
| 5306 | * Reset the cursor, don't trust |
| 5307 | * it after any insert operation. |
| 5308 | */ |
| 5309 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 5310 | got.br_startoff, |
| 5311 | got.br_startblock, |
| 5312 | temp, &i))) |
| 5313 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5314 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 5315 | i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5316 | /* |
| 5317 | * Update the btree record back |
| 5318 | * to the original value. |
| 5319 | */ |
| 5320 | if ((error = xfs_bmbt_update(cur, |
| 5321 | got.br_startoff, |
| 5322 | got.br_startblock, |
| 5323 | got.br_blockcount, |
| 5324 | got.br_state))) |
| 5325 | goto done; |
| 5326 | /* |
| 5327 | * Reset the extent record back |
| 5328 | * to the original value. |
| 5329 | */ |
| 5330 | xfs_bmbt_set_blockcount(ep, |
| 5331 | got.br_blockcount); |
| 5332 | flags = 0; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5333 | error = -ENOSPC; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5334 | goto done; |
| 5335 | } |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5336 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5337 | } else |
| 5338 | flags |= xfs_ilog_fext(whichfork); |
| 5339 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5340 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5341 | } else { |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5342 | xfs_filblks_t stolen; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5343 | ASSERT(whichfork == XFS_DATA_FORK); |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5344 | |
| 5345 | /* |
| 5346 | * Distribute the original indlen reservation across the |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5347 | * two new extents. Steal blocks from the deleted extent |
| 5348 | * if necessary. Stealing blocks simply fudges the |
| 5349 | * fdblocks accounting in xfs_bunmapi(). |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5350 | */ |
| 5351 | temp = xfs_bmap_worst_indlen(ip, got.br_blockcount); |
| 5352 | temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5353 | stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2, |
| 5354 | del->br_blockcount); |
| 5355 | da_new = temp + temp2 - stolen; |
| 5356 | del->br_blockcount -= stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5357 | |
| 5358 | /* |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5359 | * Set the reservation for each extent. Warn if either |
| 5360 | * is zero as this can lead to delalloc problems. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5361 | */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5362 | WARN_ON_ONCE(!temp || !temp2); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5363 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5364 | new.br_startblock = nullstartblock((int)temp2); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5365 | } |
| 5366 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5367 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 5368 | ++*idx; |
| 5369 | break; |
| 5370 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5371 | |
| 5372 | /* remove reverse mapping */ |
| 5373 | if (!delay) { |
| 5374 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5375 | if (error) |
| 5376 | goto done; |
| 5377 | } |
| 5378 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5379 | /* |
| 5380 | * If we need to, add to list of extents to delete. |
| 5381 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5382 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5383 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5384 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5385 | if (error) |
| 5386 | goto done; |
| 5387 | } else |
| 5388 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5389 | del->br_blockcount, NULL); |
| 5390 | } |
| 5391 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5392 | /* |
| 5393 | * Adjust inode # blocks in the file. |
| 5394 | */ |
| 5395 | if (nblks) |
| 5396 | ip->i_d.di_nblocks -= nblks; |
| 5397 | /* |
| 5398 | * Adjust quota data. |
| 5399 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5400 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5401 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5402 | |
| 5403 | /* |
| 5404 | * Account for change in delayed indirect blocks. |
| 5405 | * Nothing to do for disk quota accounting here. |
| 5406 | */ |
| 5407 | ASSERT(da_old >= da_new); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 5408 | if (da_old > da_new) |
| 5409 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5410 | done: |
| 5411 | *logflagsp = flags; |
| 5412 | return error; |
| 5413 | } |
| 5414 | |
| 5415 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | * Unmap (remove) blocks from a file. |
| 5417 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5418 | * that value. If not all extents in the block range can be removed then |
| 5419 | * *done is set. |
| 5420 | */ |
| 5421 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5422 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | xfs_trans_t *tp, /* transaction pointer */ |
| 5424 | struct xfs_inode *ip, /* incore inode */ |
| 5425 | xfs_fileoff_t bno, /* starting offset to unmap */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5426 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | int flags, /* misc flags */ |
| 5428 | xfs_extnum_t nexts, /* number of extents max */ |
| 5429 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5430 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5431 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | { |
| 5433 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5434 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5435 | int error; /* error return value */ |
| 5436 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5437 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5439 | int isrt; /* freeing in rt area */ |
| 5440 | xfs_extnum_t lastx; /* last extent index used */ |
| 5441 | int logflags; /* transaction logging flags */ |
| 5442 | xfs_extlen_t mod; /* rt extent offset */ |
| 5443 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5445 | int tmp_logflags; /* partial logging flags */ |
| 5446 | int wasdel; /* was a delayed alloc extent */ |
| 5447 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5449 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5451 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5452 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5453 | whichfork = xfs_bmapi_whichfork(flags); |
| 5454 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5456 | if (unlikely( |
| 5457 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5458 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5459 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5460 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5461 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5462 | } |
| 5463 | mp = ip->i_mount; |
| 5464 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5465 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5466 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5467 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | ASSERT(len > 0); |
| 5469 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5472 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5473 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5474 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5475 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | return 0; |
| 5477 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5478 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5479 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | start = bno; |
| 5481 | bno = start + len - 1; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | /* |
| 5484 | * Check to see if the given block number is past the end of the |
| 5485 | * file, back up to the last block if so... |
| 5486 | */ |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5487 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) { |
| 5488 | ASSERT(lastx > 0); |
| 5489 | xfs_iext_get_extent(ifp, --lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5491 | } |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | logflags = 0; |
| 5494 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5495 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5496 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5498 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | cur->bc_private.b.flags = 0; |
| 5500 | } else |
| 5501 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5502 | |
| 5503 | if (isrt) { |
| 5504 | /* |
| 5505 | * Synchronize by locking the bitmap inode. |
| 5506 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5507 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5508 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5509 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5510 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | extno = 0; |
| 5514 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5515 | (nexts == 0 || extno < nexts)) { |
| 5516 | /* |
| 5517 | * Is the found extent after a hole in which bno lives? |
| 5518 | * Just back up to the previous extent, if so. |
| 5519 | */ |
| 5520 | if (got.br_startoff > bno) { |
| 5521 | if (--lastx < 0) |
| 5522 | break; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5523 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | } |
| 5525 | /* |
| 5526 | * Is the last block of this extent before the range |
| 5527 | * we're supposed to delete? If so, we're done. |
| 5528 | */ |
| 5529 | bno = XFS_FILEOFF_MIN(bno, |
| 5530 | got.br_startoff + got.br_blockcount - 1); |
| 5531 | if (bno < start) |
| 5532 | break; |
| 5533 | /* |
| 5534 | * Then deal with the (possibly delayed) allocated space |
| 5535 | * we found. |
| 5536 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5538 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | if (got.br_startoff < start) { |
| 5540 | del.br_startoff = start; |
| 5541 | del.br_blockcount -= start - got.br_startoff; |
| 5542 | if (!wasdel) |
| 5543 | del.br_startblock += start - got.br_startoff; |
| 5544 | } |
| 5545 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5546 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5547 | sum = del.br_startblock + del.br_blockcount; |
| 5548 | if (isrt && |
| 5549 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5550 | /* |
| 5551 | * Realtime extent not lined up at the end. |
| 5552 | * The extent could have been split into written |
| 5553 | * and unwritten pieces, or we could just be |
| 5554 | * unmapping part of it. But we can't really |
| 5555 | * get rid of part of a realtime extent. |
| 5556 | */ |
| 5557 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5558 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | /* |
| 5560 | * This piece is unwritten, or we're not |
| 5561 | * using unwritten extents. Skip over it. |
| 5562 | */ |
| 5563 | ASSERT(bno >= mod); |
| 5564 | bno -= mod > del.br_blockcount ? |
| 5565 | del.br_blockcount : mod; |
| 5566 | if (bno < got.br_startoff) { |
| 5567 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5568 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5569 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | } |
| 5571 | continue; |
| 5572 | } |
| 5573 | /* |
| 5574 | * It's written, turn it unwritten. |
| 5575 | * This is better than zeroing it. |
| 5576 | */ |
| 5577 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5578 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | /* |
| 5580 | * If this spans a realtime extent boundary, |
| 5581 | * chop it back to the start of the one we end at. |
| 5582 | */ |
| 5583 | if (del.br_blockcount > mod) { |
| 5584 | del.br_startoff += del.br_blockcount - mod; |
| 5585 | del.br_startblock += del.br_blockcount - mod; |
| 5586 | del.br_blockcount = mod; |
| 5587 | } |
| 5588 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5589 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5590 | whichfork, &lastx, &cur, &del, |
| 5591 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | if (error) |
| 5593 | goto error0; |
| 5594 | goto nodelete; |
| 5595 | } |
| 5596 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5597 | /* |
| 5598 | * Realtime extent is lined up at the end but not |
| 5599 | * at the front. We'll get rid of full extents if |
| 5600 | * we can. |
| 5601 | */ |
| 5602 | mod = mp->m_sb.sb_rextsize - mod; |
| 5603 | if (del.br_blockcount > mod) { |
| 5604 | del.br_blockcount -= mod; |
| 5605 | del.br_startoff += mod; |
| 5606 | del.br_startblock += mod; |
| 5607 | } else if ((del.br_startoff == start && |
| 5608 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5609 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5610 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | /* |
| 5612 | * Can't make it unwritten. There isn't |
| 5613 | * a full extent here so just skip it. |
| 5614 | */ |
| 5615 | ASSERT(bno >= del.br_blockcount); |
| 5616 | bno -= del.br_blockcount; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5617 | if (got.br_startoff > bno && --lastx >= 0) |
| 5618 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | continue; |
| 5620 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5621 | struct xfs_bmbt_irec prev; |
| 5622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | /* |
| 5624 | * This one is already unwritten. |
| 5625 | * It must have a written left neighbor. |
| 5626 | * Unwrite the killed part of that one and |
| 5627 | * try again. |
| 5628 | */ |
| 5629 | ASSERT(lastx > 0); |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5630 | xfs_iext_get_extent(ifp, lastx - 1, &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5632 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | ASSERT(del.br_startblock == |
| 5634 | prev.br_startblock + prev.br_blockcount); |
| 5635 | if (prev.br_startoff < start) { |
| 5636 | mod = start - prev.br_startoff; |
| 5637 | prev.br_blockcount -= mod; |
| 5638 | prev.br_startblock += mod; |
| 5639 | prev.br_startoff = start; |
| 5640 | } |
| 5641 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5642 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5643 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5644 | ip, whichfork, &lastx, &cur, |
| 5645 | &prev, firstblock, dfops, |
| 5646 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | if (error) |
| 5648 | goto error0; |
| 5649 | goto nodelete; |
| 5650 | } else { |
| 5651 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5652 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5653 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5654 | ip, whichfork, &lastx, &cur, |
| 5655 | &del, firstblock, dfops, |
| 5656 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | if (error) |
| 5658 | goto error0; |
| 5659 | goto nodelete; |
| 5660 | } |
| 5661 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | /* |
| 5664 | * If it's the case where the directory code is running |
| 5665 | * with no block reservation, and the deleted block is in |
| 5666 | * the middle of its extent, and the resulting insert |
| 5667 | * of an extent would cause transformation to btree format, |
| 5668 | * then reject it. The calling code will then swap |
| 5669 | * blocks around instead. |
| 5670 | * We have to do this now, rather than waiting for the |
| 5671 | * conversion to btree format, since the transaction |
| 5672 | * will be dirty. |
| 5673 | */ |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5674 | if (!wasdel && tp->t_blk_res == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5676 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ |
| 5677 | XFS_IFORK_MAXEXT(ip, whichfork) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | del.br_startoff > got.br_startoff && |
| 5679 | del.br_startoff + del.br_blockcount < |
| 5680 | got.br_startoff + got.br_blockcount) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5681 | error = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | goto error0; |
| 5683 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5684 | |
| 5685 | /* |
| 5686 | * Unreserve quota and update realtime free space, if |
| 5687 | * appropriate. If delayed allocation, update the inode delalloc |
| 5688 | * counter now and wait to update the sb counters as |
| 5689 | * xfs_bmap_del_extent() might need to borrow some blocks. |
| 5690 | */ |
| 5691 | if (wasdel) { |
| 5692 | ASSERT(startblockval(del.br_startblock) > 0); |
| 5693 | if (isrt) { |
| 5694 | xfs_filblks_t rtexts; |
| 5695 | |
| 5696 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5697 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5698 | xfs_mod_frextents(mp, (int64_t)rtexts); |
| 5699 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5700 | ip, -((long)del.br_blockcount), 0, |
| 5701 | XFS_QMOPT_RES_RTBLKS); |
| 5702 | } else { |
| 5703 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5704 | ip, -((long)del.br_blockcount), 0, |
| 5705 | XFS_QMOPT_RES_REGBLKS); |
| 5706 | } |
| 5707 | ip->i_delayed_blks -= del.br_blockcount; |
| 5708 | if (cur) |
| 5709 | cur->bc_private.b.flags |= |
| 5710 | XFS_BTCUR_BPRV_WASDEL; |
| 5711 | } else if (cur) |
| 5712 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5713 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5714 | error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del, |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5715 | &tmp_logflags, whichfork, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | logflags |= tmp_logflags; |
| 5717 | if (error) |
| 5718 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5719 | |
| 5720 | if (!isrt && wasdel) |
| 5721 | xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false); |
| 5722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | bno = del.br_startoff - 1; |
| 5724 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | /* |
| 5726 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5729 | if (lastx >= 0) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5730 | xfs_iext_get_extent(ifp, lastx, &got); |
| 5731 | if (got.br_startoff > bno && --lastx >= 0) |
| 5732 | xfs_iext_get_extent(ifp, lastx, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | extno++; |
| 5735 | } |
| 5736 | } |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5737 | if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0) |
| 5738 | *rlen = 0; |
| 5739 | else |
| 5740 | *rlen = bno - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | /* |
| 5743 | * Convert to a btree if necessary. |
| 5744 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5745 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5747 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | &cur, 0, &tmp_logflags, whichfork); |
| 5749 | logflags |= tmp_logflags; |
| 5750 | if (error) |
| 5751 | goto error0; |
| 5752 | } |
| 5753 | /* |
| 5754 | * transform from btree to extents, give it cur |
| 5755 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5756 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | ASSERT(cur != NULL); |
| 5758 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5759 | whichfork); |
| 5760 | logflags |= tmp_logflags; |
| 5761 | if (error) |
| 5762 | goto error0; |
| 5763 | } |
| 5764 | /* |
| 5765 | * transform from extents to local? |
| 5766 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | error = 0; |
| 5768 | error0: |
| 5769 | /* |
| 5770 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5771 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5772 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5773 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5775 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5776 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5778 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | /* |
| 5780 | * Log inode even in the error case, if the transaction |
| 5781 | * is dirty we'll need to shut down the filesystem. |
| 5782 | */ |
| 5783 | if (logflags) |
| 5784 | xfs_trans_log_inode(tp, ip, logflags); |
| 5785 | if (cur) { |
| 5786 | if (!error) { |
| 5787 | *firstblock = cur->bc_private.b.firstblock; |
| 5788 | cur->bc_private.b.allocated = 0; |
| 5789 | } |
| 5790 | xfs_btree_del_cursor(cur, |
| 5791 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5792 | } |
| 5793 | return error; |
| 5794 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5795 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5796 | /* Unmap a range of a file. */ |
| 5797 | int |
| 5798 | xfs_bunmapi( |
| 5799 | xfs_trans_t *tp, |
| 5800 | struct xfs_inode *ip, |
| 5801 | xfs_fileoff_t bno, |
| 5802 | xfs_filblks_t len, |
| 5803 | int flags, |
| 5804 | xfs_extnum_t nexts, |
| 5805 | xfs_fsblock_t *firstblock, |
| 5806 | struct xfs_defer_ops *dfops, |
| 5807 | int *done) |
| 5808 | { |
| 5809 | int error; |
| 5810 | |
| 5811 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5812 | dfops); |
| 5813 | *done = (len == 0); |
| 5814 | return error; |
| 5815 | } |
| 5816 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5817 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5818 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5819 | * extent that precedes the target hole of the shift. |
| 5820 | */ |
| 5821 | STATIC bool |
| 5822 | xfs_bmse_can_merge( |
| 5823 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5824 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5825 | xfs_fileoff_t shift) /* shift fsb */ |
| 5826 | { |
| 5827 | xfs_fileoff_t startoff; |
| 5828 | |
| 5829 | startoff = got->br_startoff - shift; |
| 5830 | |
| 5831 | /* |
| 5832 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5833 | * the preceding extent. |
| 5834 | */ |
| 5835 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5836 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5837 | (left->br_state != got->br_state) || |
| 5838 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5839 | return false; |
| 5840 | |
| 5841 | return true; |
| 5842 | } |
| 5843 | |
| 5844 | /* |
| 5845 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5846 | * hole in the file. If an extent shift would result in the extent being fully |
| 5847 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5848 | * the preceding extent rather than do the shift. |
| 5849 | * |
| 5850 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5851 | * with the provided extents via xfs_bmse_can_merge(). |
| 5852 | */ |
| 5853 | STATIC int |
| 5854 | xfs_bmse_merge( |
| 5855 | struct xfs_inode *ip, |
| 5856 | int whichfork, |
| 5857 | xfs_fileoff_t shift, /* shift fsb */ |
| 5858 | int current_ext, /* idx of gotp */ |
| 5859 | struct xfs_bmbt_rec_host *gotp, /* extent to shift */ |
| 5860 | struct xfs_bmbt_rec_host *leftp, /* preceding extent */ |
| 5861 | struct xfs_btree_cur *cur, |
| 5862 | int *logflags) /* output */ |
| 5863 | { |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5864 | struct xfs_bmbt_irec got; |
| 5865 | struct xfs_bmbt_irec left; |
| 5866 | xfs_filblks_t blockcount; |
| 5867 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5868 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5869 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5870 | xfs_bmbt_get_all(gotp, &got); |
| 5871 | xfs_bmbt_get_all(leftp, &left); |
| 5872 | blockcount = left.br_blockcount + got.br_blockcount; |
| 5873 | |
| 5874 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5875 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 5876 | ASSERT(xfs_bmse_can_merge(&left, &got, shift)); |
| 5877 | |
| 5878 | /* |
| 5879 | * Merge the in-core extents. Note that the host record pointers and |
| 5880 | * current_ext index are invalid once the extent has been removed via |
| 5881 | * xfs_iext_remove(). |
| 5882 | */ |
| 5883 | xfs_bmbt_set_blockcount(leftp, blockcount); |
| 5884 | xfs_iext_remove(ip, current_ext, 1, 0); |
| 5885 | |
| 5886 | /* |
| 5887 | * Update the on-disk extent count, the btree if necessary and log the |
| 5888 | * inode. |
| 5889 | */ |
| 5890 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5891 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5892 | *logflags |= XFS_ILOG_CORE; |
| 5893 | if (!cur) { |
| 5894 | *logflags |= XFS_ILOG_DEXT; |
| 5895 | return 0; |
| 5896 | } |
| 5897 | |
| 5898 | /* lookup and remove the extent to merge */ |
| 5899 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5900 | got.br_blockcount, &i); |
| 5901 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5902 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5903 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5904 | |
| 5905 | error = xfs_btree_delete(cur, &i); |
| 5906 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5907 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5908 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5909 | |
| 5910 | /* lookup and update size of the previous extent */ |
| 5911 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock, |
| 5912 | left.br_blockcount, &i); |
| 5913 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5914 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5915 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5916 | |
| 5917 | left.br_blockcount = blockcount; |
| 5918 | |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5919 | return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock, |
| 5920 | left.br_blockcount, left.br_state); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5921 | } |
| 5922 | |
| 5923 | /* |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5924 | * Shift a single extent. |
| 5925 | */ |
| 5926 | STATIC int |
| 5927 | xfs_bmse_shift_one( |
| 5928 | struct xfs_inode *ip, |
| 5929 | int whichfork, |
| 5930 | xfs_fileoff_t offset_shift_fsb, |
| 5931 | int *current_ext, |
| 5932 | struct xfs_bmbt_rec_host *gotp, |
| 5933 | struct xfs_btree_cur *cur, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5934 | int *logflags, |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5935 | enum shift_direction direction, |
| 5936 | struct xfs_defer_ops *dfops) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5937 | { |
| 5938 | struct xfs_ifork *ifp; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5939 | struct xfs_mount *mp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5940 | xfs_fileoff_t startoff; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5941 | struct xfs_bmbt_rec_host *adj_irecp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5942 | struct xfs_bmbt_irec got; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5943 | struct xfs_bmbt_irec adj_irec; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5944 | int error; |
| 5945 | int i; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5946 | int total_extents; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5947 | |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5948 | mp = ip->i_mount; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5949 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5950 | total_extents = xfs_iext_count(ifp); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5951 | |
| 5952 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5953 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5954 | /* delalloc extents should be prevented by caller */ |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5955 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5956 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5957 | if (direction == SHIFT_LEFT) { |
| 5958 | startoff = got.br_startoff - offset_shift_fsb; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5959 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5960 | /* |
| 5961 | * Check for merge if we've got an extent to the left, |
| 5962 | * otherwise make sure there's enough room at the start |
| 5963 | * of the file for the shift. |
| 5964 | */ |
| 5965 | if (!*current_ext) { |
| 5966 | if (got.br_startoff < offset_shift_fsb) |
| 5967 | return -EINVAL; |
| 5968 | goto update_current_ext; |
| 5969 | } |
| 5970 | /* |
| 5971 | * grab the left extent and check for a large |
| 5972 | * enough hole. |
| 5973 | */ |
| 5974 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1); |
| 5975 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5976 | |
| 5977 | if (startoff < |
| 5978 | adj_irec.br_startoff + adj_irec.br_blockcount) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5979 | return -EINVAL; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5980 | |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5981 | /* check whether to merge the extent or shift it down */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5982 | if (xfs_bmse_can_merge(&adj_irec, &got, |
| 5983 | offset_shift_fsb)) { |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5984 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
| 5985 | *current_ext, gotp, adj_irecp, |
| 5986 | cur, logflags); |
| 5987 | if (error) |
| 5988 | return error; |
| 5989 | adj_irec = got; |
| 5990 | goto update_rmap; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5991 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5992 | } else { |
| 5993 | startoff = got.br_startoff + offset_shift_fsb; |
| 5994 | /* nothing to move if this is the last extent */ |
| 5995 | if (*current_ext >= (total_extents - 1)) |
| 5996 | goto update_current_ext; |
| 5997 | /* |
| 5998 | * If this is not the last extent in the file, make sure there |
| 5999 | * is enough room between current extent and next extent for |
| 6000 | * accommodating the shift. |
| 6001 | */ |
| 6002 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1); |
| 6003 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 6004 | if (startoff + got.br_blockcount > adj_irec.br_startoff) |
| 6005 | return -EINVAL; |
| 6006 | /* |
| 6007 | * Unlike a left shift (which involves a hole punch), |
| 6008 | * a right shift does not modify extent neighbors |
| 6009 | * in any way. We should never find mergeable extents |
| 6010 | * in this scenario. Check anyways and warn if we |
| 6011 | * encounter two extents that could be one. |
| 6012 | */ |
| 6013 | if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb)) |
| 6014 | WARN_ON_ONCE(1); |
| 6015 | } |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6016 | /* |
| 6017 | * Increment the extent index for the next iteration, update the start |
| 6018 | * offset of the in-core extent and update the btree if applicable. |
| 6019 | */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6020 | update_current_ext: |
| 6021 | if (direction == SHIFT_LEFT) |
| 6022 | (*current_ext)++; |
| 6023 | else |
| 6024 | (*current_ext)--; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6025 | xfs_bmbt_set_startoff(gotp, startoff); |
| 6026 | *logflags |= XFS_ILOG_CORE; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6027 | adj_irec = got; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6028 | if (!cur) { |
| 6029 | *logflags |= XFS_ILOG_DEXT; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6030 | goto update_rmap; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6031 | } |
| 6032 | |
| 6033 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 6034 | got.br_blockcount, &i); |
| 6035 | if (error) |
| 6036 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 6037 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6038 | |
| 6039 | got.br_startoff = startoff; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6040 | error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock, |
| 6041 | got.br_blockcount, got.br_state); |
| 6042 | if (error) |
| 6043 | return error; |
| 6044 | |
| 6045 | update_rmap: |
| 6046 | /* update reverse mapping */ |
| 6047 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec); |
| 6048 | if (error) |
| 6049 | return error; |
| 6050 | adj_irec.br_startoff = startoff; |
| 6051 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6052 | } |
| 6053 | |
| 6054 | /* |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6055 | * Shift extent records to the left/right to cover/create a hole. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6056 | * |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6057 | * 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] | 6058 | * @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] | 6059 | * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb |
| 6060 | * is the length by which each extent is shifted. If there is no hole to shift |
| 6061 | * the extents into, this will be considered invalid operation and we abort |
| 6062 | * immediately. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6063 | */ |
| 6064 | int |
| 6065 | xfs_bmap_shift_extents( |
| 6066 | struct xfs_trans *tp, |
| 6067 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6068 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6069 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6070 | int *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6071 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6072 | xfs_fsblock_t *firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6073 | struct xfs_defer_ops *dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6074 | enum shift_direction direction, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6075 | int num_exts) |
| 6076 | { |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6077 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6078 | struct xfs_bmbt_rec_host *gotp; |
| 6079 | struct xfs_bmbt_irec got; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6080 | struct xfs_mount *mp = ip->i_mount; |
| 6081 | struct xfs_ifork *ifp; |
| 6082 | xfs_extnum_t nexts = 0; |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6083 | xfs_extnum_t current_ext; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6084 | xfs_extnum_t total_extents; |
| 6085 | xfs_extnum_t stop_extent; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6086 | int error = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6087 | int whichfork = XFS_DATA_FORK; |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6088 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6089 | |
| 6090 | if (unlikely(XFS_TEST_ERROR( |
| 6091 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 6092 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 6093 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 6094 | XFS_ERROR_REPORT("xfs_bmap_shift_extents", |
| 6095 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 6096 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6097 | } |
| 6098 | |
| 6099 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 6100 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6101 | |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6102 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 6103 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6104 | ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT); |
| 6105 | ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6106 | |
| 6107 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6108 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6109 | /* Read in all the extents */ |
| 6110 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6111 | if (error) |
| 6112 | return error; |
| 6113 | } |
| 6114 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6115 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6116 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 6117 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6118 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6119 | cur->bc_private.b.flags = 0; |
| 6120 | } |
| 6121 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6122 | /* |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 6123 | * 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] | 6124 | * are collapsing out, so we cannot use the count of real extents here. |
| 6125 | * Instead we have to calculate it from the incore fork. |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 6126 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 6127 | total_extents = xfs_iext_count(ifp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6128 | if (total_extents == 0) { |
| 6129 | *done = 1; |
| 6130 | goto del_cursor; |
| 6131 | } |
| 6132 | |
| 6133 | /* |
| 6134 | * In case of first right shift, we need to initialize next_fsb |
| 6135 | */ |
| 6136 | if (*next_fsb == NULLFSBLOCK) { |
| 6137 | gotp = xfs_iext_get_ext(ifp, total_extents - 1); |
| 6138 | xfs_bmbt_get_all(gotp, &got); |
| 6139 | *next_fsb = got.br_startoff; |
| 6140 | if (stop_fsb > *next_fsb) { |
| 6141 | *done = 1; |
| 6142 | goto del_cursor; |
| 6143 | } |
| 6144 | } |
| 6145 | |
| 6146 | /* Lookup the extent index at which we have to stop */ |
| 6147 | if (direction == SHIFT_RIGHT) { |
| 6148 | gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent); |
| 6149 | /* Make stop_extent exclusive of shift range */ |
| 6150 | stop_extent--; |
| 6151 | } else |
| 6152 | stop_extent = total_extents; |
| 6153 | |
| 6154 | /* |
| 6155 | * Look up the extent index for the fsb where we start shifting. We can |
| 6156 | * henceforth iterate with current_ext as extent list changes are locked |
| 6157 | * out via ilock. |
| 6158 | * |
| 6159 | * gotp can be null in 2 cases: 1) if there are no extents or 2) |
| 6160 | * *next_fsb lies in a hole beyond which there are no extents. Either |
| 6161 | * way, we are done. |
| 6162 | */ |
| 6163 | gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext); |
| 6164 | if (!gotp) { |
| 6165 | *done = 1; |
| 6166 | goto del_cursor; |
| 6167 | } |
| 6168 | |
| 6169 | /* some sanity checking before we finally start shifting extents */ |
| 6170 | if ((direction == SHIFT_LEFT && current_ext >= stop_extent) || |
| 6171 | (direction == SHIFT_RIGHT && current_ext <= stop_extent)) { |
| 6172 | error = -EIO; |
| 6173 | goto del_cursor; |
| 6174 | } |
| 6175 | |
| 6176 | while (nexts++ < num_exts) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6177 | error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6178 | ¤t_ext, gotp, cur, &logflags, |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6179 | direction, dfops); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6180 | if (error) |
| 6181 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6182 | /* |
| 6183 | * If there was an extent merge during the shift, the extent |
| 6184 | * count can change. Update the total and grade the next record. |
| 6185 | */ |
| 6186 | if (direction == SHIFT_LEFT) { |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 6187 | total_extents = xfs_iext_count(ifp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6188 | stop_extent = total_extents; |
| 6189 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6190 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6191 | if (current_ext == stop_extent) { |
| 6192 | *done = 1; |
| 6193 | *next_fsb = NULLFSBLOCK; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6194 | break; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6195 | } |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6196 | gotp = xfs_iext_get_ext(ifp, current_ext); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6197 | } |
| 6198 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6199 | if (!*done) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6200 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6201 | *next_fsb = got.br_startoff; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6202 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6203 | |
| 6204 | del_cursor: |
| 6205 | if (cur) |
| 6206 | xfs_btree_del_cursor(cur, |
| 6207 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6208 | |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6209 | if (logflags) |
| 6210 | xfs_trans_log_inode(tp, ip, logflags); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6211 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6212 | return error; |
| 6213 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6214 | |
| 6215 | /* |
| 6216 | * Splits an extent into two extents at split_fsb block such that it is |
| 6217 | * the first block of the current_ext. @current_ext is a target extent |
| 6218 | * to be split. @split_fsb is a block where the extents is split. |
| 6219 | * If split_fsb lies in a hole or the first block of extents, just return 0. |
| 6220 | */ |
| 6221 | STATIC int |
| 6222 | xfs_bmap_split_extent_at( |
| 6223 | struct xfs_trans *tp, |
| 6224 | struct xfs_inode *ip, |
| 6225 | xfs_fileoff_t split_fsb, |
| 6226 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6227 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6228 | { |
| 6229 | int whichfork = XFS_DATA_FORK; |
| 6230 | struct xfs_btree_cur *cur = NULL; |
| 6231 | struct xfs_bmbt_rec_host *gotp; |
| 6232 | struct xfs_bmbt_irec got; |
| 6233 | struct xfs_bmbt_irec new; /* split extent */ |
| 6234 | struct xfs_mount *mp = ip->i_mount; |
| 6235 | struct xfs_ifork *ifp; |
| 6236 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
| 6237 | xfs_extnum_t current_ext; |
| 6238 | int error = 0; |
| 6239 | int logflags = 0; |
| 6240 | int i = 0; |
| 6241 | |
| 6242 | if (unlikely(XFS_TEST_ERROR( |
| 6243 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 6244 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 6245 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 6246 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 6247 | XFS_ERRLEVEL_LOW, mp); |
| 6248 | return -EFSCORRUPTED; |
| 6249 | } |
| 6250 | |
| 6251 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 6252 | return -EIO; |
| 6253 | |
| 6254 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6255 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6256 | /* Read in all the extents */ |
| 6257 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6258 | if (error) |
| 6259 | return error; |
| 6260 | } |
| 6261 | |
| 6262 | /* |
| 6263 | * gotp can be null in 2 cases: 1) if there are no extents |
| 6264 | * or 2) split_fsb lies in a hole beyond which there are |
| 6265 | * no extents. Either way, we are done. |
| 6266 | */ |
| 6267 | gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext); |
| 6268 | if (!gotp) |
| 6269 | return 0; |
| 6270 | |
| 6271 | xfs_bmbt_get_all(gotp, &got); |
| 6272 | |
| 6273 | /* |
| 6274 | * Check split_fsb lies in a hole or the start boundary offset |
| 6275 | * of the extent. |
| 6276 | */ |
| 6277 | if (got.br_startoff >= split_fsb) |
| 6278 | return 0; |
| 6279 | |
| 6280 | gotblkcnt = split_fsb - got.br_startoff; |
| 6281 | new.br_startoff = split_fsb; |
| 6282 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 6283 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 6284 | new.br_state = got.br_state; |
| 6285 | |
| 6286 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6287 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 6288 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6289 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6290 | cur->bc_private.b.flags = 0; |
| 6291 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 6292 | got.br_startblock, |
| 6293 | got.br_blockcount, |
| 6294 | &i); |
| 6295 | if (error) |
| 6296 | goto del_cursor; |
| 6297 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6298 | } |
| 6299 | |
| 6300 | xfs_bmbt_set_blockcount(gotp, gotblkcnt); |
| 6301 | got.br_blockcount = gotblkcnt; |
| 6302 | |
| 6303 | logflags = XFS_ILOG_CORE; |
| 6304 | if (cur) { |
| 6305 | error = xfs_bmbt_update(cur, got.br_startoff, |
| 6306 | got.br_startblock, |
| 6307 | got.br_blockcount, |
| 6308 | got.br_state); |
| 6309 | if (error) |
| 6310 | goto del_cursor; |
| 6311 | } else |
| 6312 | logflags |= XFS_ILOG_DEXT; |
| 6313 | |
| 6314 | /* Add new extent */ |
| 6315 | current_ext++; |
| 6316 | xfs_iext_insert(ip, current_ext, 1, &new, 0); |
| 6317 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 6318 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 6319 | |
| 6320 | if (cur) { |
| 6321 | error = xfs_bmbt_lookup_eq(cur, new.br_startoff, |
| 6322 | new.br_startblock, new.br_blockcount, |
| 6323 | &i); |
| 6324 | if (error) |
| 6325 | goto del_cursor; |
| 6326 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
| 6327 | cur->bc_rec.b.br_state = new.br_state; |
| 6328 | |
| 6329 | error = xfs_btree_insert(cur, &i); |
| 6330 | if (error) |
| 6331 | goto del_cursor; |
| 6332 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6333 | } |
| 6334 | |
| 6335 | /* |
| 6336 | * Convert to a btree if necessary. |
| 6337 | */ |
| 6338 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 6339 | int tmp_logflags; /* partial log flag return val */ |
| 6340 | |
| 6341 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6342 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6343 | &cur, 0, &tmp_logflags, whichfork); |
| 6344 | logflags |= tmp_logflags; |
| 6345 | } |
| 6346 | |
| 6347 | del_cursor: |
| 6348 | if (cur) { |
| 6349 | cur->bc_private.b.allocated = 0; |
| 6350 | xfs_btree_del_cursor(cur, |
| 6351 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6352 | } |
| 6353 | |
| 6354 | if (logflags) |
| 6355 | xfs_trans_log_inode(tp, ip, logflags); |
| 6356 | return error; |
| 6357 | } |
| 6358 | |
| 6359 | int |
| 6360 | xfs_bmap_split_extent( |
| 6361 | struct xfs_inode *ip, |
| 6362 | xfs_fileoff_t split_fsb) |
| 6363 | { |
| 6364 | struct xfs_mount *mp = ip->i_mount; |
| 6365 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6366 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6367 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6368 | int error; |
| 6369 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6370 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6371 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6372 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6373 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6374 | |
| 6375 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6376 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6377 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6378 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6379 | |
| 6380 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6381 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6382 | if (error) |
| 6383 | goto out; |
| 6384 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6385 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6386 | if (error) |
| 6387 | goto out; |
| 6388 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6389 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6390 | |
| 6391 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6392 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6393 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6394 | return error; |
| 6395 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6396 | |
| 6397 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6398 | static bool |
| 6399 | xfs_bmap_is_update_needed( |
| 6400 | struct xfs_bmbt_irec *bmap) |
| 6401 | { |
| 6402 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6403 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6404 | } |
| 6405 | |
| 6406 | /* Record a bmap intent. */ |
| 6407 | static int |
| 6408 | __xfs_bmap_add( |
| 6409 | struct xfs_mount *mp, |
| 6410 | struct xfs_defer_ops *dfops, |
| 6411 | enum xfs_bmap_intent_type type, |
| 6412 | struct xfs_inode *ip, |
| 6413 | int whichfork, |
| 6414 | struct xfs_bmbt_irec *bmap) |
| 6415 | { |
| 6416 | int error; |
| 6417 | struct xfs_bmap_intent *bi; |
| 6418 | |
| 6419 | trace_xfs_bmap_defer(mp, |
| 6420 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6421 | type, |
| 6422 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6423 | ip->i_ino, whichfork, |
| 6424 | bmap->br_startoff, |
| 6425 | bmap->br_blockcount, |
| 6426 | bmap->br_state); |
| 6427 | |
| 6428 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6429 | INIT_LIST_HEAD(&bi->bi_list); |
| 6430 | bi->bi_type = type; |
| 6431 | bi->bi_owner = ip; |
| 6432 | bi->bi_whichfork = whichfork; |
| 6433 | bi->bi_bmap = *bmap; |
| 6434 | |
| 6435 | error = xfs_defer_join(dfops, bi->bi_owner); |
| 6436 | if (error) { |
| 6437 | kmem_free(bi); |
| 6438 | return error; |
| 6439 | } |
| 6440 | |
| 6441 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6442 | return 0; |
| 6443 | } |
| 6444 | |
| 6445 | /* Map an extent into a file. */ |
| 6446 | int |
| 6447 | xfs_bmap_map_extent( |
| 6448 | struct xfs_mount *mp, |
| 6449 | struct xfs_defer_ops *dfops, |
| 6450 | struct xfs_inode *ip, |
| 6451 | struct xfs_bmbt_irec *PREV) |
| 6452 | { |
| 6453 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6454 | return 0; |
| 6455 | |
| 6456 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6457 | XFS_DATA_FORK, PREV); |
| 6458 | } |
| 6459 | |
| 6460 | /* Unmap an extent out of a file. */ |
| 6461 | int |
| 6462 | xfs_bmap_unmap_extent( |
| 6463 | struct xfs_mount *mp, |
| 6464 | struct xfs_defer_ops *dfops, |
| 6465 | struct xfs_inode *ip, |
| 6466 | struct xfs_bmbt_irec *PREV) |
| 6467 | { |
| 6468 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6469 | return 0; |
| 6470 | |
| 6471 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6472 | XFS_DATA_FORK, PREV); |
| 6473 | } |
| 6474 | |
| 6475 | /* |
| 6476 | * Process one of the deferred bmap operations. We pass back the |
| 6477 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6478 | */ |
| 6479 | int |
| 6480 | xfs_bmap_finish_one( |
| 6481 | struct xfs_trans *tp, |
| 6482 | struct xfs_defer_ops *dfops, |
| 6483 | struct xfs_inode *ip, |
| 6484 | enum xfs_bmap_intent_type type, |
| 6485 | int whichfork, |
| 6486 | xfs_fileoff_t startoff, |
| 6487 | xfs_fsblock_t startblock, |
| 6488 | xfs_filblks_t blockcount, |
| 6489 | xfs_exntst_t state) |
| 6490 | { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6491 | int error = 0, done; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6492 | |
| 6493 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6494 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6495 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
| 6496 | ip->i_ino, whichfork, startoff, blockcount, state); |
| 6497 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6498 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6499 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6500 | |
| 6501 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
| 6502 | XFS_ERRTAG_BMAP_FINISH_ONE, |
| 6503 | XFS_RANDOM_BMAP_FINISH_ONE)) |
| 6504 | return -EIO; |
| 6505 | |
| 6506 | switch (type) { |
| 6507 | case XFS_BMAP_MAP: |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6508 | error = xfs_bmapi_remap(tp, ip, startoff, blockcount, |
| 6509 | startblock, dfops); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6510 | break; |
| 6511 | case XFS_BMAP_UNMAP: |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6512 | error = xfs_bunmapi(tp, ip, startoff, blockcount, |
| 6513 | XFS_BMAPI_REMAP, 1, &startblock, dfops, &done); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6514 | ASSERT(done); |
| 6515 | break; |
| 6516 | default: |
| 6517 | ASSERT(0); |
| 6518 | error = -EFSCORRUPTED; |
| 6519 | } |
| 6520 | |
| 6521 | return error; |
| 6522 | } |