Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
| 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_sb.h" |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 27 | #include "xfs_defer.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 28 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_da_btree.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 30 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_btree.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 33 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_extfree_item.h" |
| 36 | #include "xfs_alloc.h" |
| 37 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 38 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 39 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_rtalloc.h" |
| 41 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_quota.h" |
| 43 | #include "xfs_trans_space.h" |
| 44 | #include "xfs_buf_item.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 45 | #include "xfs_trace.h" |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 46 | #include "xfs_symlink.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 47 | #include "xfs_attr_leaf.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 48 | #include "xfs_filestream.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 49 | #include "xfs_rmap.h" |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 50 | #include "xfs_ag_resv.h" |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 51 | #include "xfs_refcount.h" |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 52 | #include "xfs_rmap_btree.h" |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 53 | #include "xfs_icache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 57 | |
| 58 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 59 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 63 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 64 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 66 | void |
| 67 | xfs_bmap_compute_maxlevels( |
| 68 | xfs_mount_t *mp, /* file system mount structure */ |
| 69 | int whichfork) /* data or attr fork */ |
| 70 | { |
| 71 | int level; /* btree level */ |
| 72 | uint maxblocks; /* max blocks at this level */ |
| 73 | uint maxleafents; /* max leaf entries possible */ |
| 74 | int maxrootrecs; /* max records in root block */ |
| 75 | int minleafrecs; /* min records in leaf block */ |
| 76 | int minnoderecs; /* min records in node block */ |
| 77 | int sz; /* root block size */ |
| 78 | |
| 79 | /* |
| 80 | * The maximum number of extents in a file, hence the maximum |
| 81 | * number of leaf entries, is controlled by the type of di_nextents |
| 82 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 83 | * (a signed 16-bit number, xfs_aextnum_t). |
| 84 | * |
| 85 | * Note that we can no longer assume that if we are in ATTR1 that |
| 86 | * the fork offset of all the inodes will be |
| 87 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 88 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 89 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 90 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 91 | * of a minimum size available. |
| 92 | */ |
| 93 | if (whichfork == XFS_DATA_FORK) { |
| 94 | maxleafents = MAXEXTNUM; |
| 95 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 96 | } else { |
| 97 | maxleafents = MAXAEXTNUM; |
| 98 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 99 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 100 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 101 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 102 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 103 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 104 | for (level = 1; maxblocks > 1; level++) { |
| 105 | if (maxblocks <= maxrootrecs) |
| 106 | maxblocks = 1; |
| 107 | else |
| 108 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 109 | } |
| 110 | mp->m_bm_maxlevels[whichfork] = level; |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 113 | STATIC int /* error */ |
| 114 | xfs_bmbt_lookup_eq( |
| 115 | struct xfs_btree_cur *cur, |
| 116 | xfs_fileoff_t off, |
| 117 | xfs_fsblock_t bno, |
| 118 | xfs_filblks_t len, |
| 119 | int *stat) /* success/failure */ |
| 120 | { |
| 121 | cur->bc_rec.b.br_startoff = off; |
| 122 | cur->bc_rec.b.br_startblock = bno; |
| 123 | cur->bc_rec.b.br_blockcount = len; |
| 124 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 125 | } |
| 126 | |
| 127 | STATIC int /* error */ |
| 128 | xfs_bmbt_lookup_ge( |
| 129 | struct xfs_btree_cur *cur, |
| 130 | xfs_fileoff_t off, |
| 131 | xfs_fsblock_t bno, |
| 132 | xfs_filblks_t len, |
| 133 | int *stat) /* success/failure */ |
| 134 | { |
| 135 | cur->bc_rec.b.br_startoff = off; |
| 136 | cur->bc_rec.b.br_startblock = bno; |
| 137 | cur->bc_rec.b.br_blockcount = len; |
| 138 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 139 | } |
| 140 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 141 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 142 | * Check if the inode needs to be converted to btree format. |
| 143 | */ |
| 144 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 145 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 146 | return whichfork != XFS_COW_FORK && |
| 147 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 148 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 149 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Check if the inode should be converted to extent format. |
| 154 | */ |
| 155 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 156 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 157 | return whichfork != XFS_COW_FORK && |
| 158 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 159 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 160 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Update the record referred to by cur to the value given |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 165 | * by [off, bno, len, state]. |
| 166 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 167 | */ |
| 168 | STATIC int |
| 169 | xfs_bmbt_update( |
| 170 | struct xfs_btree_cur *cur, |
| 171 | xfs_fileoff_t off, |
| 172 | xfs_fsblock_t bno, |
| 173 | xfs_filblks_t len, |
| 174 | xfs_exntst_t state) |
| 175 | { |
| 176 | union xfs_btree_rec rec; |
| 177 | |
| 178 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 179 | return xfs_btree_update(cur, &rec); |
| 180 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 183 | * Compute the worst-case number of indirect blocks that will be used |
| 184 | * for ip's delayed extent of length "len". |
| 185 | */ |
| 186 | STATIC xfs_filblks_t |
| 187 | xfs_bmap_worst_indlen( |
| 188 | xfs_inode_t *ip, /* incore inode pointer */ |
| 189 | xfs_filblks_t len) /* delayed extent length */ |
| 190 | { |
| 191 | int level; /* btree level number */ |
| 192 | int maxrecs; /* maximum record count at this level */ |
| 193 | xfs_mount_t *mp; /* mount structure */ |
| 194 | xfs_filblks_t rval; /* return value */ |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 195 | xfs_filblks_t orig_len; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 196 | |
| 197 | mp = ip->i_mount; |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 198 | |
| 199 | /* Calculate the worst-case size of the bmbt. */ |
| 200 | orig_len = len; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 201 | maxrecs = mp->m_bmap_dmxr[0]; |
| 202 | for (level = 0, rval = 0; |
| 203 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 204 | level++) { |
| 205 | len += maxrecs - 1; |
| 206 | do_div(len, maxrecs); |
| 207 | rval += len; |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 208 | if (len == 1) { |
| 209 | rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 210 | level - 1; |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 211 | break; |
| 212 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 213 | if (level == 0) |
| 214 | maxrecs = mp->m_bmap_dmxr[1]; |
| 215 | } |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 216 | |
| 217 | /* Calculate the worst-case size of the rmapbt. */ |
| 218 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 219 | rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) + |
| 220 | mp->m_rmap_maxlevels; |
| 221 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 222 | return rval; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Calculate the default attribute fork offset for newly created inodes. |
| 227 | */ |
| 228 | uint |
| 229 | xfs_default_attroffset( |
| 230 | struct xfs_inode *ip) |
| 231 | { |
| 232 | struct xfs_mount *mp = ip->i_mount; |
| 233 | uint offset; |
| 234 | |
| 235 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 236 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 237 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 238 | } else { |
| 239 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 240 | } |
| 241 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 242 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 243 | return offset; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Helper routine to reset inode di_forkoff field when switching |
| 248 | * attribute fork from local to extent format - we reset it where |
| 249 | * possible to make space available for inline data fork extents. |
| 250 | */ |
| 251 | STATIC void |
| 252 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 253 | xfs_inode_t *ip, |
| 254 | int whichfork) |
| 255 | { |
| 256 | if (whichfork == XFS_ATTR_FORK && |
| 257 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 258 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 259 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 260 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 261 | |
| 262 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 263 | ip->i_d.di_forkoff = dfl_forkoff; |
| 264 | } |
| 265 | } |
| 266 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 267 | #ifdef DEBUG |
| 268 | STATIC struct xfs_buf * |
| 269 | xfs_bmap_get_bp( |
| 270 | struct xfs_btree_cur *cur, |
| 271 | xfs_fsblock_t bno) |
| 272 | { |
| 273 | struct xfs_log_item_desc *lidp; |
| 274 | int i; |
| 275 | |
| 276 | if (!cur) |
| 277 | return NULL; |
| 278 | |
| 279 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 280 | if (!cur->bc_bufs[i]) |
| 281 | break; |
| 282 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 283 | return cur->bc_bufs[i]; |
| 284 | } |
| 285 | |
| 286 | /* Chase down all the log items to see if the bp is there */ |
| 287 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 288 | struct xfs_buf_log_item *bip; |
| 289 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 290 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 291 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 292 | return bip->bli_buf; |
| 293 | } |
| 294 | |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | STATIC void |
| 299 | xfs_check_block( |
| 300 | struct xfs_btree_block *block, |
| 301 | xfs_mount_t *mp, |
| 302 | int root, |
| 303 | short sz) |
| 304 | { |
| 305 | int i, j, dmxr; |
| 306 | __be64 *pp, *thispa; /* pointer to block address */ |
| 307 | xfs_bmbt_key_t *prevp, *keyp; |
| 308 | |
| 309 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 310 | |
| 311 | prevp = NULL; |
| 312 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 313 | dmxr = mp->m_bmap_dmxr[0]; |
| 314 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 315 | |
| 316 | if (prevp) { |
| 317 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 318 | be64_to_cpu(keyp->br_startoff)); |
| 319 | } |
| 320 | prevp = keyp; |
| 321 | |
| 322 | /* |
| 323 | * Compare the block numbers to see if there are dups. |
| 324 | */ |
| 325 | if (root) |
| 326 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 327 | else |
| 328 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 329 | |
| 330 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 331 | if (root) |
| 332 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 333 | else |
| 334 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 335 | if (*thispa == *pp) { |
| 336 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 337 | __func__, j, i, |
| 338 | (unsigned long long)be64_to_cpu(*thispa)); |
| 339 | panic("%s: ptrs are equal in node\n", |
| 340 | __func__); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Check that the extents for the inode ip are in the right order in all |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 348 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 349 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 350 | * them. The btree record ordering checks will still be done, so for such large |
| 351 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 352 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 353 | STATIC void |
| 354 | xfs_bmap_check_leaf_extents( |
| 355 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 356 | xfs_inode_t *ip, /* incore inode pointer */ |
| 357 | int whichfork) /* data or attr fork */ |
| 358 | { |
| 359 | struct xfs_btree_block *block; /* current btree block */ |
| 360 | xfs_fsblock_t bno; /* block # of "block" */ |
| 361 | xfs_buf_t *bp; /* buffer for "block" */ |
| 362 | int error; /* error return value */ |
| 363 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 364 | xfs_ifork_t *ifp; /* fork structure */ |
| 365 | int level; /* btree level, for checking */ |
| 366 | xfs_mount_t *mp; /* file system mount structure */ |
| 367 | __be64 *pp; /* pointer to block address */ |
| 368 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 369 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 370 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 371 | int bp_release = 0; |
| 372 | |
| 373 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 374 | return; |
| 375 | } |
| 376 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 377 | /* skip large extent count inodes */ |
| 378 | if (ip->i_d.di_nextents > 10000) |
| 379 | return; |
| 380 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 381 | bno = NULLFSBLOCK; |
| 382 | mp = ip->i_mount; |
| 383 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 384 | block = ifp->if_broot; |
| 385 | /* |
| 386 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 387 | */ |
| 388 | level = be16_to_cpu(block->bb_level); |
| 389 | ASSERT(level > 0); |
| 390 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 391 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 392 | bno = be64_to_cpu(*pp); |
| 393 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 394 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 395 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 396 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 397 | |
| 398 | /* |
| 399 | * Go down the tree until leaf level is reached, following the first |
| 400 | * pointer (leftmost) at each level. |
| 401 | */ |
| 402 | while (level-- > 0) { |
| 403 | /* See if buf is in cur first */ |
| 404 | bp_release = 0; |
| 405 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 406 | if (!bp) { |
| 407 | bp_release = 1; |
| 408 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 409 | XFS_BMAP_BTREE_REF, |
| 410 | &xfs_bmbt_buf_ops); |
| 411 | if (error) |
| 412 | goto error_norelse; |
| 413 | } |
| 414 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 415 | if (level == 0) |
| 416 | break; |
| 417 | |
| 418 | /* |
| 419 | * Check this block for basic sanity (increasing keys and |
| 420 | * no duplicate blocks). |
| 421 | */ |
| 422 | |
| 423 | xfs_check_block(block, mp, 0, 0); |
| 424 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 425 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 426 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 427 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 428 | if (bp_release) { |
| 429 | bp_release = 0; |
| 430 | xfs_trans_brelse(NULL, bp); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 436 | */ |
| 437 | i = 0; |
| 438 | |
| 439 | /* |
| 440 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 441 | */ |
| 442 | for (;;) { |
| 443 | xfs_fsblock_t nextbno; |
| 444 | xfs_extnum_t num_recs; |
| 445 | |
| 446 | |
| 447 | num_recs = xfs_btree_get_numrecs(block); |
| 448 | |
| 449 | /* |
| 450 | * Read-ahead the next leaf block, if any. |
| 451 | */ |
| 452 | |
| 453 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 454 | |
| 455 | /* |
| 456 | * Check all the extents to make sure they are OK. |
| 457 | * If we had a previous block, the last entry should |
| 458 | * conform with the first entry in this one. |
| 459 | */ |
| 460 | |
| 461 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 462 | if (i) { |
| 463 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 464 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 465 | xfs_bmbt_disk_get_startoff(ep)); |
| 466 | } |
| 467 | for (j = 1; j < num_recs; j++) { |
| 468 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 469 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 470 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 471 | xfs_bmbt_disk_get_startoff(nextp)); |
| 472 | ep = nextp; |
| 473 | } |
| 474 | |
| 475 | last = *ep; |
| 476 | i += num_recs; |
| 477 | if (bp_release) { |
| 478 | bp_release = 0; |
| 479 | xfs_trans_brelse(NULL, bp); |
| 480 | } |
| 481 | bno = nextbno; |
| 482 | /* |
| 483 | * If we've reached the end, stop. |
| 484 | */ |
| 485 | if (bno == NULLFSBLOCK) |
| 486 | break; |
| 487 | |
| 488 | bp_release = 0; |
| 489 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 490 | if (!bp) { |
| 491 | bp_release = 1; |
| 492 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 493 | XFS_BMAP_BTREE_REF, |
| 494 | &xfs_bmbt_buf_ops); |
| 495 | if (error) |
| 496 | goto error_norelse; |
| 497 | } |
| 498 | block = XFS_BUF_TO_BLOCK(bp); |
| 499 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 500 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 501 | return; |
| 502 | |
| 503 | error0: |
| 504 | xfs_warn(mp, "%s: at error0", __func__); |
| 505 | if (bp_release) |
| 506 | xfs_trans_brelse(NULL, bp); |
| 507 | error_norelse: |
| 508 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 509 | __func__, i); |
| 510 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 511 | return; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Add bmap trace insert entries for all the contents of the extent records. |
| 516 | */ |
| 517 | void |
| 518 | xfs_bmap_trace_exlist( |
| 519 | xfs_inode_t *ip, /* incore inode pointer */ |
| 520 | xfs_extnum_t cnt, /* count of entries in the list */ |
Eric Sandeen | c44a1f2 | 2016-12-05 12:32:00 +1100 | [diff] [blame] | 521 | int whichfork, /* data or attr or cow fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 522 | unsigned long caller_ip) |
| 523 | { |
| 524 | xfs_extnum_t idx; /* extent record index */ |
| 525 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 526 | int state = 0; |
| 527 | |
| 528 | if (whichfork == XFS_ATTR_FORK) |
| 529 | state |= BMAP_ATTRFORK; |
Eric Sandeen | c44a1f2 | 2016-12-05 12:32:00 +1100 | [diff] [blame] | 530 | else if (whichfork == XFS_COW_FORK) |
| 531 | state |= BMAP_COWFORK; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 532 | |
| 533 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 534 | ASSERT(cnt == xfs_iext_count(ifp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 535 | for (idx = 0; idx < cnt; idx++) |
Eric Sandeen | 7710517 | 2016-12-05 12:31:50 +1100 | [diff] [blame] | 536 | trace_xfs_extlist(ip, idx, state, caller_ip); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 541 | * given the caller's original parameters. Specifically check the |
| 542 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 543 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 544 | */ |
| 545 | STATIC void |
| 546 | xfs_bmap_validate_ret( |
| 547 | xfs_fileoff_t bno, |
| 548 | xfs_filblks_t len, |
| 549 | int flags, |
| 550 | xfs_bmbt_irec_t *mval, |
| 551 | int nmap, |
| 552 | int ret_nmap) |
| 553 | { |
| 554 | int i; /* index to map values */ |
| 555 | |
| 556 | ASSERT(ret_nmap <= nmap); |
| 557 | |
| 558 | for (i = 0; i < ret_nmap; i++) { |
| 559 | ASSERT(mval[i].br_blockcount > 0); |
| 560 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 561 | ASSERT(mval[i].br_startoff >= bno); |
| 562 | ASSERT(mval[i].br_blockcount <= len); |
| 563 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 564 | bno + len); |
| 565 | } else { |
| 566 | ASSERT(mval[i].br_startoff < bno + len); |
| 567 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 568 | bno); |
| 569 | } |
| 570 | ASSERT(i == 0 || |
| 571 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 572 | mval[i].br_startoff); |
| 573 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 574 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 575 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 576 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | #else |
| 581 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
| 582 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 583 | #endif /* DEBUG */ |
| 584 | |
| 585 | /* |
| 586 | * bmap free list manipulation functions |
| 587 | */ |
| 588 | |
| 589 | /* |
| 590 | * Add the extent to the list of extents to be free at transaction end. |
| 591 | * The list is maintained sorted (by block number). |
| 592 | */ |
| 593 | void |
| 594 | xfs_bmap_add_free( |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 595 | struct xfs_mount *mp, |
| 596 | struct xfs_defer_ops *dfops, |
| 597 | xfs_fsblock_t bno, |
| 598 | xfs_filblks_t len, |
| 599 | struct xfs_owner_info *oinfo) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 600 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 601 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 602 | #ifdef DEBUG |
| 603 | xfs_agnumber_t agno; |
| 604 | xfs_agblock_t agbno; |
| 605 | |
| 606 | ASSERT(bno != NULLFSBLOCK); |
| 607 | ASSERT(len > 0); |
| 608 | ASSERT(len <= MAXEXTLEN); |
| 609 | ASSERT(!isnullstartblock(bno)); |
| 610 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 611 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 612 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 613 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 614 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 615 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 616 | #endif |
| 617 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 618 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 619 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 620 | new->xefi_startblock = bno; |
| 621 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 622 | if (oinfo) |
| 623 | new->xefi_oinfo = *oinfo; |
| 624 | else |
| 625 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 626 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
| 627 | XFS_FSB_TO_AGBNO(mp, bno), len); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 628 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Inode fork format manipulation functions |
| 633 | */ |
| 634 | |
| 635 | /* |
| 636 | * Transform a btree format file with only one leaf node, where the |
| 637 | * extents list will fit in the inode, into an extents format file. |
| 638 | * Since the file extents are already in-core, all we have to do is |
| 639 | * give up the space for the btree root and pitch the leaf block. |
| 640 | */ |
| 641 | STATIC int /* error */ |
| 642 | xfs_bmap_btree_to_extents( |
| 643 | xfs_trans_t *tp, /* transaction pointer */ |
| 644 | xfs_inode_t *ip, /* incore inode pointer */ |
| 645 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 646 | int *logflagsp, /* inode logging flags */ |
| 647 | int whichfork) /* data or attr fork */ |
| 648 | { |
| 649 | /* REFERENCED */ |
| 650 | struct xfs_btree_block *cblock;/* child btree block */ |
| 651 | xfs_fsblock_t cbno; /* child block number */ |
| 652 | xfs_buf_t *cbp; /* child block's buffer */ |
| 653 | int error; /* error return value */ |
| 654 | xfs_ifork_t *ifp; /* inode fork data */ |
| 655 | xfs_mount_t *mp; /* mount point structure */ |
| 656 | __be64 *pp; /* ptr to block address */ |
| 657 | struct xfs_btree_block *rblock;/* root btree block */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 658 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 659 | |
| 660 | mp = ip->i_mount; |
| 661 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 662 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 663 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 664 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 665 | rblock = ifp->if_broot; |
| 666 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 667 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 668 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 669 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 670 | cbno = be64_to_cpu(*pp); |
| 671 | *logflagsp = 0; |
| 672 | #ifdef DEBUG |
| 673 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
| 674 | return error; |
| 675 | #endif |
| 676 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 677 | &xfs_bmbt_buf_ops); |
| 678 | if (error) |
| 679 | return error; |
| 680 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 681 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 682 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 683 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
| 684 | xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 685 | ip->i_d.di_nblocks--; |
| 686 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 687 | xfs_trans_binval(tp, cbp); |
| 688 | if (cur->bc_bufs[0] == cbp) |
| 689 | cur->bc_bufs[0] = NULL; |
| 690 | xfs_iroot_realloc(ip, -1, whichfork); |
| 691 | ASSERT(ifp->if_broot == NULL); |
| 692 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 693 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 694 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Convert an extents-format file into a btree-format file. |
| 700 | * The new file will have a root block (in the inode) and a single child block. |
| 701 | */ |
| 702 | STATIC int /* error */ |
| 703 | xfs_bmap_extents_to_btree( |
| 704 | xfs_trans_t *tp, /* transaction pointer */ |
| 705 | xfs_inode_t *ip, /* incore inode pointer */ |
| 706 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 707 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 708 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 709 | int wasdel, /* converting a delayed alloc */ |
| 710 | int *logflagsp, /* inode logging flags */ |
| 711 | int whichfork) /* data or attr fork */ |
| 712 | { |
| 713 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 714 | xfs_buf_t *abp; /* buffer for ablock */ |
| 715 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 716 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 717 | struct xfs_btree_block *block; /* btree root block */ |
| 718 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 719 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 720 | int error; /* error return value */ |
| 721 | xfs_extnum_t i, cnt; /* extent record index */ |
| 722 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 723 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 724 | xfs_mount_t *mp; /* mount structure */ |
| 725 | xfs_extnum_t nextents; /* number of file extents */ |
| 726 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 727 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 728 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 729 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 730 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 731 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 732 | |
| 733 | /* |
| 734 | * Make space in the inode incore. |
| 735 | */ |
| 736 | xfs_iroot_realloc(ip, 1, whichfork); |
| 737 | ifp->if_flags |= XFS_IFBROOT; |
| 738 | |
| 739 | /* |
| 740 | * Fill in the root. |
| 741 | */ |
| 742 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 743 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 744 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 745 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 746 | /* |
| 747 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 748 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 749 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 750 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 751 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 752 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 753 | /* |
| 754 | * Convert to a btree with two levels, one record in root. |
| 755 | */ |
| 756 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 757 | memset(&args, 0, sizeof(args)); |
| 758 | args.tp = tp; |
| 759 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 760 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 761 | args.firstblock = *firstblock; |
| 762 | if (*firstblock == NULLFSBLOCK) { |
| 763 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 764 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 765 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 766 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 767 | args.fsbno = *firstblock; |
| 768 | } else { |
| 769 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 770 | args.fsbno = *firstblock; |
| 771 | } |
| 772 | args.minlen = args.maxlen = args.prod = 1; |
| 773 | args.wasdel = wasdel; |
| 774 | *logflagsp = 0; |
| 775 | if ((error = xfs_alloc_vextent(&args))) { |
| 776 | xfs_iroot_realloc(ip, -1, whichfork); |
| 777 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 778 | return error; |
| 779 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 780 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 781 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
| 782 | xfs_iroot_realloc(ip, -1, whichfork); |
| 783 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 784 | return -ENOSPC; |
| 785 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 786 | /* |
| 787 | * Allocation can't fail, the space was reserved. |
| 788 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 789 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 790 | args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 791 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 792 | cur->bc_private.b.allocated++; |
| 793 | ip->i_d.di_nblocks++; |
| 794 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 795 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 796 | /* |
| 797 | * Fill in the child block. |
| 798 | */ |
| 799 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 800 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 801 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 802 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 803 | XFS_BTREE_LONG_PTRS); |
| 804 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 805 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 806 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 807 | for (cnt = i = 0; i < nextents; i++) { |
| 808 | ep = xfs_iext_get_ext(ifp, i); |
| 809 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
| 810 | arp->l0 = cpu_to_be64(ep->l0); |
| 811 | arp->l1 = cpu_to_be64(ep->l1); |
| 812 | arp++; cnt++; |
| 813 | } |
| 814 | } |
| 815 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 816 | xfs_btree_set_numrecs(ablock, cnt); |
| 817 | |
| 818 | /* |
| 819 | * Fill in the root key and pointer. |
| 820 | */ |
| 821 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 822 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 823 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 824 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 825 | be16_to_cpu(block->bb_level))); |
| 826 | *pp = cpu_to_be64(args.fsbno); |
| 827 | |
| 828 | /* |
| 829 | * Do all this logging at the end so that |
| 830 | * the root is at the right level. |
| 831 | */ |
| 832 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 833 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 834 | ASSERT(*curp == NULL); |
| 835 | *curp = cur; |
| 836 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Convert a local file to an extents file. |
| 842 | * This code is out of bounds for data forks of regular files, |
| 843 | * since the file data needs to get logged so things will stay consistent. |
| 844 | * (The bmap-level manipulations are ok, though). |
| 845 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 846 | void |
| 847 | xfs_bmap_local_to_extents_empty( |
| 848 | struct xfs_inode *ip, |
| 849 | int whichfork) |
| 850 | { |
| 851 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 852 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 853 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 854 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 855 | ASSERT(ifp->if_bytes == 0); |
| 856 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 857 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 858 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 859 | ifp->if_flags &= ~XFS_IFINLINE; |
| 860 | ifp->if_flags |= XFS_IFEXTENTS; |
| 861 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 862 | } |
| 863 | |
| 864 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 865 | STATIC int /* error */ |
| 866 | xfs_bmap_local_to_extents( |
| 867 | xfs_trans_t *tp, /* transaction pointer */ |
| 868 | xfs_inode_t *ip, /* incore inode pointer */ |
| 869 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 870 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 871 | int *logflagsp, /* inode logging flags */ |
| 872 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 873 | void (*init_fn)(struct xfs_trans *tp, |
| 874 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 875 | struct xfs_inode *ip, |
| 876 | struct xfs_ifork *ifp)) |
| 877 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 878 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 879 | int flags; /* logging flags returned */ |
| 880 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 881 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 882 | xfs_buf_t *bp; /* buffer for extent block */ |
| 883 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 884 | |
| 885 | /* |
| 886 | * We don't want to deal with the case of keeping inode data inline yet. |
| 887 | * So sending the data fork of a regular inode is invalid. |
| 888 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 889 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 890 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 891 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 892 | |
| 893 | if (!ifp->if_bytes) { |
| 894 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 895 | flags = XFS_ILOG_CORE; |
| 896 | goto done; |
| 897 | } |
| 898 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 899 | flags = 0; |
| 900 | error = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 901 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
| 902 | XFS_IFINLINE); |
| 903 | memset(&args, 0, sizeof(args)); |
| 904 | args.tp = tp; |
| 905 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 906 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 907 | args.firstblock = *firstblock; |
| 908 | /* |
| 909 | * Allocate a block. We know we need only one, since the |
| 910 | * file currently fits in an inode. |
| 911 | */ |
| 912 | if (*firstblock == NULLFSBLOCK) { |
| 913 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 914 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 915 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 916 | args.fsbno = *firstblock; |
| 917 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 918 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 919 | args.total = total; |
| 920 | args.minlen = args.maxlen = args.prod = 1; |
| 921 | error = xfs_alloc_vextent(&args); |
| 922 | if (error) |
| 923 | goto done; |
| 924 | |
| 925 | /* Can't fail, the space was reserved. */ |
| 926 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 927 | ASSERT(args.len == 1); |
| 928 | *firstblock = args.fsbno; |
| 929 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 930 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 931 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 932 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 933 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 934 | * The callout is responsible for logging because the remote format |
| 935 | * might differ from the local format and thus we don't know how much to |
| 936 | * log here. Note that init_fn must also set the buffer log item type |
| 937 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 938 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 939 | init_fn(tp, bp, ip, ifp); |
| 940 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 941 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 942 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 943 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 944 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 945 | |
| 946 | xfs_iext_add(ifp, 0, 1); |
| 947 | ep = xfs_iext_get_ext(ifp, 0); |
| 948 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
| 949 | trace_xfs_bmap_post_update(ip, 0, |
| 950 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 951 | _THIS_IP_); |
| 952 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 953 | ip->i_d.di_nblocks = 1; |
| 954 | xfs_trans_mod_dquot_byino(tp, ip, |
| 955 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 956 | flags |= xfs_ilog_fext(whichfork); |
| 957 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 958 | done: |
| 959 | *logflagsp = flags; |
| 960 | return error; |
| 961 | } |
| 962 | |
| 963 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 965 | */ |
| 966 | STATIC int /* error */ |
| 967 | xfs_bmap_add_attrfork_btree( |
| 968 | xfs_trans_t *tp, /* transaction pointer */ |
| 969 | xfs_inode_t *ip, /* incore inode pointer */ |
| 970 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 971 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | int *flags) /* inode logging flags */ |
| 973 | { |
| 974 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 975 | int error; /* error return value */ |
| 976 | xfs_mount_t *mp; /* file system mount struct */ |
| 977 | int stat; /* newroot status */ |
| 978 | |
| 979 | mp = ip->i_mount; |
| 980 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 981 | *flags |= XFS_ILOG_DBROOT; |
| 982 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 983 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 984 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | cur->bc_private.b.firstblock = *firstblock; |
| 986 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 987 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 988 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 989 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 990 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | goto error0; |
| 992 | if (stat == 0) { |
| 993 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 994 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } |
| 996 | *firstblock = cur->bc_private.b.firstblock; |
| 997 | cur->bc_private.b.allocated = 0; |
| 998 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 999 | } |
| 1000 | return 0; |
| 1001 | error0: |
| 1002 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1003 | return error; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 1008 | */ |
| 1009 | STATIC int /* error */ |
| 1010 | xfs_bmap_add_attrfork_extents( |
| 1011 | xfs_trans_t *tp, /* transaction pointer */ |
| 1012 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1013 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1014 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | int *flags) /* inode logging flags */ |
| 1016 | { |
| 1017 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 1018 | int error; /* error return value */ |
| 1019 | |
| 1020 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 1021 | return 0; |
| 1022 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1023 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | flags, XFS_DATA_FORK); |
| 1025 | if (cur) { |
| 1026 | cur->bc_private.b.allocated = 0; |
| 1027 | xfs_btree_del_cursor(cur, |
| 1028 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 1029 | } |
| 1030 | return error; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1034 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 1035 | * different data fork content type needs a different callout to do the |
| 1036 | * conversion. Some are basic and only require special block initialisation |
| 1037 | * callouts for the data formating, others (directories) are so specialised they |
| 1038 | * handle everything themselves. |
| 1039 | * |
| 1040 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1041 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1042 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | */ |
| 1044 | STATIC int /* error */ |
| 1045 | xfs_bmap_add_attrfork_local( |
| 1046 | xfs_trans_t *tp, /* transaction pointer */ |
| 1047 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1048 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1049 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | int *flags) /* inode logging flags */ |
| 1051 | { |
| 1052 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1055 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1056 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1057 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1059 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | dargs.dp = ip; |
| 1061 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1062 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1063 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | dargs.whichfork = XFS_DATA_FORK; |
| 1065 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1066 | return xfs_dir2_sf_to_block(&dargs); |
| 1067 | } |
| 1068 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1069 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1070 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1071 | flags, XFS_DATA_FORK, |
| 1072 | xfs_symlink_local_to_remote); |
| 1073 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1074 | /* should only be called for types that support local format data */ |
| 1075 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1076 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1080 | * Convert inode from non-attributed to attributed. |
| 1081 | * Must not be in a transaction, ip must not be locked. |
| 1082 | */ |
| 1083 | int /* error code */ |
| 1084 | xfs_bmap_add_attrfork( |
| 1085 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1086 | int size, /* space new attribute needs */ |
| 1087 | int rsvd) /* xact may use reserved blks */ |
| 1088 | { |
| 1089 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1090 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1091 | xfs_mount_t *mp; /* mount structure */ |
| 1092 | xfs_trans_t *tp; /* transaction pointer */ |
| 1093 | int blks; /* space reservation */ |
| 1094 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1095 | int logflags; /* logging flags */ |
| 1096 | int error; /* error return value */ |
| 1097 | |
| 1098 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1099 | |
| 1100 | mp = ip->i_mount; |
| 1101 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1102 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1103 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1104 | |
| 1105 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1106 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1107 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1108 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1109 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1110 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1111 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1112 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1113 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1114 | if (error) |
| 1115 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1116 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1117 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1118 | if (ip->i_d.di_anextents != 0) { |
| 1119 | error = -EFSCORRUPTED; |
| 1120 | goto trans_cancel; |
| 1121 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1122 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1123 | /* |
| 1124 | * For inodes coming from pre-6.2 filesystems. |
| 1125 | */ |
| 1126 | ASSERT(ip->i_d.di_aformat == 0); |
| 1127 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1128 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1129 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1130 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1131 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1132 | |
| 1133 | switch (ip->i_d.di_format) { |
| 1134 | case XFS_DINODE_FMT_DEV: |
| 1135 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1136 | break; |
| 1137 | case XFS_DINODE_FMT_UUID: |
| 1138 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 1139 | break; |
| 1140 | case XFS_DINODE_FMT_LOCAL: |
| 1141 | case XFS_DINODE_FMT_EXTENTS: |
| 1142 | case XFS_DINODE_FMT_BTREE: |
| 1143 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1144 | if (!ip->i_d.di_forkoff) |
| 1145 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1146 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1147 | version = 2; |
| 1148 | break; |
| 1149 | default: |
| 1150 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1151 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1152 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | ASSERT(ip->i_afp == NULL); |
| 1156 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1157 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1158 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1159 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1160 | switch (ip->i_d.di_format) { |
| 1161 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1162 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1163 | &logflags); |
| 1164 | break; |
| 1165 | case XFS_DINODE_FMT_EXTENTS: |
| 1166 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1167 | &dfops, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1168 | break; |
| 1169 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1170 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1171 | &logflags); |
| 1172 | break; |
| 1173 | default: |
| 1174 | error = 0; |
| 1175 | break; |
| 1176 | } |
| 1177 | if (logflags) |
| 1178 | xfs_trans_log_inode(tp, ip, logflags); |
| 1179 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1180 | goto bmap_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1181 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1182 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1183 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1184 | |
| 1185 | spin_lock(&mp->m_sb_lock); |
| 1186 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1187 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1188 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1189 | } |
| 1190 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1191 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1192 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1193 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1194 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1195 | if (log_sb) |
| 1196 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1197 | } |
| 1198 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1199 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1200 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1201 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1202 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1203 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1204 | return error; |
| 1205 | |
| 1206 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1207 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1208 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1209 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1210 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1211 | return error; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * Internal and external extent tree search functions. |
| 1216 | */ |
| 1217 | |
| 1218 | /* |
| 1219 | * Read in the extents to if_extents. |
| 1220 | * All inode fields are set up by caller, we just traverse the btree |
| 1221 | * and copy the records in. If the file system cannot contain unwritten |
| 1222 | * extents, the records are checked for no "state" flags. |
| 1223 | */ |
| 1224 | int /* error */ |
| 1225 | xfs_bmap_read_extents( |
| 1226 | xfs_trans_t *tp, /* transaction pointer */ |
| 1227 | xfs_inode_t *ip, /* incore inode */ |
| 1228 | int whichfork) /* data or attr fork */ |
| 1229 | { |
| 1230 | struct xfs_btree_block *block; /* current btree block */ |
| 1231 | xfs_fsblock_t bno; /* block # of "block" */ |
| 1232 | xfs_buf_t *bp; /* buffer for "block" */ |
| 1233 | int error; /* error return value */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1234 | xfs_extnum_t i, j; /* index into the extents list */ |
| 1235 | xfs_ifork_t *ifp; /* fork structure */ |
| 1236 | int level; /* btree level, for checking */ |
| 1237 | xfs_mount_t *mp; /* file system mount structure */ |
| 1238 | __be64 *pp; /* pointer to block address */ |
| 1239 | /* REFERENCED */ |
| 1240 | xfs_extnum_t room; /* number of entries there's room for */ |
| 1241 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1242 | mp = ip->i_mount; |
| 1243 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1244 | block = ifp->if_broot; |
| 1245 | /* |
| 1246 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1247 | */ |
| 1248 | level = be16_to_cpu(block->bb_level); |
| 1249 | ASSERT(level > 0); |
| 1250 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1251 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1252 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1253 | /* |
| 1254 | * Go down the tree until leaf level is reached, following the first |
| 1255 | * pointer (leftmost) at each level. |
| 1256 | */ |
| 1257 | while (level-- > 0) { |
| 1258 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1259 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1260 | if (error) |
| 1261 | return error; |
| 1262 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1263 | if (level == 0) |
| 1264 | break; |
| 1265 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1266 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1267 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 1268 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1269 | xfs_trans_brelse(tp, bp); |
| 1270 | } |
| 1271 | /* |
| 1272 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1273 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1274 | room = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1275 | i = 0; |
| 1276 | /* |
| 1277 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1278 | */ |
| 1279 | for (;;) { |
| 1280 | xfs_bmbt_rec_t *frp; |
| 1281 | xfs_fsblock_t nextbno; |
| 1282 | xfs_extnum_t num_recs; |
| 1283 | xfs_extnum_t start; |
| 1284 | |
| 1285 | num_recs = xfs_btree_get_numrecs(block); |
| 1286 | if (unlikely(i + num_recs > room)) { |
| 1287 | ASSERT(i + num_recs <= room); |
| 1288 | xfs_warn(ip->i_mount, |
| 1289 | "corrupt dinode %Lu, (btree extents).", |
| 1290 | (unsigned long long) ip->i_ino); |
| 1291 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 1292 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
| 1293 | goto error0; |
| 1294 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1295 | /* |
| 1296 | * Read-ahead the next leaf block, if any. |
| 1297 | */ |
| 1298 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1299 | if (nextbno != NULLFSBLOCK) |
| 1300 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1301 | &xfs_bmbt_buf_ops); |
| 1302 | /* |
| 1303 | * Copy records into the extent records. |
| 1304 | */ |
| 1305 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 1306 | start = i; |
| 1307 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 1308 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
| 1309 | trp->l0 = be64_to_cpu(frp->l0); |
| 1310 | trp->l1 = be64_to_cpu(frp->l1); |
Christoph Hellwig | 0c1d9e4 | 2017-04-20 09:42:48 -0700 | [diff] [blame] | 1311 | if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1312 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
Christoph Hellwig | 0c1d9e4 | 2017-04-20 09:42:48 -0700 | [diff] [blame] | 1313 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1314 | goto error0; |
| 1315 | } |
| 1316 | } |
| 1317 | xfs_trans_brelse(tp, bp); |
| 1318 | bno = nextbno; |
| 1319 | /* |
| 1320 | * If we've reached the end, stop. |
| 1321 | */ |
| 1322 | if (bno == NULLFSBLOCK) |
| 1323 | break; |
| 1324 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1325 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1326 | if (error) |
| 1327 | return error; |
| 1328 | block = XFS_BUF_TO_BLOCK(bp); |
| 1329 | } |
Darrick J. Wong | 356a322 | 2016-12-05 12:36:56 +1100 | [diff] [blame] | 1330 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) |
| 1331 | return -EFSCORRUPTED; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1332 | ASSERT(i == xfs_iext_count(ifp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1333 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
| 1334 | return 0; |
| 1335 | error0: |
| 1336 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1337 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1338 | } |
| 1339 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1340 | /* |
| 1341 | * Returns the file-relative block number of the first unused block(s) |
| 1342 | * in the file with at least "len" logically contiguous blocks free. |
| 1343 | * This is the lowest-address hole if the file has holes, else the first block |
| 1344 | * past the end of file. |
| 1345 | * Return 0 if the file is currently local (in-inode). |
| 1346 | */ |
| 1347 | int /* error */ |
| 1348 | xfs_bmap_first_unused( |
| 1349 | xfs_trans_t *tp, /* transaction pointer */ |
| 1350 | xfs_inode_t *ip, /* incore inode */ |
| 1351 | xfs_extlen_t len, /* size of hole to find */ |
| 1352 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1353 | int whichfork) /* data or attr fork */ |
| 1354 | { |
| 1355 | int error; /* error return value */ |
| 1356 | int idx; /* extent record index */ |
| 1357 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1358 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 1359 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 1360 | xfs_fileoff_t max; /* starting useful block */ |
| 1361 | xfs_fileoff_t off; /* offset for this block */ |
| 1362 | xfs_extnum_t nextents; /* number of extent entries */ |
| 1363 | |
| 1364 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1365 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1366 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 1367 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1368 | *first_unused = 0; |
| 1369 | return 0; |
| 1370 | } |
| 1371 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1372 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1373 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1374 | return error; |
| 1375 | lowest = *first_unused; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1376 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1377 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
| 1378 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
| 1379 | off = xfs_bmbt_get_startoff(ep); |
| 1380 | /* |
| 1381 | * See if the hole before this extent will work. |
| 1382 | */ |
| 1383 | if (off >= lowest + len && off - max >= len) { |
| 1384 | *first_unused = max; |
| 1385 | return 0; |
| 1386 | } |
| 1387 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 1388 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1389 | } |
| 1390 | *first_unused = max; |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1395 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1396 | * last_block (input value) in the file. |
| 1397 | * This is not based on i_size, it is based on the extent records. |
| 1398 | * Returns 0 for local files, as they do not have extent records. |
| 1399 | */ |
| 1400 | int /* error */ |
| 1401 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1402 | struct xfs_trans *tp, /* transaction pointer */ |
| 1403 | struct xfs_inode *ip, /* incore inode */ |
| 1404 | xfs_fileoff_t *last_block, /* last block */ |
| 1405 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1406 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1407 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1408 | struct xfs_bmbt_irec got; |
| 1409 | xfs_extnum_t idx; |
| 1410 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1411 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1412 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1413 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1414 | *last_block = 0; |
| 1415 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1416 | case XFS_DINODE_FMT_BTREE: |
| 1417 | case XFS_DINODE_FMT_EXTENTS: |
| 1418 | break; |
| 1419 | default: |
| 1420 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1421 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1422 | |
| 1423 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1424 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1425 | if (error) |
| 1426 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1427 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1428 | |
| 1429 | if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) { |
| 1430 | if (got.br_startoff <= *last_block - 1) |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | if (xfs_iext_get_extent(ifp, idx - 1, &got)) { |
| 1435 | *last_block = got.br_startoff + got.br_blockcount; |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
| 1439 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1440 | return 0; |
| 1441 | } |
| 1442 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1443 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1444 | xfs_bmap_last_extent( |
| 1445 | struct xfs_trans *tp, |
| 1446 | struct xfs_inode *ip, |
| 1447 | int whichfork, |
| 1448 | struct xfs_bmbt_irec *rec, |
| 1449 | int *is_empty) |
| 1450 | { |
| 1451 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1452 | int error; |
| 1453 | int nextents; |
| 1454 | |
| 1455 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1456 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1457 | if (error) |
| 1458 | return error; |
| 1459 | } |
| 1460 | |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1461 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1462 | if (nextents == 0) { |
| 1463 | *is_empty = 1; |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 1468 | *is_empty = 0; |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * Check the last inode extent to determine whether this allocation will result |
| 1474 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1475 | * blocks at the end of the file which do not start at the previous data block, |
| 1476 | * we will try to align the new blocks at stripe unit boundaries. |
| 1477 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1478 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1479 | * at, or past the EOF. |
| 1480 | */ |
| 1481 | STATIC int |
| 1482 | xfs_bmap_isaeof( |
| 1483 | struct xfs_bmalloca *bma, |
| 1484 | int whichfork) |
| 1485 | { |
| 1486 | struct xfs_bmbt_irec rec; |
| 1487 | int is_empty; |
| 1488 | int error; |
| 1489 | |
| 1490 | bma->aeof = 0; |
| 1491 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1492 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1493 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1494 | return error; |
| 1495 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1496 | if (is_empty) { |
| 1497 | bma->aeof = 1; |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1501 | /* |
| 1502 | * Check if we are allocation or past the last extent, or at least into |
| 1503 | * the last delayed allocated extent. |
| 1504 | */ |
| 1505 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1506 | (bma->offset >= rec.br_startoff && |
| 1507 | isnullstartblock(rec.br_startblock)); |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1512 | * Returns the file-relative block number of the first block past eof in |
| 1513 | * the file. This is not based on i_size, it is based on the extent records. |
| 1514 | * Returns 0 for local files, as they do not have extent records. |
| 1515 | */ |
| 1516 | int |
| 1517 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1518 | struct xfs_inode *ip, |
| 1519 | xfs_fileoff_t *last_block, |
| 1520 | int whichfork) |
| 1521 | { |
| 1522 | struct xfs_bmbt_irec rec; |
| 1523 | int is_empty; |
| 1524 | int error; |
| 1525 | |
| 1526 | *last_block = 0; |
| 1527 | |
| 1528 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1529 | return 0; |
| 1530 | |
| 1531 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1532 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1533 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1534 | |
| 1535 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1536 | if (error || is_empty) |
| 1537 | return error; |
| 1538 | |
| 1539 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | /* |
| 1544 | * Returns whether the selected fork of the inode has exactly one |
| 1545 | * block or not. For the data fork we check this matches di_size, |
| 1546 | * implying the file's range is 0..bsize-1. |
| 1547 | */ |
| 1548 | int /* 1=>1 block, 0=>otherwise */ |
| 1549 | xfs_bmap_one_block( |
| 1550 | xfs_inode_t *ip, /* incore inode */ |
| 1551 | int whichfork) /* data or attr fork */ |
| 1552 | { |
| 1553 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
| 1554 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1555 | int rval; /* return value */ |
| 1556 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 1557 | |
| 1558 | #ifndef DEBUG |
| 1559 | if (whichfork == XFS_DATA_FORK) |
| 1560 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1561 | #endif /* !DEBUG */ |
| 1562 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1563 | return 0; |
| 1564 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1565 | return 0; |
| 1566 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1567 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 1568 | ep = xfs_iext_get_ext(ifp, 0); |
| 1569 | xfs_bmbt_get_all(ep, &s); |
| 1570 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1571 | if (rval && whichfork == XFS_DATA_FORK) |
| 1572 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1573 | return rval; |
| 1574 | } |
| 1575 | |
| 1576 | /* |
| 1577 | * Extent tree manipulation functions used during allocation. |
| 1578 | */ |
| 1579 | |
| 1580 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1581 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | */ |
| 1583 | STATIC int /* error */ |
| 1584 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1585 | struct xfs_bmalloca *bma, |
| 1586 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1588 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1590 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1593 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1595 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1596 | /* left is 0, right is 1, prev is 2 */ |
| 1597 | int rval=0; /* return value (logging flags) */ |
| 1598 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1599 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1600 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1601 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 1602 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1604 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1605 | xfs_extnum_t *nextents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1607 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1608 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1609 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1610 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1611 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1612 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1613 | ASSERT(bma->idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1614 | ASSERT(bma->idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1615 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1616 | ASSERT(!bma->cur || |
| 1617 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1618 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1619 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | #define LEFT r[0] |
| 1622 | #define RIGHT r[1] |
| 1623 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1625 | if (whichfork == XFS_COW_FORK) |
| 1626 | state |= BMAP_COWFORK; |
| 1627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | /* |
| 1629 | * Set up a bunch of variables to make the tests simpler. |
| 1630 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1631 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | xfs_bmbt_get_all(ep, &PREV); |
| 1633 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1634 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1635 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1636 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1637 | da_old = startblockval(PREV.br_startblock); |
| 1638 | da_new = 0; |
| 1639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | /* |
| 1641 | * Set flags determining what part of the previous delayed allocation |
| 1642 | * extent is being replaced by a real allocation. |
| 1643 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1644 | if (PREV.br_startoff == new->br_startoff) |
| 1645 | state |= BMAP_LEFT_FILLING; |
| 1646 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1647 | state |= BMAP_RIGHT_FILLING; |
| 1648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | /* |
| 1650 | * Check and set flags if this segment has a left neighbor. |
| 1651 | * Don't set contiguous if the combined extent would be too large. |
| 1652 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1653 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1654 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1655 | 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] | 1656 | |
| 1657 | if (isnullstartblock(LEFT.br_startblock)) |
| 1658 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1660 | |
| 1661 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1662 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1663 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1664 | LEFT.br_state == new->br_state && |
| 1665 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1666 | state |= BMAP_LEFT_CONTIG; |
| 1667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | /* |
| 1669 | * Check and set flags if this segment has a right neighbor. |
| 1670 | * Don't set contiguous if the combined extent would be too large. |
| 1671 | * Also check for all-three-contiguous being too large. |
| 1672 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1673 | if (bma->idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1674 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1675 | 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] | 1676 | |
| 1677 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1678 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1680 | |
| 1681 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1682 | new_endoff == RIGHT.br_startoff && |
| 1683 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1684 | new->br_state == RIGHT.br_state && |
| 1685 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1686 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1687 | BMAP_RIGHT_FILLING)) != |
| 1688 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1689 | BMAP_RIGHT_FILLING) || |
| 1690 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1691 | <= MAXEXTLEN)) |
| 1692 | state |= BMAP_RIGHT_CONTIG; |
| 1693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | error = 0; |
| 1695 | /* |
| 1696 | * Switch out based on the FILLING and CONTIG state bits. |
| 1697 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1698 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1699 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1700 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1701 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | /* |
| 1703 | * Filling in all of a previously delayed allocation extent. |
| 1704 | * The left and right neighbors are both contiguous with new. |
| 1705 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1706 | bma->idx--; |
| 1707 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1708 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1710 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1711 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1712 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1713 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1714 | (*nextents)--; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1715 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1717 | else { |
| 1718 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1719 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1721 | RIGHT.br_blockcount, &i); |
| 1722 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1724 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1725 | error = xfs_btree_delete(bma->cur, &i); |
| 1726 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1728 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1729 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1730 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1732 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1733 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | LEFT.br_startblock, |
| 1735 | LEFT.br_blockcount + |
| 1736 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1737 | RIGHT.br_blockcount, LEFT.br_state); |
| 1738 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | goto done; |
| 1740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | break; |
| 1742 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1743 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | /* |
| 1745 | * Filling in all of a previously delayed allocation extent. |
| 1746 | * The left neighbor is contiguous, the right is not. |
| 1747 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1748 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1749 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1750 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1751 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1752 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1753 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1754 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1755 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1756 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | rval = XFS_ILOG_DEXT; |
| 1758 | else { |
| 1759 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1760 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1762 | &i); |
| 1763 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1765 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1766 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | LEFT.br_startblock, |
| 1768 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1769 | PREV.br_blockcount, LEFT.br_state); |
| 1770 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | goto done; |
| 1772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | break; |
| 1774 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1775 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | /* |
| 1777 | * Filling in all of a previously delayed allocation extent. |
| 1778 | * The right neighbor is contiguous, the left is not. |
| 1779 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1780 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 1782 | xfs_bmbt_set_blockcount(ep, |
| 1783 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1784 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1785 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1786 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1787 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | rval = XFS_ILOG_DEXT; |
| 1789 | else { |
| 1790 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1791 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1793 | RIGHT.br_blockcount, &i); |
| 1794 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1796 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1797 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | new->br_startblock, |
| 1799 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1800 | RIGHT.br_blockcount, PREV.br_state); |
| 1801 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | goto done; |
| 1803 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | break; |
| 1805 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1806 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | /* |
| 1808 | * Filling in all of a previously delayed allocation extent. |
| 1809 | * Neither the left nor right neighbors are contiguous with |
| 1810 | * the new one. |
| 1811 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1812 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 1814 | xfs_bmbt_set_state(ep, new->br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1815 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1816 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1817 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1818 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1820 | else { |
| 1821 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1822 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1824 | &i); |
| 1825 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1827 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1828 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1829 | error = xfs_btree_insert(bma->cur, &i); |
| 1830 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1832 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | break; |
| 1835 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1836 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | /* |
| 1838 | * Filling in the first part of a previous delayed allocation. |
| 1839 | * The left neighbor is contiguous. |
| 1840 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1841 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 1842 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | LEFT.br_blockcount + new->br_blockcount); |
| 1844 | xfs_bmbt_set_startoff(ep, |
| 1845 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1846 | 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] | 1847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1849 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1851 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | rval = XFS_ILOG_DEXT; |
| 1853 | else { |
| 1854 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1855 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1857 | &i); |
| 1858 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1860 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1861 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | LEFT.br_startblock, |
| 1863 | LEFT.br_blockcount + |
| 1864 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1865 | LEFT.br_state); |
| 1866 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | goto done; |
| 1868 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1869 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1870 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1871 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1872 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1873 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1874 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | break; |
| 1876 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1877 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | /* |
| 1879 | * Filling in the first part of a previous delayed allocation. |
| 1880 | * The left neighbor is not contiguous. |
| 1881 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1882 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1884 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1885 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1886 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1887 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1888 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1890 | else { |
| 1891 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1892 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1894 | &i); |
| 1895 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1897 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1898 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1899 | error = xfs_btree_insert(bma->cur, &i); |
| 1900 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1902 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1904 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1905 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1906 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1907 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1908 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | rval |= tmp_rval; |
| 1910 | if (error) |
| 1911 | goto done; |
| 1912 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1913 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1914 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1915 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 1916 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1917 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1918 | 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] | 1919 | break; |
| 1920 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1921 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | /* |
| 1923 | * Filling in the last part of a previous delayed allocation. |
| 1924 | * The right neighbor is contiguous with the new allocation. |
| 1925 | */ |
| 1926 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1927 | 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] | 1928 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1929 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1930 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | new->br_blockcount + RIGHT.br_blockcount, |
| 1932 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1933 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 1934 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | rval = XFS_ILOG_DEXT; |
| 1936 | else { |
| 1937 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1938 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1940 | RIGHT.br_blockcount, &i); |
| 1941 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1943 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1944 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | new->br_startblock, |
| 1946 | new->br_blockcount + |
| 1947 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1948 | RIGHT.br_state); |
| 1949 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | goto done; |
| 1951 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1952 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1953 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1954 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1955 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1956 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1957 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1958 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1959 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | break; |
| 1961 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1962 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | /* |
| 1964 | * Filling in the last part of a previous delayed allocation. |
| 1965 | * The right neighbor is not contiguous. |
| 1966 | */ |
| 1967 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1968 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1970 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1971 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1972 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1974 | else { |
| 1975 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1976 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1978 | &i); |
| 1979 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1981 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1982 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1983 | error = xfs_btree_insert(bma->cur, &i); |
| 1984 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1986 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1988 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1989 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1990 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1991 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1992 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | rval |= tmp_rval; |
| 1994 | if (error) |
| 1995 | goto done; |
| 1996 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1997 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1998 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1999 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 2000 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2001 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2002 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2003 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2004 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | break; |
| 2006 | |
| 2007 | case 0: |
| 2008 | /* |
| 2009 | * Filling in the middle part of a previous delayed allocation. |
| 2010 | * Contiguity is impossible here. |
| 2011 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2012 | * |
| 2013 | * We start with a delayed allocation: |
| 2014 | * |
| 2015 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 2016 | * PREV @ idx |
| 2017 | * |
| 2018 | * and we are allocating: |
| 2019 | * +rrrrrrrrrrrrrrrrr+ |
| 2020 | * new |
| 2021 | * |
| 2022 | * and we set it up for insertion as: |
| 2023 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 2024 | * new |
| 2025 | * PREV @ idx LEFT RIGHT |
| 2026 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | */ |
| 2028 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2030 | 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] | 2031 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 2032 | LEFT = *new; |
| 2033 | RIGHT.br_state = PREV.br_state; |
| 2034 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2035 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2036 | RIGHT.br_startoff = new_endoff; |
| 2037 | RIGHT.br_blockcount = temp2; |
| 2038 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2039 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2040 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2041 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2043 | else { |
| 2044 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2045 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2047 | &i); |
| 2048 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2050 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2051 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2052 | error = xfs_btree_insert(bma->cur, &i); |
| 2053 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2055 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2057 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2058 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2059 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2060 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2061 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | rval |= tmp_rval; |
| 2063 | if (error) |
| 2064 | goto done; |
| 2065 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2066 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 2067 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Brian Foster | 0daaeca | 2017-05-12 10:44:08 -0700 | [diff] [blame^] | 2068 | diff = (int)(temp + temp2 - |
| 2069 | (startblockval(PREV.br_startblock) - |
| 2070 | (bma->cur ? |
| 2071 | bma->cur->bc_private.b.allocated : 0))); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2072 | if (diff > 0) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2073 | error = xfs_mod_fdblocks(bma->ip->i_mount, |
| 2074 | -((int64_t)diff), false); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2075 | ASSERT(!error); |
| 2076 | if (error) |
| 2077 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2079 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2080 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2081 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2082 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2083 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 2084 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2085 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2086 | 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] | 2087 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2088 | bma->idx++; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2089 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | break; |
| 2091 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2092 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2093 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2094 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2095 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2096 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2097 | case BMAP_LEFT_CONTIG: |
| 2098 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | /* |
| 2100 | * These cases are all impossible. |
| 2101 | */ |
| 2102 | ASSERT(0); |
| 2103 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2104 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2105 | /* add reverse mapping */ |
| 2106 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 2107 | if (error) |
| 2108 | goto done; |
| 2109 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2110 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2111 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2112 | int tmp_logflags; /* partial log flag return val */ |
| 2113 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2114 | ASSERT(bma->cur == NULL); |
| 2115 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2116 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2117 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2118 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2119 | if (error) |
| 2120 | goto done; |
| 2121 | } |
| 2122 | |
| 2123 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2124 | if (da_old || da_new) { |
| 2125 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2126 | if (bma->cur) |
| 2127 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2128 | if (temp < da_old) |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2129 | xfs_mod_fdblocks(bma->ip->i_mount, |
| 2130 | (int64_t)(da_old - temp), false); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2134 | if (bma->cur) |
| 2135 | bma->cur->bc_private.b.allocated = 0; |
| 2136 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2137 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2139 | if (whichfork != XFS_COW_FORK) |
| 2140 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | return error; |
| 2142 | #undef LEFT |
| 2143 | #undef RIGHT |
| 2144 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2148 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | */ |
| 2150 | STATIC int /* error */ |
| 2151 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2152 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2154 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2155 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2157 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2158 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2159 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2160 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2163 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2166 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 2168 | xfs_exntst_t newext; /* new extent state */ |
| 2169 | xfs_exntst_t oldext; /* old extent state */ |
| 2170 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2171 | /* left is 0, right is 1, prev is 2 */ |
| 2172 | int rval=0; /* return value (logging flags) */ |
| 2173 | int state = 0;/* state bits, accessed thru macros */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2174 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2176 | *logflagsp = 0; |
| 2177 | |
| 2178 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2179 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2180 | if (whichfork == XFS_COW_FORK) |
| 2181 | state |= BMAP_COWFORK; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2182 | |
| 2183 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2184 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2185 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2186 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2187 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | #define LEFT r[0] |
| 2190 | #define RIGHT r[1] |
| 2191 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | /* |
| 2194 | * Set up a bunch of variables to make the tests simpler. |
| 2195 | */ |
| 2196 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2197 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | xfs_bmbt_get_all(ep, &PREV); |
| 2199 | newext = new->br_state; |
| 2200 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 2201 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 2202 | ASSERT(PREV.br_state == oldext); |
| 2203 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2204 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2205 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | /* |
| 2208 | * Set flags determining what part of the previous oldext allocation |
| 2209 | * extent is being replaced by a newext allocation. |
| 2210 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2211 | if (PREV.br_startoff == new->br_startoff) |
| 2212 | state |= BMAP_LEFT_FILLING; |
| 2213 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2214 | state |= BMAP_RIGHT_FILLING; |
| 2215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | /* |
| 2217 | * Check and set flags if this segment has a left neighbor. |
| 2218 | * Don't set contiguous if the combined extent would be too large. |
| 2219 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2220 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2221 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2222 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2223 | |
| 2224 | if (isnullstartblock(LEFT.br_startblock)) |
| 2225 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2227 | |
| 2228 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2229 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2230 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 2231 | LEFT.br_state == newext && |
| 2232 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2233 | state |= BMAP_LEFT_CONTIG; |
| 2234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | /* |
| 2236 | * Check and set flags if this segment has a right neighbor. |
| 2237 | * Don't set contiguous if the combined extent would be too large. |
| 2238 | * Also check for all-three-contiguous being too large. |
| 2239 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2240 | if (*idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2241 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2242 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2243 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2244 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2246 | |
| 2247 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2248 | new_endoff == RIGHT.br_startoff && |
| 2249 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 2250 | newext == RIGHT.br_state && |
| 2251 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2252 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2253 | BMAP_RIGHT_FILLING)) != |
| 2254 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2255 | BMAP_RIGHT_FILLING) || |
| 2256 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2257 | <= MAXEXTLEN)) |
| 2258 | state |= BMAP_RIGHT_CONTIG; |
| 2259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | /* |
| 2261 | * Switch out based on the FILLING and CONTIG state bits. |
| 2262 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2263 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2264 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2265 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2266 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | /* |
| 2268 | * Setting all of a previous oldext extent to newext. |
| 2269 | * The left and right neighbors are both contiguous with new. |
| 2270 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2271 | --*idx; |
| 2272 | |
| 2273 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2274 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2276 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2277 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2278 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2279 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2280 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2281 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | if (cur == NULL) |
| 2283 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2284 | else { |
| 2285 | rval = XFS_ILOG_CORE; |
| 2286 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2287 | RIGHT.br_startblock, |
| 2288 | RIGHT.br_blockcount, &i))) |
| 2289 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2290 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2291 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2293 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2294 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2296 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2297 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2299 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2300 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2302 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2304 | LEFT.br_startblock, |
| 2305 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2306 | RIGHT.br_blockcount, LEFT.br_state))) |
| 2307 | goto done; |
| 2308 | } |
| 2309 | break; |
| 2310 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2311 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | /* |
| 2313 | * Setting all of a previous oldext extent to newext. |
| 2314 | * The left neighbor is contiguous, the right is not. |
| 2315 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2316 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2317 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2318 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2319 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 2320 | LEFT.br_blockcount + PREV.br_blockcount); |
| 2321 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2322 | |
| 2323 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2324 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2325 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | if (cur == NULL) |
| 2327 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2328 | else { |
| 2329 | rval = XFS_ILOG_CORE; |
| 2330 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2331 | PREV.br_startblock, PREV.br_blockcount, |
| 2332 | &i))) |
| 2333 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2334 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2335 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2337 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2338 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2340 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2342 | LEFT.br_startblock, |
| 2343 | LEFT.br_blockcount + PREV.br_blockcount, |
| 2344 | LEFT.br_state))) |
| 2345 | goto done; |
| 2346 | } |
| 2347 | break; |
| 2348 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2349 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | /* |
| 2351 | * Setting all of a previous oldext extent to newext. |
| 2352 | * The right neighbor is contiguous, the left is not. |
| 2353 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2354 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | xfs_bmbt_set_blockcount(ep, |
| 2356 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 2357 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2358 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2359 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2360 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2361 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | if (cur == NULL) |
| 2363 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2364 | else { |
| 2365 | rval = XFS_ILOG_CORE; |
| 2366 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2367 | RIGHT.br_startblock, |
| 2368 | RIGHT.br_blockcount, &i))) |
| 2369 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2370 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2371 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2373 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2374 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2376 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2378 | new->br_startblock, |
| 2379 | new->br_blockcount + RIGHT.br_blockcount, |
| 2380 | newext))) |
| 2381 | goto done; |
| 2382 | } |
| 2383 | break; |
| 2384 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2385 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | /* |
| 2387 | * Setting all of a previous oldext extent to newext. |
| 2388 | * Neither the left nor right neighbors are contiguous with |
| 2389 | * the new one. |
| 2390 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2391 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2393 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | if (cur == NULL) |
| 2396 | rval = XFS_ILOG_DEXT; |
| 2397 | else { |
| 2398 | rval = 0; |
| 2399 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2400 | new->br_startblock, new->br_blockcount, |
| 2401 | &i))) |
| 2402 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2403 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2405 | new->br_startblock, new->br_blockcount, |
| 2406 | newext))) |
| 2407 | goto done; |
| 2408 | } |
| 2409 | break; |
| 2410 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2411 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | /* |
| 2413 | * Setting the first part of a previous oldext extent to newext. |
| 2414 | * The left neighbor is contiguous. |
| 2415 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2416 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 2417 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | LEFT.br_blockcount + new->br_blockcount); |
| 2419 | xfs_bmbt_set_startoff(ep, |
| 2420 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2421 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2422 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2423 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | xfs_bmbt_set_startblock(ep, |
| 2425 | new->br_startblock + new->br_blockcount); |
| 2426 | xfs_bmbt_set_blockcount(ep, |
| 2427 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2428 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2429 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2430 | --*idx; |
| 2431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | if (cur == NULL) |
| 2433 | rval = XFS_ILOG_DEXT; |
| 2434 | else { |
| 2435 | rval = 0; |
| 2436 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2437 | PREV.br_startblock, PREV.br_blockcount, |
| 2438 | &i))) |
| 2439 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2440 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | if ((error = xfs_bmbt_update(cur, |
| 2442 | PREV.br_startoff + new->br_blockcount, |
| 2443 | PREV.br_startblock + new->br_blockcount, |
| 2444 | PREV.br_blockcount - new->br_blockcount, |
| 2445 | oldext))) |
| 2446 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2447 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2449 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | LEFT.br_startblock, |
| 2451 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2452 | LEFT.br_state); |
| 2453 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | goto done; |
| 2455 | } |
| 2456 | break; |
| 2457 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2458 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | /* |
| 2460 | * Setting the first part of a previous oldext extent to newext. |
| 2461 | * The left neighbor is not contiguous. |
| 2462 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2463 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 2465 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 2466 | xfs_bmbt_set_blockcount(ep, |
| 2467 | PREV.br_blockcount - new->br_blockcount); |
| 2468 | xfs_bmbt_set_startblock(ep, |
| 2469 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2470 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2471 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2472 | xfs_iext_insert(ip, *idx, 1, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2473 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2474 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | if (cur == NULL) |
| 2476 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2477 | else { |
| 2478 | rval = XFS_ILOG_CORE; |
| 2479 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2480 | PREV.br_startblock, PREV.br_blockcount, |
| 2481 | &i))) |
| 2482 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2483 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | if ((error = xfs_bmbt_update(cur, |
| 2485 | PREV.br_startoff + new->br_blockcount, |
| 2486 | PREV.br_startblock + new->br_blockcount, |
| 2487 | PREV.br_blockcount - new->br_blockcount, |
| 2488 | oldext))) |
| 2489 | goto done; |
| 2490 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2491 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2493 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | } |
| 2495 | break; |
| 2496 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2497 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | /* |
| 2499 | * Setting the last part of a previous oldext extent to newext. |
| 2500 | * The right neighbor is contiguous with the new allocation. |
| 2501 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2502 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | xfs_bmbt_set_blockcount(ep, |
| 2504 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2505 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2506 | |
| 2507 | ++*idx; |
| 2508 | |
| 2509 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2510 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2511 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2513 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | if (cur == NULL) |
| 2516 | rval = XFS_ILOG_DEXT; |
| 2517 | else { |
| 2518 | rval = 0; |
| 2519 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2520 | PREV.br_startblock, |
| 2521 | PREV.br_blockcount, &i))) |
| 2522 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2523 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2525 | PREV.br_startblock, |
| 2526 | PREV.br_blockcount - new->br_blockcount, |
| 2527 | oldext))) |
| 2528 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2529 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | goto done; |
| 2531 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2532 | new->br_startblock, |
| 2533 | new->br_blockcount + RIGHT.br_blockcount, |
| 2534 | newext))) |
| 2535 | goto done; |
| 2536 | } |
| 2537 | break; |
| 2538 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2539 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | /* |
| 2541 | * Setting the last part of a previous oldext extent to newext. |
| 2542 | * The right neighbor is not contiguous. |
| 2543 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2544 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | xfs_bmbt_set_blockcount(ep, |
| 2546 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2547 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2548 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2549 | ++*idx; |
| 2550 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2551 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2552 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2553 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | if (cur == NULL) |
| 2555 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2556 | else { |
| 2557 | rval = XFS_ILOG_CORE; |
| 2558 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2559 | PREV.br_startblock, PREV.br_blockcount, |
| 2560 | &i))) |
| 2561 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2562 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2564 | PREV.br_startblock, |
| 2565 | PREV.br_blockcount - new->br_blockcount, |
| 2566 | oldext))) |
| 2567 | goto done; |
| 2568 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2569 | new->br_startblock, new->br_blockcount, |
| 2570 | &i))) |
| 2571 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2572 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2574 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2576 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | } |
| 2578 | break; |
| 2579 | |
| 2580 | case 0: |
| 2581 | /* |
| 2582 | * Setting the middle part of a previous oldext extent to |
| 2583 | * newext. Contiguity is impossible here. |
| 2584 | * One extent becomes three extents. |
| 2585 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2586 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | xfs_bmbt_set_blockcount(ep, |
| 2588 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2589 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | r[0] = *new; |
| 2592 | r[1].br_startoff = new_endoff; |
| 2593 | r[1].br_blockcount = |
| 2594 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 2595 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 2596 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2597 | |
| 2598 | ++*idx; |
| 2599 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 2600 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2601 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2602 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | if (cur == NULL) |
| 2604 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2605 | else { |
| 2606 | rval = XFS_ILOG_CORE; |
| 2607 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2608 | PREV.br_startblock, PREV.br_blockcount, |
| 2609 | &i))) |
| 2610 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2611 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | /* new right extent - oldext */ |
| 2613 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 2614 | r[1].br_startblock, r[1].br_blockcount, |
| 2615 | r[1].br_state))) |
| 2616 | goto done; |
| 2617 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 2619 | cur->bc_rec.b.br_blockcount = |
| 2620 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2621 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2623 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2624 | /* |
| 2625 | * Reset the cursor to the position of the new extent |
| 2626 | * we are about to insert as we can't trust it after |
| 2627 | * the previous insert. |
| 2628 | */ |
| 2629 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2630 | new->br_startblock, new->br_blockcount, |
| 2631 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2633 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2635 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2636 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2638 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | } |
| 2640 | break; |
| 2641 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2642 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2643 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2644 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2645 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2646 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2647 | case BMAP_LEFT_CONTIG: |
| 2648 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | /* |
| 2650 | * These cases are all impossible. |
| 2651 | */ |
| 2652 | ASSERT(0); |
| 2653 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2654 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2655 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2656 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2657 | if (error) |
| 2658 | goto done; |
| 2659 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2660 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2661 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2662 | int tmp_logflags; /* partial log flag return val */ |
| 2663 | |
| 2664 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2665 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2666 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2667 | *logflagsp |= tmp_logflags; |
| 2668 | if (error) |
| 2669 | goto done; |
| 2670 | } |
| 2671 | |
| 2672 | /* clear out the allocated field, done with it now in any case. */ |
| 2673 | if (cur) { |
| 2674 | cur->bc_private.b.allocated = 0; |
| 2675 | *curp = cur; |
| 2676 | } |
| 2677 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2678 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2680 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | return error; |
| 2682 | #undef LEFT |
| 2683 | #undef RIGHT |
| 2684 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2688 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2690 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | xfs_bmap_add_extent_hole_delay( |
| 2692 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2693 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2694 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2695 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2697 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2699 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2700 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2701 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 2702 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2703 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2705 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | state = 0; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2707 | if (whichfork == XFS_COW_FORK) |
| 2708 | state |= BMAP_COWFORK; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2709 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | /* |
| 2712 | * Check and set flags if this segment has a left neighbor |
| 2713 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2714 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2715 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2716 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2717 | |
| 2718 | if (isnullstartblock(left.br_startblock)) |
| 2719 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | } |
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 the current (right) segment exists. |
| 2724 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2725 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2726 | if (*idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2727 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2728 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2729 | |
| 2730 | if (isnullstartblock(right.br_startblock)) |
| 2731 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | /* |
| 2735 | * Set contiguity flags on the left and right neighbors. |
| 2736 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2737 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2738 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2739 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2740 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2741 | state |= BMAP_LEFT_CONTIG; |
| 2742 | |
| 2743 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2744 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2745 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2746 | (!(state & BMAP_LEFT_CONTIG) || |
| 2747 | (left.br_blockcount + new->br_blockcount + |
| 2748 | right.br_blockcount <= MAXEXTLEN))) |
| 2749 | state |= BMAP_RIGHT_CONTIG; |
| 2750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | /* |
| 2752 | * Switch out based on the contiguity flags. |
| 2753 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2754 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2755 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | /* |
| 2757 | * New allocation is contiguous with delayed allocations |
| 2758 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2759 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2761 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | temp = left.br_blockcount + new->br_blockcount + |
| 2763 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2764 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2765 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2766 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2767 | oldlen = startblockval(left.br_startblock) + |
| 2768 | startblockval(new->br_startblock) + |
| 2769 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2770 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2771 | oldlen); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2772 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2773 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2774 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
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 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | break; |
| 2778 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2779 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | /* |
| 2781 | * New allocation is contiguous with a delayed allocation |
| 2782 | * on the left. |
| 2783 | * Merge the new allocation with the left neighbor. |
| 2784 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2785 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2787 | |
| 2788 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2789 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2790 | oldlen = startblockval(left.br_startblock) + |
| 2791 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2792 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2793 | oldlen); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2794 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2795 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2796 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | break; |
| 2798 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2799 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | /* |
| 2801 | * New allocation is contiguous with a delayed allocation |
| 2802 | * on the right. |
| 2803 | * Merge the new allocation with the right neighbor. |
| 2804 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2805 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2807 | oldlen = startblockval(new->br_startblock) + |
| 2808 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2809 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2810 | oldlen); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2811 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 2812 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2813 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2814 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | break; |
| 2816 | |
| 2817 | case 0: |
| 2818 | /* |
| 2819 | * New allocation is not contiguous with another |
| 2820 | * delayed allocation. |
| 2821 | * Insert a new entry. |
| 2822 | */ |
| 2823 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2824 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | break; |
| 2826 | } |
| 2827 | if (oldlen != newlen) { |
| 2828 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2829 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2830 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | /* |
| 2832 | * Nothing to do for disk quota accounting here. |
| 2833 | */ |
| 2834 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2838 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | */ |
| 2840 | STATIC int /* error */ |
| 2841 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2842 | struct xfs_trans *tp, |
| 2843 | struct xfs_inode *ip, |
| 2844 | int whichfork, |
| 2845 | xfs_extnum_t *idx, |
| 2846 | struct xfs_btree_cur **curp, |
| 2847 | struct xfs_bmbt_irec *new, |
| 2848 | xfs_fsblock_t *first, |
| 2849 | struct xfs_defer_ops *dfops, |
| 2850 | int *logflagsp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2852 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2853 | struct xfs_mount *mp = ip->i_mount; |
| 2854 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2858 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2859 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | int state; /* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2862 | ASSERT(*idx >= 0); |
| 2863 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2864 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2865 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2866 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2867 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2868 | |
| 2869 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2870 | if (whichfork == XFS_ATTR_FORK) |
| 2871 | state |= BMAP_ATTRFORK; |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 2872 | if (whichfork == XFS_COW_FORK) |
| 2873 | state |= BMAP_COWFORK; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | /* |
| 2876 | * Check and set flags if this segment has a left neighbor. |
| 2877 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2878 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2879 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2880 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2881 | if (isnullstartblock(left.br_startblock)) |
| 2882 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | /* |
| 2886 | * Check and set flags if this segment has a current value. |
| 2887 | * Not true if we're inserting into the "hole" at eof. |
| 2888 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2889 | if (*idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2890 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2891 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2892 | if (isnullstartblock(right.br_startblock)) |
| 2893 | state |= BMAP_RIGHT_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 | * We're inserting a real allocation between "left" and "right". |
| 2898 | * Set the contiguity flags. Don't let extents get too large. |
| 2899 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2900 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2901 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2902 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2903 | left.br_state == new->br_state && |
| 2904 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2905 | state |= BMAP_LEFT_CONTIG; |
| 2906 | |
| 2907 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2908 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2909 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2910 | new->br_state == right.br_state && |
| 2911 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2912 | (!(state & BMAP_LEFT_CONTIG) || |
| 2913 | left.br_blockcount + new->br_blockcount + |
| 2914 | right.br_blockcount <= MAXEXTLEN)) |
| 2915 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2917 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | /* |
| 2919 | * Select which case we're in here, and implement it. |
| 2920 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2921 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2922 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | /* |
| 2924 | * New allocation is contiguous with real allocations on the |
| 2925 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2926 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2927 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2928 | --*idx; |
| 2929 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2930 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | left.br_blockcount + new->br_blockcount + |
| 2932 | right.br_blockcount); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2933 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2934 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2935 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2936 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2937 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2938 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2939 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2940 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2941 | } else { |
| 2942 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2943 | error = xfs_bmbt_lookup_eq(cur, right.br_startoff, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2944 | right.br_startblock, right.br_blockcount, |
| 2945 | &i); |
| 2946 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2947 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2948 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2949 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2950 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2951 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2952 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2953 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2954 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2955 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2956 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2957 | error = xfs_bmbt_update(cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2958 | left.br_startblock, |
| 2959 | left.br_blockcount + |
| 2960 | new->br_blockcount + |
| 2961 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2962 | left.br_state); |
| 2963 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2964 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2966 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2968 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | /* |
| 2970 | * New allocation is contiguous with a real allocation |
| 2971 | * on the left. |
| 2972 | * Merge the new allocation with the left neighbor. |
| 2973 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2974 | --*idx; |
| 2975 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2976 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2978 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2979 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2980 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2981 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2982 | } else { |
| 2983 | rval = 0; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2984 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2985 | left.br_startblock, left.br_blockcount, |
| 2986 | &i); |
| 2987 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2988 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2989 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2990 | error = xfs_bmbt_update(cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2991 | left.br_startblock, |
| 2992 | left.br_blockcount + |
| 2993 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2994 | left.br_state); |
| 2995 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2996 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2998 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3000 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | /* |
| 3002 | * New allocation is contiguous with a real allocation |
| 3003 | * on the right. |
| 3004 | * Merge the new allocation with the right neighbor. |
| 3005 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3006 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 3007 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 3008 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | new->br_blockcount + right.br_blockcount, |
| 3010 | right.br_state); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3011 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3012 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3013 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3014 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3015 | } else { |
| 3016 | rval = 0; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3017 | error = xfs_bmbt_lookup_eq(cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3018 | right.br_startoff, |
| 3019 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3020 | right.br_blockcount, &i); |
| 3021 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3022 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3023 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3024 | error = xfs_bmbt_update(cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3025 | new->br_startblock, |
| 3026 | new->br_blockcount + |
| 3027 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3028 | right.br_state); |
| 3029 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3030 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3032 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | |
| 3034 | case 0: |
| 3035 | /* |
| 3036 | * New allocation is not contiguous with another |
| 3037 | * real allocation. |
| 3038 | * Insert a new entry. |
| 3039 | */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3040 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 3041 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3042 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 3043 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3044 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3045 | } else { |
| 3046 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3047 | error = xfs_bmbt_lookup_eq(cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3048 | new->br_startoff, |
| 3049 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3050 | new->br_blockcount, &i); |
| 3051 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3052 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3053 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3054 | cur->bc_rec.b.br_state = new->br_state; |
| 3055 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3056 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3057 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3058 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3060 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3062 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 3063 | /* add reverse mapping */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3064 | error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 3065 | if (error) |
| 3066 | goto done; |
| 3067 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3068 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3069 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3070 | int tmp_logflags; /* partial log flag return val */ |
| 3071 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3072 | ASSERT(cur == NULL); |
| 3073 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3074 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3075 | *logflagsp |= tmp_logflags; |
| 3076 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3077 | if (error) |
| 3078 | goto done; |
| 3079 | } |
| 3080 | |
| 3081 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3082 | if (cur) |
| 3083 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3084 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3085 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3086 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 3087 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3088 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | } |
| 3090 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3091 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 3092 | * Functions used in the extent read, allocate and remove paths |
| 3093 | */ |
| 3094 | |
| 3095 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3096 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 3097 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3098 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3099 | xfs_bmap_extsize_align( |
| 3100 | xfs_mount_t *mp, |
| 3101 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 3102 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 3103 | xfs_extlen_t extsz, /* align to this extent size */ |
| 3104 | int rt, /* is this a realtime inode? */ |
| 3105 | int eof, /* is extent at end-of-file? */ |
| 3106 | int delay, /* creating delalloc extent? */ |
| 3107 | int convert, /* overwriting unwritten extent? */ |
| 3108 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 3109 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 3110 | { |
| 3111 | xfs_fileoff_t orig_off; /* original offset */ |
| 3112 | xfs_extlen_t orig_alen; /* original length */ |
| 3113 | xfs_fileoff_t orig_end; /* original off+len */ |
| 3114 | xfs_fileoff_t nexto; /* next file offset */ |
| 3115 | xfs_fileoff_t prevo; /* previous file offset */ |
| 3116 | xfs_fileoff_t align_off; /* temp for offset */ |
| 3117 | xfs_extlen_t align_alen; /* temp for length */ |
| 3118 | xfs_extlen_t temp; /* temp for calculations */ |
| 3119 | |
| 3120 | if (convert) |
| 3121 | return 0; |
| 3122 | |
| 3123 | orig_off = align_off = *offp; |
| 3124 | orig_alen = align_alen = *lenp; |
| 3125 | orig_end = orig_off + orig_alen; |
| 3126 | |
| 3127 | /* |
| 3128 | * If this request overlaps an existing extent, then don't |
| 3129 | * attempt to perform any additional alignment. |
| 3130 | */ |
| 3131 | if (!delay && !eof && |
| 3132 | (orig_off >= gotp->br_startoff) && |
| 3133 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 3134 | return 0; |
| 3135 | } |
| 3136 | |
| 3137 | /* |
| 3138 | * If the file offset is unaligned vs. the extent size |
| 3139 | * we need to align it. This will be possible unless |
| 3140 | * the file was previously written with a kernel that didn't |
| 3141 | * perform this alignment, or if a truncate shot us in the |
| 3142 | * foot. |
| 3143 | */ |
| 3144 | temp = do_mod(orig_off, extsz); |
| 3145 | if (temp) { |
| 3146 | align_alen += temp; |
| 3147 | align_off -= temp; |
| 3148 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3149 | |
| 3150 | /* Same adjustment for the end of the requested area. */ |
| 3151 | temp = (align_alen % extsz); |
| 3152 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3153 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3154 | |
| 3155 | /* |
| 3156 | * For large extent hint sizes, the aligned extent might be larger than |
| 3157 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 3158 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 3159 | * short allocation just fine, so it is safe to do this. We only want to |
| 3160 | * do it when we are forced to, though, because it means more allocation |
| 3161 | * operations are required. |
| 3162 | */ |
| 3163 | while (align_alen > MAXEXTLEN) |
| 3164 | align_alen -= extsz; |
| 3165 | ASSERT(align_alen <= MAXEXTLEN); |
| 3166 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3167 | /* |
| 3168 | * If the previous block overlaps with this proposed allocation |
| 3169 | * then move the start forward without adjusting the length. |
| 3170 | */ |
| 3171 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3172 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3173 | prevo = prevp->br_startoff; |
| 3174 | else |
| 3175 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3176 | } else |
| 3177 | prevo = 0; |
| 3178 | if (align_off != orig_off && align_off < prevo) |
| 3179 | align_off = prevo; |
| 3180 | /* |
| 3181 | * If the next block overlaps with this proposed allocation |
| 3182 | * then move the start back without adjusting the length, |
| 3183 | * but not before offset 0. |
| 3184 | * This may of course make the start overlap previous block, |
| 3185 | * and if we hit the offset 0 limit then the next block |
| 3186 | * can still overlap too. |
| 3187 | */ |
| 3188 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3189 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3190 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3191 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3192 | else |
| 3193 | nexto = gotp->br_startoff; |
| 3194 | } else |
| 3195 | nexto = NULLFILEOFF; |
| 3196 | if (!eof && |
| 3197 | align_off + align_alen != orig_end && |
| 3198 | align_off + align_alen > nexto) |
| 3199 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3200 | /* |
| 3201 | * If we're now overlapping the next or previous extent that |
| 3202 | * means we can't fit an extsz piece in this hole. Just move |
| 3203 | * the start forward to the first valid spot and set |
| 3204 | * the length so we hit the end. |
| 3205 | */ |
| 3206 | if (align_off != orig_off && align_off < prevo) |
| 3207 | align_off = prevo; |
| 3208 | if (align_off + align_alen != orig_end && |
| 3209 | align_off + align_alen > nexto && |
| 3210 | nexto != NULLFILEOFF) { |
| 3211 | ASSERT(nexto > prevo); |
| 3212 | align_alen = nexto - align_off; |
| 3213 | } |
| 3214 | |
| 3215 | /* |
| 3216 | * If realtime, and the result isn't a multiple of the realtime |
| 3217 | * extent size we need to remove blocks until it is. |
| 3218 | */ |
| 3219 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3220 | /* |
| 3221 | * We're not covering the original request, or |
| 3222 | * we won't be able to once we fix the length. |
| 3223 | */ |
| 3224 | if (orig_off < align_off || |
| 3225 | orig_end > align_off + align_alen || |
| 3226 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3227 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3228 | /* |
| 3229 | * Try to fix it by moving the start up. |
| 3230 | */ |
| 3231 | if (align_off + temp <= orig_off) { |
| 3232 | align_alen -= temp; |
| 3233 | align_off += temp; |
| 3234 | } |
| 3235 | /* |
| 3236 | * Try to fix it by moving the end in. |
| 3237 | */ |
| 3238 | else if (align_off + align_alen - temp >= orig_end) |
| 3239 | align_alen -= temp; |
| 3240 | /* |
| 3241 | * Set the start to the minimum then trim the length. |
| 3242 | */ |
| 3243 | else { |
| 3244 | align_alen -= orig_off - align_off; |
| 3245 | align_off = orig_off; |
| 3246 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3247 | } |
| 3248 | /* |
| 3249 | * Result doesn't cover the request, fail it. |
| 3250 | */ |
| 3251 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3252 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3253 | } else { |
| 3254 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3255 | /* see MAXEXTLEN handling above */ |
| 3256 | ASSERT(orig_end <= align_off + align_alen || |
| 3257 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | #ifdef DEBUG |
| 3261 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3262 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3263 | if (prevp->br_startoff != NULLFILEOFF) |
| 3264 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3265 | #endif |
| 3266 | |
| 3267 | *lenp = align_alen; |
| 3268 | *offp = align_off; |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3273 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3274 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3275 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3276 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | { |
| 3278 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3280 | xfs_mount_t *mp; /* mount point structure */ |
| 3281 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3282 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | |
| 3284 | #define ISVALID(x,y) \ |
| 3285 | (rt ? \ |
| 3286 | (x) < mp->m_sb.sb_rblocks : \ |
| 3287 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3288 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3289 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3292 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3293 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3294 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3295 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | /* |
| 3297 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3298 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3300 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3301 | !isnullstartblock(ap->prev.br_startblock) && |
| 3302 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3303 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3304 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | /* |
| 3306 | * Adjust for the gap between prevp and us. |
| 3307 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3308 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3309 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3311 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3312 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | } |
| 3314 | /* |
| 3315 | * If not at eof, then compare the two neighbor blocks. |
| 3316 | * Figure out whether either one gives us a good starting point, |
| 3317 | * and pick the better one. |
| 3318 | */ |
| 3319 | else if (!ap->eof) { |
| 3320 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3321 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3322 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3323 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3324 | |
| 3325 | /* |
| 3326 | * If there's a previous (left) block, select a requested |
| 3327 | * start block based on it. |
| 3328 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3329 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3330 | !isnullstartblock(ap->prev.br_startblock) && |
| 3331 | (prevbno = ap->prev.br_startblock + |
| 3332 | ap->prev.br_blockcount) && |
| 3333 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | /* |
| 3335 | * Calculate gap to end of previous block. |
| 3336 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3337 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3338 | (ap->prev.br_startoff + |
| 3339 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | /* |
| 3341 | * Figure the startblock based on the previous block's |
| 3342 | * end and the gap size. |
| 3343 | * Heuristic! |
| 3344 | * If the gap is large relative to the piece we're |
| 3345 | * allocating, or using it gives us an invalid block |
| 3346 | * number, then just use the end of the previous block. |
| 3347 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3348 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3350 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3351 | prevbno += adjust; |
| 3352 | else |
| 3353 | prevdiff += adjust; |
| 3354 | /* |
| 3355 | * If the firstblock forbids it, can't use it, |
| 3356 | * must use default. |
| 3357 | */ |
| 3358 | if (!rt && !nullfb && |
| 3359 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3360 | prevbno = NULLFSBLOCK; |
| 3361 | } |
| 3362 | /* |
| 3363 | * No previous block or can't follow it, just default. |
| 3364 | */ |
| 3365 | else |
| 3366 | prevbno = NULLFSBLOCK; |
| 3367 | /* |
| 3368 | * If there's a following (right) block, select a requested |
| 3369 | * start block based on it. |
| 3370 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3371 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | /* |
| 3373 | * Calculate gap to start of next block. |
| 3374 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3375 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | /* |
| 3377 | * Figure the startblock based on the next block's |
| 3378 | * start and the gap size. |
| 3379 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3380 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | /* |
| 3382 | * Heuristic! |
| 3383 | * If the gap is large relative to the piece we're |
| 3384 | * allocating, or using it gives us an invalid block |
| 3385 | * number, then just use the start of the next block |
| 3386 | * offset by our length. |
| 3387 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3388 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3390 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3391 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3392 | gotbno -= ap->length; |
| 3393 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3394 | } else |
| 3395 | gotdiff += adjust; |
| 3396 | /* |
| 3397 | * If the firstblock forbids it, can't use it, |
| 3398 | * must use default. |
| 3399 | */ |
| 3400 | if (!rt && !nullfb && |
| 3401 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3402 | gotbno = NULLFSBLOCK; |
| 3403 | } |
| 3404 | /* |
| 3405 | * No next block, just default. |
| 3406 | */ |
| 3407 | else |
| 3408 | gotbno = NULLFSBLOCK; |
| 3409 | /* |
| 3410 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3411 | * 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] | 3412 | */ |
| 3413 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3414 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3416 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3418 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3420 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3421 | } |
| 3422 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3423 | static int |
| 3424 | xfs_bmap_longest_free_extent( |
| 3425 | struct xfs_trans *tp, |
| 3426 | xfs_agnumber_t ag, |
| 3427 | xfs_extlen_t *blen, |
| 3428 | int *notinit) |
| 3429 | { |
| 3430 | struct xfs_mount *mp = tp->t_mountp; |
| 3431 | struct xfs_perag *pag; |
| 3432 | xfs_extlen_t longest; |
| 3433 | int error = 0; |
| 3434 | |
| 3435 | pag = xfs_perag_get(mp, ag); |
| 3436 | if (!pag->pagf_init) { |
| 3437 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3438 | if (error) |
| 3439 | goto out; |
| 3440 | |
| 3441 | if (!pag->pagf_init) { |
| 3442 | *notinit = 1; |
| 3443 | goto out; |
| 3444 | } |
| 3445 | } |
| 3446 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 3447 | longest = xfs_alloc_longest_free_extent(mp, pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3448 | xfs_alloc_min_freelist(mp, pag), |
| 3449 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3450 | if (*blen < longest) |
| 3451 | *blen = longest; |
| 3452 | |
| 3453 | out: |
| 3454 | xfs_perag_put(pag); |
| 3455 | return error; |
| 3456 | } |
| 3457 | |
| 3458 | static void |
| 3459 | xfs_bmap_select_minlen( |
| 3460 | struct xfs_bmalloca *ap, |
| 3461 | struct xfs_alloc_arg *args, |
| 3462 | xfs_extlen_t *blen, |
| 3463 | int notinit) |
| 3464 | { |
| 3465 | if (notinit || *blen < ap->minlen) { |
| 3466 | /* |
| 3467 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3468 | * there is space for this request. |
| 3469 | */ |
| 3470 | args->minlen = ap->minlen; |
| 3471 | } else if (*blen < args->maxlen) { |
| 3472 | /* |
| 3473 | * If the best seen length is less than the request length, |
| 3474 | * use the best as the minimum. |
| 3475 | */ |
| 3476 | args->minlen = *blen; |
| 3477 | } else { |
| 3478 | /* |
| 3479 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3480 | * as the minimum. |
| 3481 | */ |
| 3482 | args->minlen = args->maxlen; |
| 3483 | } |
| 3484 | } |
| 3485 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3486 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3487 | xfs_bmap_btalloc_nullfb( |
| 3488 | struct xfs_bmalloca *ap, |
| 3489 | struct xfs_alloc_arg *args, |
| 3490 | xfs_extlen_t *blen) |
| 3491 | { |
| 3492 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3493 | xfs_agnumber_t ag, startag; |
| 3494 | int notinit = 0; |
| 3495 | int error; |
| 3496 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3497 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3498 | args->total = ap->total; |
| 3499 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3500 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3501 | if (startag == NULLAGNUMBER) |
| 3502 | startag = ag = 0; |
| 3503 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3504 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3505 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3506 | ¬init); |
| 3507 | if (error) |
| 3508 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3509 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3510 | if (++ag == mp->m_sb.sb_agcount) |
| 3511 | ag = 0; |
| 3512 | if (ag == startag) |
| 3513 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3514 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3515 | |
| 3516 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3517 | return 0; |
| 3518 | } |
| 3519 | |
| 3520 | STATIC int |
| 3521 | xfs_bmap_btalloc_filestreams( |
| 3522 | struct xfs_bmalloca *ap, |
| 3523 | struct xfs_alloc_arg *args, |
| 3524 | xfs_extlen_t *blen) |
| 3525 | { |
| 3526 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3527 | xfs_agnumber_t ag; |
| 3528 | int notinit = 0; |
| 3529 | int error; |
| 3530 | |
| 3531 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3532 | args->total = ap->total; |
| 3533 | |
| 3534 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3535 | if (ag == NULLAGNUMBER) |
| 3536 | ag = 0; |
| 3537 | |
| 3538 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3539 | if (error) |
| 3540 | return error; |
| 3541 | |
| 3542 | if (*blen < args->maxlen) { |
| 3543 | error = xfs_filestream_new_ag(ap, &ag); |
| 3544 | if (error) |
| 3545 | return error; |
| 3546 | |
| 3547 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3548 | ¬init); |
| 3549 | if (error) |
| 3550 | return error; |
| 3551 | |
| 3552 | } |
| 3553 | |
| 3554 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3555 | |
| 3556 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3557 | * Set the failure fallback case to look in the selected AG as stream |
| 3558 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3559 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3560 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3561 | return 0; |
| 3562 | } |
| 3563 | |
| 3564 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3565 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3566 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3567 | { |
| 3568 | xfs_mount_t *mp; /* mount point structure */ |
| 3569 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3570 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3571 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3572 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3573 | xfs_alloc_arg_t args; |
| 3574 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3575 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3576 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3577 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3578 | int tryagain; |
| 3579 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3580 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3581 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3582 | ASSERT(ap->length); |
| 3583 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3584 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3585 | |
| 3586 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3587 | stripe_align = 0; |
| 3588 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3589 | stripe_align = mp->m_swidth; |
| 3590 | else if (mp->m_dalign) |
| 3591 | stripe_align = mp->m_dalign; |
| 3592 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3593 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3594 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3595 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3596 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3597 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3598 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3599 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3600 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3601 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3602 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3603 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3604 | |
| 3605 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3606 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3607 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3608 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3609 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3610 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3611 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3612 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3613 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3614 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3615 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3616 | } |
| 3617 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3618 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3619 | |
| 3620 | xfs_bmap_adjacent(ap); |
| 3621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3623 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | * it's in the right allocation group. |
| 3625 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3626 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | ; |
| 3628 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3629 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | * Normal allocation, done through xfs_alloc_vextent. |
| 3632 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3633 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3634 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3635 | args.tp = ap->tp; |
| 3636 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3637 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3638 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3639 | |
| 3640 | /* Trim the allocation back to the maximum an AG can fit. */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 3641 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3642 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3643 | blen = 0; |
| 3644 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3645 | /* |
| 3646 | * Search for an allocation group with a single extent large |
| 3647 | * enough for the request. If one isn't found, then adjust |
| 3648 | * the minimum allocation size to the largest space found. |
| 3649 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3650 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3651 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3652 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3653 | else |
| 3654 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3655 | if (error) |
| 3656 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3657 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3658 | if (xfs_inode_is_filestream(ap->ip)) |
| 3659 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3660 | else |
| 3661 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3662 | args.total = args.minlen = ap->minlen; |
| 3663 | } else { |
| 3664 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3665 | args.total = ap->total; |
| 3666 | args.minlen = ap->minlen; |
| 3667 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3668 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3669 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3670 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3671 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3672 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3673 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3674 | args.prod = 1; |
| 3675 | args.mod = 0; |
| 3676 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3677 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3678 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3679 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3680 | } |
| 3681 | /* |
| 3682 | * If we are not low on available data blocks, and the |
| 3683 | * underlying logical volume manager is a stripe, and |
| 3684 | * the file offset is zero then try to allocate data |
| 3685 | * blocks on stripe unit boundary. |
| 3686 | * NOTE: ap->aeof is only set if the allocation length |
| 3687 | * is >= the stripe unit and the allocation offset is |
| 3688 | * at the end of file. |
| 3689 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3690 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3691 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3692 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3693 | atype = args.type; |
| 3694 | isaligned = 1; |
| 3695 | /* |
| 3696 | * Adjust for alignment |
| 3697 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3698 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3699 | args.minlen = blen - args.alignment; |
| 3700 | args.minalignslop = 0; |
| 3701 | } else { |
| 3702 | /* |
| 3703 | * First try an exact bno allocation. |
| 3704 | * If it fails then do a near or start bno |
| 3705 | * allocation with alignment turned on. |
| 3706 | */ |
| 3707 | atype = args.type; |
| 3708 | tryagain = 1; |
| 3709 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3710 | args.alignment = 1; |
| 3711 | /* |
| 3712 | * Compute the minlen+alignment for the |
| 3713 | * next case. Set slop so that the value |
| 3714 | * of minlen+alignment+slop doesn't go up |
| 3715 | * between the calls. |
| 3716 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3717 | if (blen > stripe_align && blen <= args.maxlen) |
| 3718 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3719 | else |
| 3720 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3721 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3722 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3723 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3724 | args.minlen - 1; |
| 3725 | else |
| 3726 | args.minalignslop = 0; |
| 3727 | } |
| 3728 | } else { |
| 3729 | args.alignment = 1; |
| 3730 | args.minalignslop = 0; |
| 3731 | } |
| 3732 | args.minleft = ap->minleft; |
| 3733 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3734 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3735 | args.datatype = ap->datatype; |
| 3736 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3737 | args.ip = ap->ip; |
| 3738 | |
| 3739 | error = xfs_alloc_vextent(&args); |
| 3740 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3741 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3742 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3743 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3744 | /* |
| 3745 | * Exact allocation failed. Now try with alignment |
| 3746 | * turned on. |
| 3747 | */ |
| 3748 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3749 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3750 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3751 | args.minlen = nextminlen; |
| 3752 | args.minalignslop = 0; |
| 3753 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | if ((error = xfs_alloc_vextent(&args))) |
| 3755 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3756 | } |
| 3757 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3758 | /* |
| 3759 | * allocation failed, so turn off alignment and |
| 3760 | * try again. |
| 3761 | */ |
| 3762 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3763 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3764 | args.alignment = 0; |
| 3765 | if ((error = xfs_alloc_vextent(&args))) |
| 3766 | return error; |
| 3767 | } |
| 3768 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3769 | args.minlen > ap->minlen) { |
| 3770 | args.minlen = ap->minlen; |
| 3771 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3772 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3773 | if ((error = xfs_alloc_vextent(&args))) |
| 3774 | return error; |
| 3775 | } |
| 3776 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3777 | args.fsbno = 0; |
| 3778 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3779 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3780 | if ((error = xfs_alloc_vextent(&args))) |
| 3781 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3782 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3783 | } |
| 3784 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3785 | /* |
| 3786 | * check the allocation happened at the same or higher AG than |
| 3787 | * the first block that was allocated. |
| 3788 | */ |
| 3789 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3790 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) <= |
| 3791 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3792 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3793 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3794 | if (*ap->firstblock == NULLFSBLOCK) |
| 3795 | *ap->firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3796 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3797 | ap->length = args.len; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 3798 | if (!(ap->flags & XFS_BMAPI_COWFORK)) |
| 3799 | ap->ip->i_d.di_nblocks += args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3800 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3801 | if (ap->wasdel) |
| 3802 | ap->ip->i_delayed_blks -= args.len; |
| 3803 | /* |
| 3804 | * Adjust the disk quota also. This was reserved |
| 3805 | * earlier. |
| 3806 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3807 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3808 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3809 | XFS_TRANS_DQ_BCOUNT, |
| 3810 | (long) args.len); |
| 3811 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3812 | ap->blkno = NULLFSBLOCK; |
| 3813 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | } |
| 3815 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | /* |
| 3819 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3820 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3821 | */ |
| 3822 | STATIC int |
| 3823 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3824 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3825 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3826 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3827 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3828 | return xfs_bmap_rtalloc(ap); |
| 3829 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | } |
| 3831 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3832 | /* Trim extent to fit a logical block range. */ |
| 3833 | void |
| 3834 | xfs_trim_extent( |
| 3835 | struct xfs_bmbt_irec *irec, |
| 3836 | xfs_fileoff_t bno, |
| 3837 | xfs_filblks_t len) |
| 3838 | { |
| 3839 | xfs_fileoff_t distance; |
| 3840 | xfs_fileoff_t end = bno + len; |
| 3841 | |
| 3842 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3843 | irec->br_startoff >= end) { |
| 3844 | irec->br_blockcount = 0; |
| 3845 | return; |
| 3846 | } |
| 3847 | |
| 3848 | if (irec->br_startoff < bno) { |
| 3849 | distance = bno - irec->br_startoff; |
| 3850 | if (isnullstartblock(irec->br_startblock)) |
| 3851 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3852 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3853 | irec->br_startblock != HOLESTARTBLOCK) |
| 3854 | irec->br_startblock += distance; |
| 3855 | irec->br_startoff += distance; |
| 3856 | irec->br_blockcount -= distance; |
| 3857 | } |
| 3858 | |
| 3859 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3860 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3861 | irec->br_blockcount -= distance; |
| 3862 | } |
| 3863 | } |
| 3864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3866 | * Trim the returned map to the required bounds |
| 3867 | */ |
| 3868 | STATIC void |
| 3869 | xfs_bmapi_trim_map( |
| 3870 | struct xfs_bmbt_irec *mval, |
| 3871 | struct xfs_bmbt_irec *got, |
| 3872 | xfs_fileoff_t *bno, |
| 3873 | xfs_filblks_t len, |
| 3874 | xfs_fileoff_t obno, |
| 3875 | xfs_fileoff_t end, |
| 3876 | int n, |
| 3877 | int flags) |
| 3878 | { |
| 3879 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3880 | got->br_startoff + got->br_blockcount <= obno) { |
| 3881 | *mval = *got; |
| 3882 | if (isnullstartblock(got->br_startblock)) |
| 3883 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3884 | return; |
| 3885 | } |
| 3886 | |
| 3887 | if (obno > *bno) |
| 3888 | *bno = obno; |
| 3889 | ASSERT((*bno >= obno) || (n == 0)); |
| 3890 | ASSERT(*bno < end); |
| 3891 | mval->br_startoff = *bno; |
| 3892 | if (isnullstartblock(got->br_startblock)) |
| 3893 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3894 | else |
| 3895 | mval->br_startblock = got->br_startblock + |
| 3896 | (*bno - got->br_startoff); |
| 3897 | /* |
| 3898 | * Return the minimum of what we got and what we asked for for |
| 3899 | * the length. We can use the len variable here because it is |
| 3900 | * modified below and we could have been there before coming |
| 3901 | * here if the first part of the allocation didn't overlap what |
| 3902 | * was asked for. |
| 3903 | */ |
| 3904 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3905 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3906 | mval->br_state = got->br_state; |
| 3907 | ASSERT(mval->br_blockcount <= len); |
| 3908 | return; |
| 3909 | } |
| 3910 | |
| 3911 | /* |
| 3912 | * Update and validate the extent map to return |
| 3913 | */ |
| 3914 | STATIC void |
| 3915 | xfs_bmapi_update_map( |
| 3916 | struct xfs_bmbt_irec **map, |
| 3917 | xfs_fileoff_t *bno, |
| 3918 | xfs_filblks_t *len, |
| 3919 | xfs_fileoff_t obno, |
| 3920 | xfs_fileoff_t end, |
| 3921 | int *n, |
| 3922 | int flags) |
| 3923 | { |
| 3924 | xfs_bmbt_irec_t *mval = *map; |
| 3925 | |
| 3926 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3927 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3928 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3929 | (mval->br_startoff < obno)); |
| 3930 | |
| 3931 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3932 | *len = end - *bno; |
| 3933 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3934 | /* update previous map with new information */ |
| 3935 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3936 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3937 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3938 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3939 | mval[-1].br_state = mval->br_state; |
| 3940 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3941 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3942 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3943 | mval->br_startblock == mval[-1].br_startblock + |
| 3944 | mval[-1].br_blockcount && |
| 3945 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3946 | mval[-1].br_state == mval->br_state)) { |
| 3947 | ASSERT(mval->br_startoff == |
| 3948 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3949 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3950 | } else if (*n > 0 && |
| 3951 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3952 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3953 | mval->br_startoff == |
| 3954 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3955 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3956 | mval[-1].br_state = mval->br_state; |
| 3957 | } else if (!((*n == 0) && |
| 3958 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3959 | obno))) { |
| 3960 | mval++; |
| 3961 | (*n)++; |
| 3962 | } |
| 3963 | *map = mval; |
| 3964 | } |
| 3965 | |
| 3966 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3967 | * Map file blocks to filesystem blocks without allocation. |
| 3968 | */ |
| 3969 | int |
| 3970 | xfs_bmapi_read( |
| 3971 | struct xfs_inode *ip, |
| 3972 | xfs_fileoff_t bno, |
| 3973 | xfs_filblks_t len, |
| 3974 | struct xfs_bmbt_irec *mval, |
| 3975 | int *nmap, |
| 3976 | int flags) |
| 3977 | { |
| 3978 | struct xfs_mount *mp = ip->i_mount; |
| 3979 | struct xfs_ifork *ifp; |
| 3980 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3981 | xfs_fileoff_t obno; |
| 3982 | xfs_fileoff_t end; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3983 | xfs_extnum_t idx; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3984 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3985 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3986 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3987 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3988 | |
| 3989 | ASSERT(*nmap >= 1); |
| 3990 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3991 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3992 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3993 | |
| 3994 | if (unlikely(XFS_TEST_ERROR( |
| 3995 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3996 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 3997 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 3998 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3999 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4003 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4004 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4005 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4006 | |
| 4007 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4008 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4009 | /* No CoW fork? Return a hole. */ |
| 4010 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 4011 | mval->br_startoff = bno; |
| 4012 | mval->br_startblock = HOLESTARTBLOCK; |
| 4013 | mval->br_blockcount = len; |
| 4014 | mval->br_state = XFS_EXT_NORM; |
| 4015 | *nmap = 1; |
| 4016 | return 0; |
| 4017 | } |
| 4018 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4019 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4020 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4021 | if (error) |
| 4022 | return error; |
| 4023 | } |
| 4024 | |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4025 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) |
| 4026 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4027 | end = bno + len; |
| 4028 | obno = bno; |
| 4029 | |
| 4030 | while (bno < end && n < *nmap) { |
| 4031 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4032 | if (eof) |
| 4033 | got.br_startoff = end; |
| 4034 | if (got.br_startoff > bno) { |
| 4035 | /* Reading in a hole. */ |
| 4036 | mval->br_startoff = bno; |
| 4037 | mval->br_startblock = HOLESTARTBLOCK; |
| 4038 | mval->br_blockcount = |
| 4039 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4040 | mval->br_state = XFS_EXT_NORM; |
| 4041 | bno += mval->br_blockcount; |
| 4042 | len -= mval->br_blockcount; |
| 4043 | mval++; |
| 4044 | n++; |
| 4045 | continue; |
| 4046 | } |
| 4047 | |
| 4048 | /* set up the extent map to return. */ |
| 4049 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4050 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4051 | |
| 4052 | /* If we're done, stop now. */ |
| 4053 | if (bno >= end || n >= *nmap) |
| 4054 | break; |
| 4055 | |
| 4056 | /* Else go on to the next record. */ |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4057 | if (!xfs_iext_get_extent(ifp, ++idx, &got)) |
| 4058 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4059 | } |
| 4060 | *nmap = n; |
| 4061 | return 0; |
| 4062 | } |
| 4063 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 4064 | /* |
| 4065 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 4066 | * global pool and the extent inserted into the inode in-core extent tree. |
| 4067 | * |
| 4068 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 4069 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 4070 | * to the extent record that was inserted to the inode fork. |
| 4071 | * |
| 4072 | * Note that the allocated extent may have been merged with contiguous extents |
| 4073 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 4074 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 4075 | * look up the updated record in the inode fork. |
| 4076 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 4077 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4078 | xfs_bmapi_reserve_delalloc( |
| 4079 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4080 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4081 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4082 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4083 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4084 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4085 | xfs_extnum_t *lastx, |
| 4086 | int eof) |
| 4087 | { |
| 4088 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4089 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4090 | xfs_extlen_t alen; |
| 4091 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4092 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4093 | xfs_extlen_t extsz; |
| 4094 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4095 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4096 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4097 | /* |
| 4098 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 4099 | * tag the inode before we return. |
| 4100 | */ |
| 4101 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4102 | if (!eof) |
| 4103 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4104 | if (prealloc && alen >= len) |
| 4105 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4106 | |
| 4107 | /* Figure out the extent size, adjust alen */ |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 4108 | if (whichfork == XFS_COW_FORK) |
| 4109 | extsz = xfs_get_cowextsz_hint(ip); |
| 4110 | else |
| 4111 | extsz = xfs_get_extsz_hint(ip); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4112 | if (extsz) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 4113 | struct xfs_bmbt_irec prev; |
| 4114 | |
| 4115 | if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev)) |
| 4116 | prev.br_startoff = NULLFILEOFF; |
| 4117 | |
| 4118 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4119 | 1, 0, &aoff, &alen); |
| 4120 | ASSERT(!error); |
| 4121 | } |
| 4122 | |
| 4123 | if (rt) |
| 4124 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4125 | |
| 4126 | /* |
| 4127 | * Make a transaction-less quota reservation for delayed allocation |
| 4128 | * blocks. This number gets adjusted later. We return if we haven't |
| 4129 | * allocated blocks already inside this loop. |
| 4130 | */ |
| 4131 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4132 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4133 | if (error) |
| 4134 | return error; |
| 4135 | |
| 4136 | /* |
| 4137 | * Split changing sb for alen and indlen since they could be coming |
| 4138 | * from different places. |
| 4139 | */ |
| 4140 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4141 | ASSERT(indlen > 0); |
| 4142 | |
| 4143 | if (rt) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4144 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4145 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4146 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | if (error) |
| 4150 | goto out_unreserve_quota; |
| 4151 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4152 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4153 | if (error) |
| 4154 | goto out_unreserve_blocks; |
| 4155 | |
| 4156 | |
| 4157 | ip->i_delayed_blks += alen; |
| 4158 | |
| 4159 | got->br_startoff = aoff; |
| 4160 | got->br_startblock = nullstartblock(indlen); |
| 4161 | got->br_blockcount = alen; |
| 4162 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4163 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 4164 | xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4165 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4166 | /* |
| 4167 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 4168 | * preallocation can occur at the start or end of the extent, even when |
| 4169 | * prealloc == 0, so we must also check the aligned offset and length. |
| 4170 | */ |
| 4171 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4172 | xfs_inode_set_eofblocks_tag(ip); |
| 4173 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4174 | xfs_inode_set_cowblocks_tag(ip); |
| 4175 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4176 | return 0; |
| 4177 | |
| 4178 | out_unreserve_blocks: |
| 4179 | if (rt) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4180 | xfs_mod_frextents(mp, extsz); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4181 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4182 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4183 | out_unreserve_quota: |
| 4184 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 4185 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4186 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4187 | return error; |
| 4188 | } |
| 4189 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4190 | static int |
| 4191 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4192 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4193 | { |
| 4194 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4195 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4196 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4197 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4198 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4199 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4200 | ASSERT(bma->length > 0); |
| 4201 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4202 | /* |
| 4203 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4204 | * for in this bmap call but that wouldn't be as good. |
| 4205 | */ |
| 4206 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4207 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4208 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | 0e8d630 | 2016-11-24 11:40:32 +1100 | [diff] [blame] | 4209 | if (bma->idx) { |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4210 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4211 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4212 | } |
| 4213 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4214 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4215 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4216 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4217 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4221 | * Set the data type being allocated. For the data fork, the first data |
| 4222 | * in the file is treated differently to all other allocations. For the |
| 4223 | * attribute fork, we only need to ensure the allocated range is not on |
| 4224 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4225 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4226 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4227 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4228 | if (whichfork == XFS_DATA_FORK) { |
| 4229 | if (bma->offset == 0) |
| 4230 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4231 | else |
| 4232 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4233 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4234 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4235 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4238 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4239 | |
| 4240 | /* |
| 4241 | * Only want to do the alignment at the eof if it is userdata and |
| 4242 | * allocation length is larger than a stripe unit. |
| 4243 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4244 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4245 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4246 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4247 | if (error) |
| 4248 | return error; |
| 4249 | } |
| 4250 | |
| 4251 | error = xfs_bmap_alloc(bma); |
| 4252 | if (error) |
| 4253 | return error; |
| 4254 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4255 | if (bma->cur) |
| 4256 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4257 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4258 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4259 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4260 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4261 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4262 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4263 | } |
| 4264 | /* |
| 4265 | * Bump the number of extents we've allocated |
| 4266 | * in this call. |
| 4267 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4268 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4269 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4270 | if (bma->cur) |
| 4271 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4272 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4273 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4274 | bma->got.br_startoff = bma->offset; |
| 4275 | bma->got.br_startblock = bma->blkno; |
| 4276 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4277 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4278 | |
| 4279 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4280 | * In the data fork, a wasdelay extent has been initialized, so |
| 4281 | * shouldn't be flagged as unwritten. |
| 4282 | * |
| 4283 | * For the cow fork, however, we convert delalloc reservations |
| 4284 | * (extents allocated for speculative preallocation) to |
| 4285 | * allocated unwritten extents, and only convert the unwritten |
| 4286 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4287 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4288 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4289 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4290 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4291 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4292 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4293 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4294 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4295 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4296 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
| 4297 | whichfork, &bma->idx, &bma->cur, &bma->got, |
| 4298 | bma->firstblock, bma->dfops, &bma->logflags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4299 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4300 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4301 | if (error) |
| 4302 | return error; |
| 4303 | |
| 4304 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4305 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4306 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4307 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4308 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4309 | 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] | 4310 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4311 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4312 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4313 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4314 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4315 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4316 | return 0; |
| 4317 | } |
| 4318 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4319 | STATIC int |
| 4320 | xfs_bmapi_convert_unwritten( |
| 4321 | struct xfs_bmalloca *bma, |
| 4322 | struct xfs_bmbt_irec *mval, |
| 4323 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4324 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4325 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4326 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4327 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4328 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4329 | int error; |
| 4330 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4331 | /* check if we need to do unwritten->real conversion */ |
| 4332 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4333 | (flags & XFS_BMAPI_PREALLOC)) |
| 4334 | return 0; |
| 4335 | |
| 4336 | /* check if we need to do real->unwritten conversion */ |
| 4337 | if (mval->br_state == XFS_EXT_NORM && |
| 4338 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4339 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4340 | return 0; |
| 4341 | |
| 4342 | /* |
| 4343 | * Modify (by adding) the state flag, if writing. |
| 4344 | */ |
| 4345 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4346 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4347 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4348 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4349 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4350 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4351 | } |
| 4352 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4353 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4354 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4355 | /* |
| 4356 | * Before insertion into the bmbt, zero the range being converted |
| 4357 | * if required. |
| 4358 | */ |
| 4359 | if (flags & XFS_BMAPI_ZERO) { |
| 4360 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4361 | mval->br_blockcount); |
| 4362 | if (error) |
| 4363 | return error; |
| 4364 | } |
| 4365 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4366 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
| 4367 | &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4368 | &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4369 | /* |
| 4370 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4371 | * path because the conversion might not have done so (e.g., if the |
| 4372 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4373 | * in the transaction for the sake of fsync(), even if nothing has |
| 4374 | * changed, because fsync() will not force the log for this transaction |
| 4375 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4376 | * |
| 4377 | * Note: If we're only converting cow fork extents, there aren't |
| 4378 | * 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] | 4379 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4380 | if (whichfork != XFS_COW_FORK) |
| 4381 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4382 | if (error) |
| 4383 | return error; |
| 4384 | |
| 4385 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4386 | * Update our extent pointer, given that |
| 4387 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4388 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4389 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4390 | 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] | 4391 | |
| 4392 | /* |
| 4393 | * We may have combined previously unwritten space with written space, |
| 4394 | * so generate another request. |
| 4395 | */ |
| 4396 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4397 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4398 | return 0; |
| 4399 | } |
| 4400 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4401 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4402 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4403 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4404 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4405 | * locking problems. |
| 4406 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | * The returned value in "firstblock" from the first call in a transaction |
| 4408 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4409 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4410 | * the first call in "total"; if no allocation group has that many free |
| 4411 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4412 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4413 | int |
| 4414 | xfs_bmapi_write( |
| 4415 | struct xfs_trans *tp, /* transaction pointer */ |
| 4416 | struct xfs_inode *ip, /* incore inode */ |
| 4417 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4418 | xfs_filblks_t len, /* length to map in file */ |
| 4419 | int flags, /* XFS_BMAPI_... */ |
| 4420 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4421 | controls a.g. for allocs */ |
| 4422 | xfs_extlen_t total, /* total blocks needed */ |
| 4423 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4424 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4425 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4427 | struct xfs_mount *mp = ip->i_mount; |
| 4428 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4429 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4430 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4431 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4432 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4433 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4434 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4435 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4438 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4439 | int orig_flags; /* original flags arg value */ |
| 4440 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4441 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4442 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | |
| 4444 | orig_bno = bno; |
| 4445 | orig_len = len; |
| 4446 | orig_flags = flags; |
| 4447 | orig_mval = mval; |
| 4448 | orig_nmap = *nmap; |
| 4449 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4450 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4453 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4454 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4455 | ASSERT(tp != NULL || |
| 4456 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4457 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4458 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4459 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4460 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4461 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4462 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4463 | /* zeroing is for currently only for data extents, not metadata */ |
| 4464 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4465 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4466 | /* |
| 4467 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4468 | * but it makes no sense to do both at once. This would result in |
| 4469 | * zeroing the unwritten extent twice, but it still being an |
| 4470 | * unwritten extent.... |
| 4471 | */ |
| 4472 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4473 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | if (unlikely(XFS_TEST_ERROR( |
| 4476 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4477 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4479 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4480 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4484 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4487 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4488 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4489 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4490 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4492 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4494 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4495 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4496 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4500 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4501 | if (error) |
| 4502 | goto error0; |
| 4503 | } |
| 4504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | n = 0; |
| 4506 | end = bno + len; |
| 4507 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4508 | |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4509 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got)) |
| 4510 | eof = true; |
| 4511 | if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev)) |
| 4512 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4513 | bma.tp = tp; |
| 4514 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4515 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4516 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4517 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4518 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4521 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4522 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4523 | /* in hole or beyoned EOF? */ |
| 4524 | if (eof || bma.got.br_startoff > bno) { |
| 4525 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4526 | /* |
| 4527 | * For the COW fork we can reasonably get a |
| 4528 | * request for converting an extent that races |
| 4529 | * with other threads already having converted |
| 4530 | * part of it, as there converting COW to |
| 4531 | * regular blocks is not protected using the |
| 4532 | * IOLOCK. |
| 4533 | */ |
| 4534 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4535 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4536 | error = -EIO; |
| 4537 | goto error0; |
| 4538 | } |
| 4539 | |
| 4540 | if (eof || bno >= end) |
| 4541 | break; |
| 4542 | } else { |
| 4543 | need_alloc = true; |
| 4544 | } |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4545 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4546 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4547 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4548 | |
| 4549 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | * First, deal with the hole before the allocated space |
| 4551 | * that we found, if any. |
| 4552 | */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4553 | if (need_alloc || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4554 | bma.eof = eof; |
| 4555 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4556 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4557 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4558 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4559 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4560 | /* |
| 4561 | * There's a 32/64 bit type mismatch between the |
| 4562 | * allocation length request (which can be 64 bits in |
| 4563 | * length) and the bma length request, which is |
| 4564 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4565 | * check for 32-bit overflows and handle them here. |
| 4566 | */ |
| 4567 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4568 | bma.length = MAXEXTLEN; |
| 4569 | else |
| 4570 | bma.length = len; |
| 4571 | |
| 4572 | ASSERT(len > 0); |
| 4573 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4574 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | if (error) |
| 4576 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4577 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4578 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4579 | |
| 4580 | /* |
| 4581 | * If this is a CoW allocation, record the data in |
| 4582 | * the refcount btree for orphan recovery. |
| 4583 | */ |
| 4584 | if (whichfork == XFS_COW_FORK) { |
| 4585 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4586 | bma.blkno, bma.length); |
| 4587 | if (error) |
| 4588 | goto error0; |
| 4589 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4591 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4592 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4593 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4594 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4596 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4597 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4598 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4599 | continue; |
| 4600 | if (error) |
| 4601 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4603 | /* update the extent map to return */ |
| 4604 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | /* |
| 4607 | * If we're done, stop now. Stop when we've allocated |
| 4608 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4609 | * the transaction may get too big. |
| 4610 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4611 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4613 | |
| 4614 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4615 | bma.prev = bma.got; |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4616 | if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got)) |
| 4617 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | /* |
| 4622 | * Transform from btree to extents, give it cur. |
| 4623 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4624 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4625 | int tmp_logflags = 0; |
| 4626 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4627 | ASSERT(bma.cur); |
| 4628 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4630 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | if (error) |
| 4632 | goto error0; |
| 4633 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4636 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4637 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | error0: |
| 4640 | /* |
| 4641 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4642 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4644 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4646 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4647 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4649 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | /* |
| 4651 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4652 | * detecting a case where the data is changed, there's an error, |
| 4653 | * and it's not logged so we don't shutdown when we should. |
| 4654 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4655 | if (bma.logflags) |
| 4656 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4657 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4658 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | if (!error) { |
| 4660 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4661 | XFS_FSB_TO_AGNO(mp, *firstblock) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | XFS_FSB_TO_AGNO(mp, |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4663 | bma.cur->bc_private.b.firstblock)); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4664 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4666 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4668 | } |
| 4669 | if (!error) |
| 4670 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4671 | orig_nmap, *nmap); |
| 4672 | return error; |
| 4673 | } |
| 4674 | |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4675 | static int |
| 4676 | xfs_bmapi_remap( |
| 4677 | struct xfs_trans *tp, |
| 4678 | struct xfs_inode *ip, |
| 4679 | xfs_fileoff_t bno, |
| 4680 | xfs_filblks_t len, |
| 4681 | xfs_fsblock_t startblock, |
| 4682 | struct xfs_defer_ops *dfops) |
| 4683 | { |
| 4684 | struct xfs_mount *mp = ip->i_mount; |
| 4685 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4686 | struct xfs_btree_cur *cur = NULL; |
| 4687 | xfs_fsblock_t firstblock = NULLFSBLOCK; |
| 4688 | struct xfs_bmbt_irec got; |
| 4689 | xfs_extnum_t idx; |
| 4690 | int logflags = 0, error; |
| 4691 | |
| 4692 | ASSERT(len > 0); |
| 4693 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4694 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 4695 | |
| 4696 | if (unlikely(XFS_TEST_ERROR( |
| 4697 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4698 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 4699 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4700 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4701 | return -EFSCORRUPTED; |
| 4702 | } |
| 4703 | |
| 4704 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4705 | return -EIO; |
| 4706 | |
| 4707 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4708 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4709 | if (error) |
| 4710 | return error; |
| 4711 | } |
| 4712 | |
| 4713 | if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) { |
| 4714 | /* make sure we only reflink into a hole. */ |
| 4715 | ASSERT(got.br_startoff > bno); |
| 4716 | ASSERT(got.br_startoff - bno >= len); |
| 4717 | } |
| 4718 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4719 | ip->i_d.di_nblocks += len; |
| 4720 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4721 | |
| 4722 | if (ifp->if_flags & XFS_IFBROOT) { |
| 4723 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
| 4724 | cur->bc_private.b.firstblock = firstblock; |
| 4725 | cur->bc_private.b.dfops = dfops; |
| 4726 | cur->bc_private.b.flags = 0; |
| 4727 | } |
| 4728 | |
| 4729 | got.br_startoff = bno; |
| 4730 | got.br_startblock = startblock; |
| 4731 | got.br_blockcount = len; |
| 4732 | got.br_state = XFS_EXT_NORM; |
| 4733 | |
| 4734 | error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur, |
| 4735 | &got, &firstblock, dfops, &logflags); |
| 4736 | if (error) |
| 4737 | goto error0; |
| 4738 | |
| 4739 | if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) { |
| 4740 | int tmp_logflags = 0; |
| 4741 | |
| 4742 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4743 | &tmp_logflags, XFS_DATA_FORK); |
| 4744 | logflags |= tmp_logflags; |
| 4745 | } |
| 4746 | |
| 4747 | error0: |
| 4748 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4749 | logflags &= ~XFS_ILOG_DEXT; |
| 4750 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4751 | logflags &= ~XFS_ILOG_DBROOT; |
| 4752 | |
| 4753 | if (logflags) |
| 4754 | xfs_trans_log_inode(tp, ip, logflags); |
| 4755 | if (cur) { |
| 4756 | xfs_btree_del_cursor(cur, |
| 4757 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4758 | } |
| 4759 | return error; |
| 4760 | } |
| 4761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4763 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4764 | * indlen reservation must be shared across the two new extents that are left |
| 4765 | * behind. |
| 4766 | * |
| 4767 | * Given the original reservation and the worst case indlen for the two new |
| 4768 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4769 | * reservation fairly across the two new extents. If necessary, steal available |
| 4770 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4771 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4772 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4773 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4774 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4775 | xfs_bmap_split_indlen( |
| 4776 | xfs_filblks_t ores, /* original res. */ |
| 4777 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4778 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4779 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4780 | { |
| 4781 | xfs_filblks_t len1 = *indlen1; |
| 4782 | xfs_filblks_t len2 = *indlen2; |
| 4783 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4784 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4785 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4786 | |
| 4787 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4788 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4789 | * indlen for both new extents. |
| 4790 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4791 | if (ores < nres && avail) |
| 4792 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4793 | ores += stolen; |
| 4794 | |
| 4795 | /* nothing else to do if we've satisfied the new reservation */ |
| 4796 | if (ores >= nres) |
| 4797 | return stolen; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4798 | |
| 4799 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4800 | * We can't meet the total required reservation for the two extents. |
| 4801 | * Calculate the percent of the overall shortage between both extents |
| 4802 | * and apply this percentage to each of the requested indlen values. |
| 4803 | * This distributes the shortage fairly and reduces the chances that one |
| 4804 | * of the two extents is left with nothing when extents are repeatedly |
| 4805 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4806 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4807 | resfactor = (ores * 100); |
| 4808 | do_div(resfactor, nres); |
| 4809 | len1 *= resfactor; |
| 4810 | do_div(len1, 100); |
| 4811 | len2 *= resfactor; |
| 4812 | do_div(len2, 100); |
| 4813 | ASSERT(len1 + len2 <= ores); |
| 4814 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4815 | |
| 4816 | /* |
| 4817 | * Hand out the remainder to each extent. If one of the two reservations |
| 4818 | * is zero, we want to make sure that one gets a block first. The loop |
| 4819 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4820 | * is zero. |
| 4821 | */ |
| 4822 | ores -= (len1 + len2); |
| 4823 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4824 | if (ores && !len2 && *indlen2) { |
| 4825 | len2++; |
| 4826 | ores--; |
| 4827 | } |
| 4828 | while (ores) { |
| 4829 | if (len1 < *indlen1) { |
| 4830 | len1++; |
| 4831 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4832 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4833 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4834 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4835 | if (len2 < *indlen2) { |
| 4836 | len2++; |
| 4837 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4838 | } |
| 4839 | } |
| 4840 | |
| 4841 | *indlen1 = len1; |
| 4842 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4843 | |
| 4844 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4845 | } |
| 4846 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4847 | int |
| 4848 | xfs_bmap_del_extent_delay( |
| 4849 | struct xfs_inode *ip, |
| 4850 | int whichfork, |
| 4851 | xfs_extnum_t *idx, |
| 4852 | struct xfs_bmbt_irec *got, |
| 4853 | struct xfs_bmbt_irec *del) |
| 4854 | { |
| 4855 | struct xfs_mount *mp = ip->i_mount; |
| 4856 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4857 | struct xfs_bmbt_irec new; |
| 4858 | int64_t da_old, da_new, da_diff = 0; |
| 4859 | xfs_fileoff_t del_endoff, got_endoff; |
| 4860 | xfs_filblks_t got_indlen, new_indlen, stolen; |
| 4861 | int error = 0, state = 0; |
| 4862 | bool isrt; |
| 4863 | |
| 4864 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4865 | |
| 4866 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4867 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4868 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4869 | da_old = startblockval(got->br_startblock); |
| 4870 | da_new = 0; |
| 4871 | |
| 4872 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 4873 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4874 | ASSERT(del->br_blockcount > 0); |
| 4875 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4876 | ASSERT(got_endoff >= del_endoff); |
| 4877 | |
| 4878 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4879 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4880 | |
| 4881 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4882 | xfs_mod_frextents(mp, rtexts); |
| 4883 | } |
| 4884 | |
| 4885 | /* |
| 4886 | * Update the inode delalloc counter now and wait to update the |
| 4887 | * sb counters as we might have to borrow some blocks for the |
| 4888 | * indirect block accounting. |
| 4889 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4890 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4891 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4892 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4893 | if (error) |
| 4894 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4895 | ip->i_delayed_blks -= del->br_blockcount; |
| 4896 | |
| 4897 | if (whichfork == XFS_COW_FORK) |
| 4898 | state |= BMAP_COWFORK; |
| 4899 | |
| 4900 | if (got->br_startoff == del->br_startoff) |
| 4901 | state |= BMAP_LEFT_CONTIG; |
| 4902 | if (got_endoff == del_endoff) |
| 4903 | state |= BMAP_RIGHT_CONTIG; |
| 4904 | |
| 4905 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 4906 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 4907 | /* |
| 4908 | * Matches the whole extent. Delete the entry. |
| 4909 | */ |
| 4910 | xfs_iext_remove(ip, *idx, 1, state); |
| 4911 | --*idx; |
| 4912 | break; |
| 4913 | case BMAP_LEFT_CONTIG: |
| 4914 | /* |
| 4915 | * Deleting the first part of the extent. |
| 4916 | */ |
| 4917 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4918 | got->br_startoff = del_endoff; |
| 4919 | got->br_blockcount -= del->br_blockcount; |
| 4920 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4921 | got->br_blockcount), da_old); |
| 4922 | got->br_startblock = nullstartblock((int)da_new); |
| 4923 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4924 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4925 | break; |
| 4926 | case BMAP_RIGHT_CONTIG: |
| 4927 | /* |
| 4928 | * Deleting the last part of the extent. |
| 4929 | */ |
| 4930 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4931 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4932 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4933 | got->br_blockcount), da_old); |
| 4934 | got->br_startblock = nullstartblock((int)da_new); |
| 4935 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4936 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4937 | break; |
| 4938 | case 0: |
| 4939 | /* |
| 4940 | * Deleting the middle of the extent. |
| 4941 | * |
| 4942 | * Distribute the original indlen reservation across the two new |
| 4943 | * extents. Steal blocks from the deleted extent if necessary. |
| 4944 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4945 | * Warn if either of the new indlen reservations is zero as this |
| 4946 | * can lead to delalloc problems. |
| 4947 | */ |
| 4948 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4949 | |
| 4950 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4951 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4952 | |
| 4953 | new.br_blockcount = got_endoff - del_endoff; |
| 4954 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4955 | |
| 4956 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4957 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4958 | del->br_blockcount); |
| 4959 | |
| 4960 | got->br_startblock = nullstartblock((int)got_indlen); |
| 4961 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4962 | trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_); |
| 4963 | |
| 4964 | new.br_startoff = del_endoff; |
| 4965 | new.br_state = got->br_state; |
| 4966 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4967 | |
| 4968 | ++*idx; |
| 4969 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 4970 | |
| 4971 | da_new = got_indlen + new_indlen - stolen; |
| 4972 | del->br_blockcount -= stolen; |
| 4973 | break; |
| 4974 | } |
| 4975 | |
| 4976 | ASSERT(da_old >= da_new); |
| 4977 | da_diff = da_old - da_new; |
| 4978 | if (!isrt) |
| 4979 | da_diff += del->br_blockcount; |
| 4980 | if (da_diff) |
| 4981 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4982 | return error; |
| 4983 | } |
| 4984 | |
| 4985 | void |
| 4986 | xfs_bmap_del_extent_cow( |
| 4987 | struct xfs_inode *ip, |
| 4988 | xfs_extnum_t *idx, |
| 4989 | struct xfs_bmbt_irec *got, |
| 4990 | struct xfs_bmbt_irec *del) |
| 4991 | { |
| 4992 | struct xfs_mount *mp = ip->i_mount; |
| 4993 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4994 | struct xfs_bmbt_irec new; |
| 4995 | xfs_fileoff_t del_endoff, got_endoff; |
| 4996 | int state = BMAP_COWFORK; |
| 4997 | |
| 4998 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4999 | |
| 5000 | del_endoff = del->br_startoff + del->br_blockcount; |
| 5001 | got_endoff = got->br_startoff + got->br_blockcount; |
| 5002 | |
| 5003 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5004 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 5005 | ASSERT(del->br_blockcount > 0); |
| 5006 | ASSERT(got->br_startoff <= del->br_startoff); |
| 5007 | ASSERT(got_endoff >= del_endoff); |
| 5008 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 5009 | |
| 5010 | if (got->br_startoff == del->br_startoff) |
| 5011 | state |= BMAP_LEFT_CONTIG; |
| 5012 | if (got_endoff == del_endoff) |
| 5013 | state |= BMAP_RIGHT_CONTIG; |
| 5014 | |
| 5015 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 5016 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 5017 | /* |
| 5018 | * Matches the whole extent. Delete the entry. |
| 5019 | */ |
| 5020 | xfs_iext_remove(ip, *idx, 1, state); |
| 5021 | --*idx; |
| 5022 | break; |
| 5023 | case BMAP_LEFT_CONTIG: |
| 5024 | /* |
| 5025 | * Deleting the first part of the extent. |
| 5026 | */ |
| 5027 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5028 | got->br_startoff = del_endoff; |
| 5029 | got->br_blockcount -= del->br_blockcount; |
| 5030 | got->br_startblock = del->br_startblock + del->br_blockcount; |
| 5031 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5032 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5033 | break; |
| 5034 | case BMAP_RIGHT_CONTIG: |
| 5035 | /* |
| 5036 | * Deleting the last part of the extent. |
| 5037 | */ |
| 5038 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5039 | got->br_blockcount -= del->br_blockcount; |
| 5040 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5041 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5042 | break; |
| 5043 | case 0: |
| 5044 | /* |
| 5045 | * Deleting the middle of the extent. |
| 5046 | */ |
| 5047 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5048 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 5049 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5050 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5051 | |
| 5052 | new.br_startoff = del_endoff; |
| 5053 | new.br_blockcount = got_endoff - del_endoff; |
| 5054 | new.br_state = got->br_state; |
| 5055 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 5056 | |
| 5057 | ++*idx; |
| 5058 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 5059 | break; |
| 5060 | } |
| 5061 | } |
| 5062 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5063 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5064 | * Called by xfs_bmapi to update file extent records and the btree |
| 5065 | * after removing space (or undoing a delayed allocation). |
| 5066 | */ |
| 5067 | STATIC int /* error */ |
| 5068 | xfs_bmap_del_extent( |
| 5069 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5070 | xfs_trans_t *tp, /* current transaction pointer */ |
| 5071 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5072 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5073 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 5074 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 5075 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5076 | int whichfork, /* data or attr fork */ |
| 5077 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5078 | { |
| 5079 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 5080 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 5081 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 5082 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 5083 | int delay; /* current block is delayed allocated */ |
| 5084 | int do_fx; /* free extent at end of routine */ |
| 5085 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
| 5086 | int error; /* error return value */ |
| 5087 | int flags; /* inode logging flags */ |
| 5088 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 5089 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 5090 | int i; /* temp state */ |
| 5091 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5092 | xfs_mount_t *mp; /* mount structure */ |
| 5093 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 5094 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 5095 | /* REFERENCED */ |
| 5096 | uint qfield; /* quota field to update */ |
| 5097 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 5098 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 5099 | int state = 0; |
| 5100 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5101 | mp = ip->i_mount; |
| 5102 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5103 | |
| 5104 | if (whichfork == XFS_ATTR_FORK) |
| 5105 | state |= BMAP_ATTRFORK; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5106 | else if (whichfork == XFS_COW_FORK) |
| 5107 | state |= BMAP_COWFORK; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5108 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5109 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5110 | ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp))); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5111 | ASSERT(del->br_blockcount > 0); |
| 5112 | ep = xfs_iext_get_ext(ifp, *idx); |
| 5113 | xfs_bmbt_get_all(ep, &got); |
| 5114 | ASSERT(got.br_startoff <= del->br_startoff); |
| 5115 | del_endoff = del->br_startoff + del->br_blockcount; |
| 5116 | got_endoff = got.br_startoff + got.br_blockcount; |
| 5117 | ASSERT(got_endoff >= del_endoff); |
| 5118 | delay = isnullstartblock(got.br_startblock); |
| 5119 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
| 5120 | flags = 0; |
| 5121 | qfield = 0; |
| 5122 | error = 0; |
| 5123 | /* |
| 5124 | * If deleting a real allocation, must free up the disk space. |
| 5125 | */ |
| 5126 | if (!delay) { |
| 5127 | flags = XFS_ILOG_CORE; |
| 5128 | /* |
| 5129 | * Realtime allocation. Free it and record di_nblocks update. |
| 5130 | */ |
| 5131 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 5132 | xfs_fsblock_t bno; |
| 5133 | xfs_filblks_t len; |
| 5134 | |
| 5135 | ASSERT(do_mod(del->br_blockcount, |
| 5136 | mp->m_sb.sb_rextsize) == 0); |
| 5137 | ASSERT(do_mod(del->br_startblock, |
| 5138 | mp->m_sb.sb_rextsize) == 0); |
| 5139 | bno = del->br_startblock; |
| 5140 | len = del->br_blockcount; |
| 5141 | do_div(bno, mp->m_sb.sb_rextsize); |
| 5142 | do_div(len, mp->m_sb.sb_rextsize); |
| 5143 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 5144 | if (error) |
| 5145 | goto done; |
| 5146 | do_fx = 0; |
| 5147 | nblks = len * mp->m_sb.sb_rextsize; |
| 5148 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 5149 | } |
| 5150 | /* |
| 5151 | * Ordinary allocation. |
| 5152 | */ |
| 5153 | else { |
| 5154 | do_fx = 1; |
| 5155 | nblks = del->br_blockcount; |
| 5156 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 5157 | } |
| 5158 | /* |
| 5159 | * Set up del_endblock and cur for later. |
| 5160 | */ |
| 5161 | del_endblock = del->br_startblock + del->br_blockcount; |
| 5162 | if (cur) { |
| 5163 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 5164 | got.br_startblock, got.br_blockcount, |
| 5165 | &i))) |
| 5166 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5167 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5168 | } |
| 5169 | da_old = da_new = 0; |
| 5170 | } else { |
| 5171 | da_old = startblockval(got.br_startblock); |
| 5172 | da_new = 0; |
| 5173 | nblks = 0; |
| 5174 | do_fx = 0; |
| 5175 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 5176 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5177 | /* |
| 5178 | * Set flag value to use in switch statement. |
| 5179 | * Left-contig is 2, right-contig is 1. |
| 5180 | */ |
| 5181 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 5182 | (got_endoff == del_endoff)) { |
| 5183 | case 3: |
| 5184 | /* |
| 5185 | * Matches the whole extent. Delete the entry. |
| 5186 | */ |
Darrick J. Wong | 4862cfe | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 5187 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5188 | xfs_iext_remove(ip, *idx, 1, |
| 5189 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 5190 | --*idx; |
| 5191 | if (delay) |
| 5192 | break; |
| 5193 | |
| 5194 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5195 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5196 | flags |= XFS_ILOG_CORE; |
| 5197 | if (!cur) { |
| 5198 | flags |= xfs_ilog_fext(whichfork); |
| 5199 | break; |
| 5200 | } |
| 5201 | if ((error = xfs_btree_delete(cur, &i))) |
| 5202 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5203 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5204 | break; |
| 5205 | |
| 5206 | case 2: |
| 5207 | /* |
| 5208 | * Deleting the first part of the extent. |
| 5209 | */ |
| 5210 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5211 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 5212 | temp = got.br_blockcount - del->br_blockcount; |
| 5213 | xfs_bmbt_set_blockcount(ep, temp); |
| 5214 | if (delay) { |
| 5215 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 5216 | da_old); |
| 5217 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 5218 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5219 | da_new = temp; |
| 5220 | break; |
| 5221 | } |
| 5222 | xfs_bmbt_set_startblock(ep, del_endblock); |
| 5223 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5224 | if (!cur) { |
| 5225 | flags |= xfs_ilog_fext(whichfork); |
| 5226 | break; |
| 5227 | } |
| 5228 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 5229 | got.br_blockcount - del->br_blockcount, |
| 5230 | got.br_state))) |
| 5231 | goto done; |
| 5232 | break; |
| 5233 | |
| 5234 | case 1: |
| 5235 | /* |
| 5236 | * Deleting the last part of the extent. |
| 5237 | */ |
| 5238 | temp = got.br_blockcount - del->br_blockcount; |
| 5239 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5240 | xfs_bmbt_set_blockcount(ep, temp); |
| 5241 | if (delay) { |
| 5242 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 5243 | da_old); |
| 5244 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 5245 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5246 | da_new = temp; |
| 5247 | break; |
| 5248 | } |
| 5249 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5250 | if (!cur) { |
| 5251 | flags |= xfs_ilog_fext(whichfork); |
| 5252 | break; |
| 5253 | } |
| 5254 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 5255 | got.br_startblock, |
| 5256 | got.br_blockcount - del->br_blockcount, |
| 5257 | got.br_state))) |
| 5258 | goto done; |
| 5259 | break; |
| 5260 | |
| 5261 | case 0: |
| 5262 | /* |
| 5263 | * Deleting the middle of the extent. |
| 5264 | */ |
| 5265 | temp = del->br_startoff - got.br_startoff; |
| 5266 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5267 | xfs_bmbt_set_blockcount(ep, temp); |
| 5268 | new.br_startoff = del_endoff; |
| 5269 | temp2 = got_endoff - del_endoff; |
| 5270 | new.br_blockcount = temp2; |
| 5271 | new.br_state = got.br_state; |
| 5272 | if (!delay) { |
| 5273 | new.br_startblock = del_endblock; |
| 5274 | flags |= XFS_ILOG_CORE; |
| 5275 | if (cur) { |
| 5276 | if ((error = xfs_bmbt_update(cur, |
| 5277 | got.br_startoff, |
| 5278 | got.br_startblock, temp, |
| 5279 | got.br_state))) |
| 5280 | goto done; |
| 5281 | if ((error = xfs_btree_increment(cur, 0, &i))) |
| 5282 | goto done; |
| 5283 | cur->bc_rec.b = new; |
| 5284 | error = xfs_btree_insert(cur, &i); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5285 | if (error && error != -ENOSPC) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5286 | goto done; |
| 5287 | /* |
| 5288 | * If get no-space back from btree insert, |
| 5289 | * it tried a split, and we have a zero |
| 5290 | * block reservation. |
| 5291 | * Fix up our state and return the error. |
| 5292 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5293 | if (error == -ENOSPC) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5294 | /* |
| 5295 | * Reset the cursor, don't trust |
| 5296 | * it after any insert operation. |
| 5297 | */ |
| 5298 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 5299 | got.br_startoff, |
| 5300 | got.br_startblock, |
| 5301 | temp, &i))) |
| 5302 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5303 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 5304 | i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5305 | /* |
| 5306 | * Update the btree record back |
| 5307 | * to the original value. |
| 5308 | */ |
| 5309 | if ((error = xfs_bmbt_update(cur, |
| 5310 | got.br_startoff, |
| 5311 | got.br_startblock, |
| 5312 | got.br_blockcount, |
| 5313 | got.br_state))) |
| 5314 | goto done; |
| 5315 | /* |
| 5316 | * Reset the extent record back |
| 5317 | * to the original value. |
| 5318 | */ |
| 5319 | xfs_bmbt_set_blockcount(ep, |
| 5320 | got.br_blockcount); |
| 5321 | flags = 0; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5322 | error = -ENOSPC; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5323 | goto done; |
| 5324 | } |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5325 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5326 | } else |
| 5327 | flags |= xfs_ilog_fext(whichfork); |
| 5328 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5329 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5330 | } else { |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5331 | xfs_filblks_t stolen; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5332 | ASSERT(whichfork == XFS_DATA_FORK); |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5333 | |
| 5334 | /* |
| 5335 | * Distribute the original indlen reservation across the |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5336 | * two new extents. Steal blocks from the deleted extent |
| 5337 | * if necessary. Stealing blocks simply fudges the |
| 5338 | * fdblocks accounting in xfs_bunmapi(). |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5339 | */ |
| 5340 | temp = xfs_bmap_worst_indlen(ip, got.br_blockcount); |
| 5341 | temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5342 | stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2, |
| 5343 | del->br_blockcount); |
| 5344 | da_new = temp + temp2 - stolen; |
| 5345 | del->br_blockcount -= stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5346 | |
| 5347 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5348 | * Set the reservation for each extent. Warn if either |
| 5349 | * is zero as this can lead to delalloc problems. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5350 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5351 | WARN_ON_ONCE(!temp || !temp2); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5352 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5353 | new.br_startblock = nullstartblock((int)temp2); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5354 | } |
| 5355 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5356 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 5357 | ++*idx; |
| 5358 | break; |
| 5359 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5360 | |
| 5361 | /* remove reverse mapping */ |
| 5362 | if (!delay) { |
| 5363 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5364 | if (error) |
| 5365 | goto done; |
| 5366 | } |
| 5367 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5368 | /* |
| 5369 | * If we need to, add to list of extents to delete. |
| 5370 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5371 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5372 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5373 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5374 | if (error) |
| 5375 | goto done; |
| 5376 | } else |
| 5377 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5378 | del->br_blockcount, NULL); |
| 5379 | } |
| 5380 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5381 | /* |
| 5382 | * Adjust inode # blocks in the file. |
| 5383 | */ |
| 5384 | if (nblks) |
| 5385 | ip->i_d.di_nblocks -= nblks; |
| 5386 | /* |
| 5387 | * Adjust quota data. |
| 5388 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5389 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5390 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5391 | |
| 5392 | /* |
| 5393 | * Account for change in delayed indirect blocks. |
| 5394 | * Nothing to do for disk quota accounting here. |
| 5395 | */ |
| 5396 | ASSERT(da_old >= da_new); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 5397 | if (da_old > da_new) |
| 5398 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5399 | done: |
| 5400 | *logflagsp = flags; |
| 5401 | return error; |
| 5402 | } |
| 5403 | |
| 5404 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | * Unmap (remove) blocks from a file. |
| 5406 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5407 | * that value. If not all extents in the block range can be removed then |
| 5408 | * *done is set. |
| 5409 | */ |
| 5410 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5411 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | xfs_trans_t *tp, /* transaction pointer */ |
| 5413 | struct xfs_inode *ip, /* incore inode */ |
| 5414 | xfs_fileoff_t bno, /* starting offset to unmap */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5415 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | int flags, /* misc flags */ |
| 5417 | xfs_extnum_t nexts, /* number of extents max */ |
| 5418 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5419 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5420 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | { |
| 5422 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5423 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | int error; /* error return value */ |
| 5425 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5426 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5428 | int isrt; /* freeing in rt area */ |
| 5429 | xfs_extnum_t lastx; /* last extent index used */ |
| 5430 | int logflags; /* transaction logging flags */ |
| 5431 | xfs_extlen_t mod; /* rt extent offset */ |
| 5432 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5433 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5434 | int tmp_logflags; /* partial logging flags */ |
| 5435 | int wasdel; /* was a delayed alloc extent */ |
| 5436 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5437 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5438 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5440 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5441 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5442 | whichfork = xfs_bmapi_whichfork(flags); |
| 5443 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5445 | if (unlikely( |
| 5446 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5447 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5448 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5449 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5450 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | } |
| 5452 | mp = ip->i_mount; |
| 5453 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5454 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5455 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5456 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | ASSERT(len > 0); |
| 5458 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5461 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5462 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5463 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5464 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | return 0; |
| 5466 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5467 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5468 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | start = bno; |
| 5470 | bno = start + len - 1; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | /* |
| 5473 | * Check to see if the given block number is past the end of the |
| 5474 | * file, back up to the last block if so... |
| 5475 | */ |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5476 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) { |
| 5477 | ASSERT(lastx > 0); |
| 5478 | xfs_iext_get_extent(ifp, --lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5480 | } |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5482 | logflags = 0; |
| 5483 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5484 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5485 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5487 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | cur->bc_private.b.flags = 0; |
| 5489 | } else |
| 5490 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5491 | |
| 5492 | if (isrt) { |
| 5493 | /* |
| 5494 | * Synchronize by locking the bitmap inode. |
| 5495 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5496 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5497 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5498 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5499 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | extno = 0; |
| 5503 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5504 | (nexts == 0 || extno < nexts)) { |
| 5505 | /* |
| 5506 | * Is the found extent after a hole in which bno lives? |
| 5507 | * Just back up to the previous extent, if so. |
| 5508 | */ |
| 5509 | if (got.br_startoff > bno) { |
| 5510 | if (--lastx < 0) |
| 5511 | break; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5512 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | } |
| 5514 | /* |
| 5515 | * Is the last block of this extent before the range |
| 5516 | * we're supposed to delete? If so, we're done. |
| 5517 | */ |
| 5518 | bno = XFS_FILEOFF_MIN(bno, |
| 5519 | got.br_startoff + got.br_blockcount - 1); |
| 5520 | if (bno < start) |
| 5521 | break; |
| 5522 | /* |
| 5523 | * Then deal with the (possibly delayed) allocated space |
| 5524 | * we found. |
| 5525 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5527 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | if (got.br_startoff < start) { |
| 5529 | del.br_startoff = start; |
| 5530 | del.br_blockcount -= start - got.br_startoff; |
| 5531 | if (!wasdel) |
| 5532 | del.br_startblock += start - got.br_startoff; |
| 5533 | } |
| 5534 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5535 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5536 | sum = del.br_startblock + del.br_blockcount; |
| 5537 | if (isrt && |
| 5538 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5539 | /* |
| 5540 | * Realtime extent not lined up at the end. |
| 5541 | * The extent could have been split into written |
| 5542 | * and unwritten pieces, or we could just be |
| 5543 | * unmapping part of it. But we can't really |
| 5544 | * get rid of part of a realtime extent. |
| 5545 | */ |
| 5546 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5547 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5548 | /* |
| 5549 | * This piece is unwritten, or we're not |
| 5550 | * using unwritten extents. Skip over it. |
| 5551 | */ |
| 5552 | ASSERT(bno >= mod); |
| 5553 | bno -= mod > del.br_blockcount ? |
| 5554 | del.br_blockcount : mod; |
| 5555 | if (bno < got.br_startoff) { |
| 5556 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5557 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5558 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | } |
| 5560 | continue; |
| 5561 | } |
| 5562 | /* |
| 5563 | * It's written, turn it unwritten. |
| 5564 | * This is better than zeroing it. |
| 5565 | */ |
| 5566 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5567 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | /* |
| 5569 | * If this spans a realtime extent boundary, |
| 5570 | * chop it back to the start of the one we end at. |
| 5571 | */ |
| 5572 | if (del.br_blockcount > mod) { |
| 5573 | del.br_startoff += del.br_blockcount - mod; |
| 5574 | del.br_startblock += del.br_blockcount - mod; |
| 5575 | del.br_blockcount = mod; |
| 5576 | } |
| 5577 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5578 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5579 | whichfork, &lastx, &cur, &del, |
| 5580 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5581 | if (error) |
| 5582 | goto error0; |
| 5583 | goto nodelete; |
| 5584 | } |
| 5585 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5586 | /* |
| 5587 | * Realtime extent is lined up at the end but not |
| 5588 | * at the front. We'll get rid of full extents if |
| 5589 | * we can. |
| 5590 | */ |
| 5591 | mod = mp->m_sb.sb_rextsize - mod; |
| 5592 | if (del.br_blockcount > mod) { |
| 5593 | del.br_blockcount -= mod; |
| 5594 | del.br_startoff += mod; |
| 5595 | del.br_startblock += mod; |
| 5596 | } else if ((del.br_startoff == start && |
| 5597 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5598 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5599 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | /* |
| 5601 | * Can't make it unwritten. There isn't |
| 5602 | * a full extent here so just skip it. |
| 5603 | */ |
| 5604 | ASSERT(bno >= del.br_blockcount); |
| 5605 | bno -= del.br_blockcount; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5606 | if (got.br_startoff > bno && --lastx >= 0) |
| 5607 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | continue; |
| 5609 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5610 | struct xfs_bmbt_irec prev; |
| 5611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | /* |
| 5613 | * This one is already unwritten. |
| 5614 | * It must have a written left neighbor. |
| 5615 | * Unwrite the killed part of that one and |
| 5616 | * try again. |
| 5617 | */ |
| 5618 | ASSERT(lastx > 0); |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5619 | xfs_iext_get_extent(ifp, lastx - 1, &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5621 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5622 | ASSERT(del.br_startblock == |
| 5623 | prev.br_startblock + prev.br_blockcount); |
| 5624 | if (prev.br_startoff < start) { |
| 5625 | mod = start - prev.br_startoff; |
| 5626 | prev.br_blockcount -= mod; |
| 5627 | prev.br_startblock += mod; |
| 5628 | prev.br_startoff = start; |
| 5629 | } |
| 5630 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5631 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5632 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5633 | ip, whichfork, &lastx, &cur, |
| 5634 | &prev, firstblock, dfops, |
| 5635 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5636 | if (error) |
| 5637 | goto error0; |
| 5638 | goto nodelete; |
| 5639 | } else { |
| 5640 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5641 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5642 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5643 | ip, whichfork, &lastx, &cur, |
| 5644 | &del, firstblock, dfops, |
| 5645 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | if (error) |
| 5647 | goto error0; |
| 5648 | goto nodelete; |
| 5649 | } |
| 5650 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | /* |
| 5653 | * If it's the case where the directory code is running |
| 5654 | * with no block reservation, and the deleted block is in |
| 5655 | * the middle of its extent, and the resulting insert |
| 5656 | * of an extent would cause transformation to btree format, |
| 5657 | * then reject it. The calling code will then swap |
| 5658 | * blocks around instead. |
| 5659 | * We have to do this now, rather than waiting for the |
| 5660 | * conversion to btree format, since the transaction |
| 5661 | * will be dirty. |
| 5662 | */ |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5663 | if (!wasdel && tp->t_blk_res == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5665 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ |
| 5666 | XFS_IFORK_MAXEXT(ip, whichfork) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | del.br_startoff > got.br_startoff && |
| 5668 | del.br_startoff + del.br_blockcount < |
| 5669 | got.br_startoff + got.br_blockcount) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5670 | error = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | goto error0; |
| 5672 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5673 | |
| 5674 | /* |
| 5675 | * Unreserve quota and update realtime free space, if |
| 5676 | * appropriate. If delayed allocation, update the inode delalloc |
| 5677 | * counter now and wait to update the sb counters as |
| 5678 | * xfs_bmap_del_extent() might need to borrow some blocks. |
| 5679 | */ |
| 5680 | if (wasdel) { |
| 5681 | ASSERT(startblockval(del.br_startblock) > 0); |
| 5682 | if (isrt) { |
| 5683 | xfs_filblks_t rtexts; |
| 5684 | |
| 5685 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5686 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5687 | xfs_mod_frextents(mp, (int64_t)rtexts); |
| 5688 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5689 | ip, -((long)del.br_blockcount), 0, |
| 5690 | XFS_QMOPT_RES_RTBLKS); |
| 5691 | } else { |
| 5692 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5693 | ip, -((long)del.br_blockcount), 0, |
| 5694 | XFS_QMOPT_RES_REGBLKS); |
| 5695 | } |
| 5696 | ip->i_delayed_blks -= del.br_blockcount; |
| 5697 | if (cur) |
| 5698 | cur->bc_private.b.flags |= |
| 5699 | XFS_BTCUR_BPRV_WASDEL; |
| 5700 | } else if (cur) |
| 5701 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5702 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5703 | error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del, |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5704 | &tmp_logflags, whichfork, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | logflags |= tmp_logflags; |
| 5706 | if (error) |
| 5707 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5708 | |
| 5709 | if (!isrt && wasdel) |
| 5710 | xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false); |
| 5711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | bno = del.br_startoff - 1; |
| 5713 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | /* |
| 5715 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5718 | if (lastx >= 0) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5719 | xfs_iext_get_extent(ifp, lastx, &got); |
| 5720 | if (got.br_startoff > bno && --lastx >= 0) |
| 5721 | xfs_iext_get_extent(ifp, lastx, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | extno++; |
| 5724 | } |
| 5725 | } |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5726 | if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0) |
| 5727 | *rlen = 0; |
| 5728 | else |
| 5729 | *rlen = bno - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | /* |
| 5732 | * Convert to a btree if necessary. |
| 5733 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5734 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5736 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | &cur, 0, &tmp_logflags, whichfork); |
| 5738 | logflags |= tmp_logflags; |
| 5739 | if (error) |
| 5740 | goto error0; |
| 5741 | } |
| 5742 | /* |
| 5743 | * transform from btree to extents, give it cur |
| 5744 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5745 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | ASSERT(cur != NULL); |
| 5747 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5748 | whichfork); |
| 5749 | logflags |= tmp_logflags; |
| 5750 | if (error) |
| 5751 | goto error0; |
| 5752 | } |
| 5753 | /* |
| 5754 | * transform from extents to local? |
| 5755 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | error = 0; |
| 5757 | error0: |
| 5758 | /* |
| 5759 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5760 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5762 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5763 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5764 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5765 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5767 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | /* |
| 5769 | * Log inode even in the error case, if the transaction |
| 5770 | * is dirty we'll need to shut down the filesystem. |
| 5771 | */ |
| 5772 | if (logflags) |
| 5773 | xfs_trans_log_inode(tp, ip, logflags); |
| 5774 | if (cur) { |
| 5775 | if (!error) { |
| 5776 | *firstblock = cur->bc_private.b.firstblock; |
| 5777 | cur->bc_private.b.allocated = 0; |
| 5778 | } |
| 5779 | xfs_btree_del_cursor(cur, |
| 5780 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5781 | } |
| 5782 | return error; |
| 5783 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5784 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5785 | /* Unmap a range of a file. */ |
| 5786 | int |
| 5787 | xfs_bunmapi( |
| 5788 | xfs_trans_t *tp, |
| 5789 | struct xfs_inode *ip, |
| 5790 | xfs_fileoff_t bno, |
| 5791 | xfs_filblks_t len, |
| 5792 | int flags, |
| 5793 | xfs_extnum_t nexts, |
| 5794 | xfs_fsblock_t *firstblock, |
| 5795 | struct xfs_defer_ops *dfops, |
| 5796 | int *done) |
| 5797 | { |
| 5798 | int error; |
| 5799 | |
| 5800 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5801 | dfops); |
| 5802 | *done = (len == 0); |
| 5803 | return error; |
| 5804 | } |
| 5805 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5806 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5807 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5808 | * extent that precedes the target hole of the shift. |
| 5809 | */ |
| 5810 | STATIC bool |
| 5811 | xfs_bmse_can_merge( |
| 5812 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5813 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5814 | xfs_fileoff_t shift) /* shift fsb */ |
| 5815 | { |
| 5816 | xfs_fileoff_t startoff; |
| 5817 | |
| 5818 | startoff = got->br_startoff - shift; |
| 5819 | |
| 5820 | /* |
| 5821 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5822 | * the preceding extent. |
| 5823 | */ |
| 5824 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5825 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5826 | (left->br_state != got->br_state) || |
| 5827 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5828 | return false; |
| 5829 | |
| 5830 | return true; |
| 5831 | } |
| 5832 | |
| 5833 | /* |
| 5834 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5835 | * hole in the file. If an extent shift would result in the extent being fully |
| 5836 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5837 | * the preceding extent rather than do the shift. |
| 5838 | * |
| 5839 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5840 | * with the provided extents via xfs_bmse_can_merge(). |
| 5841 | */ |
| 5842 | STATIC int |
| 5843 | xfs_bmse_merge( |
| 5844 | struct xfs_inode *ip, |
| 5845 | int whichfork, |
| 5846 | xfs_fileoff_t shift, /* shift fsb */ |
| 5847 | int current_ext, /* idx of gotp */ |
| 5848 | struct xfs_bmbt_rec_host *gotp, /* extent to shift */ |
| 5849 | struct xfs_bmbt_rec_host *leftp, /* preceding extent */ |
| 5850 | struct xfs_btree_cur *cur, |
| 5851 | int *logflags) /* output */ |
| 5852 | { |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5853 | struct xfs_bmbt_irec got; |
| 5854 | struct xfs_bmbt_irec left; |
| 5855 | xfs_filblks_t blockcount; |
| 5856 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5857 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5858 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5859 | xfs_bmbt_get_all(gotp, &got); |
| 5860 | xfs_bmbt_get_all(leftp, &left); |
| 5861 | blockcount = left.br_blockcount + got.br_blockcount; |
| 5862 | |
| 5863 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5864 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 5865 | ASSERT(xfs_bmse_can_merge(&left, &got, shift)); |
| 5866 | |
| 5867 | /* |
| 5868 | * Merge the in-core extents. Note that the host record pointers and |
| 5869 | * current_ext index are invalid once the extent has been removed via |
| 5870 | * xfs_iext_remove(). |
| 5871 | */ |
| 5872 | xfs_bmbt_set_blockcount(leftp, blockcount); |
| 5873 | xfs_iext_remove(ip, current_ext, 1, 0); |
| 5874 | |
| 5875 | /* |
| 5876 | * Update the on-disk extent count, the btree if necessary and log the |
| 5877 | * inode. |
| 5878 | */ |
| 5879 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5880 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5881 | *logflags |= XFS_ILOG_CORE; |
| 5882 | if (!cur) { |
| 5883 | *logflags |= XFS_ILOG_DEXT; |
| 5884 | return 0; |
| 5885 | } |
| 5886 | |
| 5887 | /* lookup and remove the extent to merge */ |
| 5888 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5889 | got.br_blockcount, &i); |
| 5890 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5891 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5892 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5893 | |
| 5894 | error = xfs_btree_delete(cur, &i); |
| 5895 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5896 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5897 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5898 | |
| 5899 | /* lookup and update size of the previous extent */ |
| 5900 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock, |
| 5901 | left.br_blockcount, &i); |
| 5902 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5903 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5904 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5905 | |
| 5906 | left.br_blockcount = blockcount; |
| 5907 | |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5908 | return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock, |
| 5909 | left.br_blockcount, left.br_state); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5910 | } |
| 5911 | |
| 5912 | /* |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5913 | * Shift a single extent. |
| 5914 | */ |
| 5915 | STATIC int |
| 5916 | xfs_bmse_shift_one( |
| 5917 | struct xfs_inode *ip, |
| 5918 | int whichfork, |
| 5919 | xfs_fileoff_t offset_shift_fsb, |
| 5920 | int *current_ext, |
| 5921 | struct xfs_bmbt_rec_host *gotp, |
| 5922 | struct xfs_btree_cur *cur, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5923 | int *logflags, |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5924 | enum shift_direction direction, |
| 5925 | struct xfs_defer_ops *dfops) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5926 | { |
| 5927 | struct xfs_ifork *ifp; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5928 | struct xfs_mount *mp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5929 | xfs_fileoff_t startoff; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5930 | struct xfs_bmbt_rec_host *adj_irecp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5931 | struct xfs_bmbt_irec got; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5932 | struct xfs_bmbt_irec adj_irec; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5933 | int error; |
| 5934 | int i; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5935 | int total_extents; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5936 | |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5937 | mp = ip->i_mount; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5938 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5939 | total_extents = xfs_iext_count(ifp); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5940 | |
| 5941 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5942 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5943 | /* delalloc extents should be prevented by caller */ |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5944 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5945 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5946 | if (direction == SHIFT_LEFT) { |
| 5947 | startoff = got.br_startoff - offset_shift_fsb; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5948 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5949 | /* |
| 5950 | * Check for merge if we've got an extent to the left, |
| 5951 | * otherwise make sure there's enough room at the start |
| 5952 | * of the file for the shift. |
| 5953 | */ |
| 5954 | if (!*current_ext) { |
| 5955 | if (got.br_startoff < offset_shift_fsb) |
| 5956 | return -EINVAL; |
| 5957 | goto update_current_ext; |
| 5958 | } |
| 5959 | /* |
| 5960 | * grab the left extent and check for a large |
| 5961 | * enough hole. |
| 5962 | */ |
| 5963 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1); |
| 5964 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5965 | |
| 5966 | if (startoff < |
| 5967 | adj_irec.br_startoff + adj_irec.br_blockcount) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5968 | return -EINVAL; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5969 | |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5970 | /* check whether to merge the extent or shift it down */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5971 | if (xfs_bmse_can_merge(&adj_irec, &got, |
| 5972 | offset_shift_fsb)) { |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5973 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
| 5974 | *current_ext, gotp, adj_irecp, |
| 5975 | cur, logflags); |
| 5976 | if (error) |
| 5977 | return error; |
| 5978 | adj_irec = got; |
| 5979 | goto update_rmap; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5980 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5981 | } else { |
| 5982 | startoff = got.br_startoff + offset_shift_fsb; |
| 5983 | /* nothing to move if this is the last extent */ |
| 5984 | if (*current_ext >= (total_extents - 1)) |
| 5985 | goto update_current_ext; |
| 5986 | /* |
| 5987 | * If this is not the last extent in the file, make sure there |
| 5988 | * is enough room between current extent and next extent for |
| 5989 | * accommodating the shift. |
| 5990 | */ |
| 5991 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1); |
| 5992 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5993 | if (startoff + got.br_blockcount > adj_irec.br_startoff) |
| 5994 | return -EINVAL; |
| 5995 | /* |
| 5996 | * Unlike a left shift (which involves a hole punch), |
| 5997 | * a right shift does not modify extent neighbors |
| 5998 | * in any way. We should never find mergeable extents |
| 5999 | * in this scenario. Check anyways and warn if we |
| 6000 | * encounter two extents that could be one. |
| 6001 | */ |
| 6002 | if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb)) |
| 6003 | WARN_ON_ONCE(1); |
| 6004 | } |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6005 | /* |
| 6006 | * Increment the extent index for the next iteration, update the start |
| 6007 | * offset of the in-core extent and update the btree if applicable. |
| 6008 | */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6009 | update_current_ext: |
| 6010 | if (direction == SHIFT_LEFT) |
| 6011 | (*current_ext)++; |
| 6012 | else |
| 6013 | (*current_ext)--; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6014 | xfs_bmbt_set_startoff(gotp, startoff); |
| 6015 | *logflags |= XFS_ILOG_CORE; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6016 | adj_irec = got; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6017 | if (!cur) { |
| 6018 | *logflags |= XFS_ILOG_DEXT; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6019 | goto update_rmap; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6020 | } |
| 6021 | |
| 6022 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 6023 | got.br_blockcount, &i); |
| 6024 | if (error) |
| 6025 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 6026 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6027 | |
| 6028 | got.br_startoff = startoff; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6029 | error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock, |
| 6030 | got.br_blockcount, got.br_state); |
| 6031 | if (error) |
| 6032 | return error; |
| 6033 | |
| 6034 | update_rmap: |
| 6035 | /* update reverse mapping */ |
| 6036 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec); |
| 6037 | if (error) |
| 6038 | return error; |
| 6039 | adj_irec.br_startoff = startoff; |
| 6040 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6041 | } |
| 6042 | |
| 6043 | /* |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6044 | * Shift extent records to the left/right to cover/create a hole. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6045 | * |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6046 | * 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] | 6047 | * @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] | 6048 | * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb |
| 6049 | * is the length by which each extent is shifted. If there is no hole to shift |
| 6050 | * the extents into, this will be considered invalid operation and we abort |
| 6051 | * immediately. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6052 | */ |
| 6053 | int |
| 6054 | xfs_bmap_shift_extents( |
| 6055 | struct xfs_trans *tp, |
| 6056 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6057 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6058 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6059 | int *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6060 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6061 | xfs_fsblock_t *firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6062 | struct xfs_defer_ops *dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6063 | enum shift_direction direction, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6064 | int num_exts) |
| 6065 | { |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6066 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6067 | struct xfs_bmbt_rec_host *gotp; |
| 6068 | struct xfs_bmbt_irec got; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6069 | struct xfs_mount *mp = ip->i_mount; |
| 6070 | struct xfs_ifork *ifp; |
| 6071 | xfs_extnum_t nexts = 0; |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6072 | xfs_extnum_t current_ext; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6073 | xfs_extnum_t total_extents; |
| 6074 | xfs_extnum_t stop_extent; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6075 | int error = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6076 | int whichfork = XFS_DATA_FORK; |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6077 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6078 | |
| 6079 | if (unlikely(XFS_TEST_ERROR( |
| 6080 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 6081 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 6082 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 6083 | XFS_ERROR_REPORT("xfs_bmap_shift_extents", |
| 6084 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 6085 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6086 | } |
| 6087 | |
| 6088 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 6089 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6090 | |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6091 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 6092 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6093 | ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT); |
| 6094 | ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6095 | |
| 6096 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6097 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6098 | /* Read in all the extents */ |
| 6099 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6100 | if (error) |
| 6101 | return error; |
| 6102 | } |
| 6103 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6104 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6105 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 6106 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6107 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6108 | cur->bc_private.b.flags = 0; |
| 6109 | } |
| 6110 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6111 | /* |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 6112 | * 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] | 6113 | * are collapsing out, so we cannot use the count of real extents here. |
| 6114 | * Instead we have to calculate it from the incore fork. |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 6115 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 6116 | total_extents = xfs_iext_count(ifp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6117 | if (total_extents == 0) { |
| 6118 | *done = 1; |
| 6119 | goto del_cursor; |
| 6120 | } |
| 6121 | |
| 6122 | /* |
| 6123 | * In case of first right shift, we need to initialize next_fsb |
| 6124 | */ |
| 6125 | if (*next_fsb == NULLFSBLOCK) { |
| 6126 | gotp = xfs_iext_get_ext(ifp, total_extents - 1); |
| 6127 | xfs_bmbt_get_all(gotp, &got); |
| 6128 | *next_fsb = got.br_startoff; |
| 6129 | if (stop_fsb > *next_fsb) { |
| 6130 | *done = 1; |
| 6131 | goto del_cursor; |
| 6132 | } |
| 6133 | } |
| 6134 | |
| 6135 | /* Lookup the extent index at which we have to stop */ |
| 6136 | if (direction == SHIFT_RIGHT) { |
| 6137 | gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent); |
| 6138 | /* Make stop_extent exclusive of shift range */ |
| 6139 | stop_extent--; |
| 6140 | } else |
| 6141 | stop_extent = total_extents; |
| 6142 | |
| 6143 | /* |
| 6144 | * Look up the extent index for the fsb where we start shifting. We can |
| 6145 | * henceforth iterate with current_ext as extent list changes are locked |
| 6146 | * out via ilock. |
| 6147 | * |
| 6148 | * gotp can be null in 2 cases: 1) if there are no extents or 2) |
| 6149 | * *next_fsb lies in a hole beyond which there are no extents. Either |
| 6150 | * way, we are done. |
| 6151 | */ |
| 6152 | gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext); |
| 6153 | if (!gotp) { |
| 6154 | *done = 1; |
| 6155 | goto del_cursor; |
| 6156 | } |
| 6157 | |
| 6158 | /* some sanity checking before we finally start shifting extents */ |
| 6159 | if ((direction == SHIFT_LEFT && current_ext >= stop_extent) || |
| 6160 | (direction == SHIFT_RIGHT && current_ext <= stop_extent)) { |
| 6161 | error = -EIO; |
| 6162 | goto del_cursor; |
| 6163 | } |
| 6164 | |
| 6165 | while (nexts++ < num_exts) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6166 | error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6167 | ¤t_ext, gotp, cur, &logflags, |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6168 | direction, dfops); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6169 | if (error) |
| 6170 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6171 | /* |
| 6172 | * If there was an extent merge during the shift, the extent |
| 6173 | * count can change. Update the total and grade the next record. |
| 6174 | */ |
| 6175 | if (direction == SHIFT_LEFT) { |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 6176 | total_extents = xfs_iext_count(ifp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6177 | stop_extent = total_extents; |
| 6178 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6179 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6180 | if (current_ext == stop_extent) { |
| 6181 | *done = 1; |
| 6182 | *next_fsb = NULLFSBLOCK; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6183 | break; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6184 | } |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6185 | gotp = xfs_iext_get_ext(ifp, current_ext); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6186 | } |
| 6187 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6188 | if (!*done) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6189 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6190 | *next_fsb = got.br_startoff; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6191 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6192 | |
| 6193 | del_cursor: |
| 6194 | if (cur) |
| 6195 | xfs_btree_del_cursor(cur, |
| 6196 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6197 | |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6198 | if (logflags) |
| 6199 | xfs_trans_log_inode(tp, ip, logflags); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6200 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6201 | return error; |
| 6202 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6203 | |
| 6204 | /* |
| 6205 | * Splits an extent into two extents at split_fsb block such that it is |
| 6206 | * the first block of the current_ext. @current_ext is a target extent |
| 6207 | * to be split. @split_fsb is a block where the extents is split. |
| 6208 | * If split_fsb lies in a hole or the first block of extents, just return 0. |
| 6209 | */ |
| 6210 | STATIC int |
| 6211 | xfs_bmap_split_extent_at( |
| 6212 | struct xfs_trans *tp, |
| 6213 | struct xfs_inode *ip, |
| 6214 | xfs_fileoff_t split_fsb, |
| 6215 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6216 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6217 | { |
| 6218 | int whichfork = XFS_DATA_FORK; |
| 6219 | struct xfs_btree_cur *cur = NULL; |
| 6220 | struct xfs_bmbt_rec_host *gotp; |
| 6221 | struct xfs_bmbt_irec got; |
| 6222 | struct xfs_bmbt_irec new; /* split extent */ |
| 6223 | struct xfs_mount *mp = ip->i_mount; |
| 6224 | struct xfs_ifork *ifp; |
| 6225 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
| 6226 | xfs_extnum_t current_ext; |
| 6227 | int error = 0; |
| 6228 | int logflags = 0; |
| 6229 | int i = 0; |
| 6230 | |
| 6231 | if (unlikely(XFS_TEST_ERROR( |
| 6232 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 6233 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 6234 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 6235 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 6236 | XFS_ERRLEVEL_LOW, mp); |
| 6237 | return -EFSCORRUPTED; |
| 6238 | } |
| 6239 | |
| 6240 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 6241 | return -EIO; |
| 6242 | |
| 6243 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6244 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6245 | /* Read in all the extents */ |
| 6246 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6247 | if (error) |
| 6248 | return error; |
| 6249 | } |
| 6250 | |
| 6251 | /* |
| 6252 | * gotp can be null in 2 cases: 1) if there are no extents |
| 6253 | * or 2) split_fsb lies in a hole beyond which there are |
| 6254 | * no extents. Either way, we are done. |
| 6255 | */ |
| 6256 | gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext); |
| 6257 | if (!gotp) |
| 6258 | return 0; |
| 6259 | |
| 6260 | xfs_bmbt_get_all(gotp, &got); |
| 6261 | |
| 6262 | /* |
| 6263 | * Check split_fsb lies in a hole or the start boundary offset |
| 6264 | * of the extent. |
| 6265 | */ |
| 6266 | if (got.br_startoff >= split_fsb) |
| 6267 | return 0; |
| 6268 | |
| 6269 | gotblkcnt = split_fsb - got.br_startoff; |
| 6270 | new.br_startoff = split_fsb; |
| 6271 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 6272 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 6273 | new.br_state = got.br_state; |
| 6274 | |
| 6275 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6276 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 6277 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6278 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6279 | cur->bc_private.b.flags = 0; |
| 6280 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 6281 | got.br_startblock, |
| 6282 | got.br_blockcount, |
| 6283 | &i); |
| 6284 | if (error) |
| 6285 | goto del_cursor; |
| 6286 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6287 | } |
| 6288 | |
| 6289 | xfs_bmbt_set_blockcount(gotp, gotblkcnt); |
| 6290 | got.br_blockcount = gotblkcnt; |
| 6291 | |
| 6292 | logflags = XFS_ILOG_CORE; |
| 6293 | if (cur) { |
| 6294 | error = xfs_bmbt_update(cur, got.br_startoff, |
| 6295 | got.br_startblock, |
| 6296 | got.br_blockcount, |
| 6297 | got.br_state); |
| 6298 | if (error) |
| 6299 | goto del_cursor; |
| 6300 | } else |
| 6301 | logflags |= XFS_ILOG_DEXT; |
| 6302 | |
| 6303 | /* Add new extent */ |
| 6304 | current_ext++; |
| 6305 | xfs_iext_insert(ip, current_ext, 1, &new, 0); |
| 6306 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 6307 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 6308 | |
| 6309 | if (cur) { |
| 6310 | error = xfs_bmbt_lookup_eq(cur, new.br_startoff, |
| 6311 | new.br_startblock, new.br_blockcount, |
| 6312 | &i); |
| 6313 | if (error) |
| 6314 | goto del_cursor; |
| 6315 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
| 6316 | cur->bc_rec.b.br_state = new.br_state; |
| 6317 | |
| 6318 | error = xfs_btree_insert(cur, &i); |
| 6319 | if (error) |
| 6320 | goto del_cursor; |
| 6321 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6322 | } |
| 6323 | |
| 6324 | /* |
| 6325 | * Convert to a btree if necessary. |
| 6326 | */ |
| 6327 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 6328 | int tmp_logflags; /* partial log flag return val */ |
| 6329 | |
| 6330 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6331 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6332 | &cur, 0, &tmp_logflags, whichfork); |
| 6333 | logflags |= tmp_logflags; |
| 6334 | } |
| 6335 | |
| 6336 | del_cursor: |
| 6337 | if (cur) { |
| 6338 | cur->bc_private.b.allocated = 0; |
| 6339 | xfs_btree_del_cursor(cur, |
| 6340 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6341 | } |
| 6342 | |
| 6343 | if (logflags) |
| 6344 | xfs_trans_log_inode(tp, ip, logflags); |
| 6345 | return error; |
| 6346 | } |
| 6347 | |
| 6348 | int |
| 6349 | xfs_bmap_split_extent( |
| 6350 | struct xfs_inode *ip, |
| 6351 | xfs_fileoff_t split_fsb) |
| 6352 | { |
| 6353 | struct xfs_mount *mp = ip->i_mount; |
| 6354 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6355 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6356 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6357 | int error; |
| 6358 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6359 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6360 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6361 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6362 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6363 | |
| 6364 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6365 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6366 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6367 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6368 | |
| 6369 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6370 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6371 | if (error) |
| 6372 | goto out; |
| 6373 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6374 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6375 | if (error) |
| 6376 | goto out; |
| 6377 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6378 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6379 | |
| 6380 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6381 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6382 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6383 | return error; |
| 6384 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6385 | |
| 6386 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6387 | static bool |
| 6388 | xfs_bmap_is_update_needed( |
| 6389 | struct xfs_bmbt_irec *bmap) |
| 6390 | { |
| 6391 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6392 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6393 | } |
| 6394 | |
| 6395 | /* Record a bmap intent. */ |
| 6396 | static int |
| 6397 | __xfs_bmap_add( |
| 6398 | struct xfs_mount *mp, |
| 6399 | struct xfs_defer_ops *dfops, |
| 6400 | enum xfs_bmap_intent_type type, |
| 6401 | struct xfs_inode *ip, |
| 6402 | int whichfork, |
| 6403 | struct xfs_bmbt_irec *bmap) |
| 6404 | { |
| 6405 | int error; |
| 6406 | struct xfs_bmap_intent *bi; |
| 6407 | |
| 6408 | trace_xfs_bmap_defer(mp, |
| 6409 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6410 | type, |
| 6411 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6412 | ip->i_ino, whichfork, |
| 6413 | bmap->br_startoff, |
| 6414 | bmap->br_blockcount, |
| 6415 | bmap->br_state); |
| 6416 | |
| 6417 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6418 | INIT_LIST_HEAD(&bi->bi_list); |
| 6419 | bi->bi_type = type; |
| 6420 | bi->bi_owner = ip; |
| 6421 | bi->bi_whichfork = whichfork; |
| 6422 | bi->bi_bmap = *bmap; |
| 6423 | |
| 6424 | error = xfs_defer_join(dfops, bi->bi_owner); |
| 6425 | if (error) { |
| 6426 | kmem_free(bi); |
| 6427 | return error; |
| 6428 | } |
| 6429 | |
| 6430 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6431 | return 0; |
| 6432 | } |
| 6433 | |
| 6434 | /* Map an extent into a file. */ |
| 6435 | int |
| 6436 | xfs_bmap_map_extent( |
| 6437 | struct xfs_mount *mp, |
| 6438 | struct xfs_defer_ops *dfops, |
| 6439 | struct xfs_inode *ip, |
| 6440 | struct xfs_bmbt_irec *PREV) |
| 6441 | { |
| 6442 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6443 | return 0; |
| 6444 | |
| 6445 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6446 | XFS_DATA_FORK, PREV); |
| 6447 | } |
| 6448 | |
| 6449 | /* Unmap an extent out of a file. */ |
| 6450 | int |
| 6451 | xfs_bmap_unmap_extent( |
| 6452 | struct xfs_mount *mp, |
| 6453 | struct xfs_defer_ops *dfops, |
| 6454 | struct xfs_inode *ip, |
| 6455 | struct xfs_bmbt_irec *PREV) |
| 6456 | { |
| 6457 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6458 | return 0; |
| 6459 | |
| 6460 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6461 | XFS_DATA_FORK, PREV); |
| 6462 | } |
| 6463 | |
| 6464 | /* |
| 6465 | * Process one of the deferred bmap operations. We pass back the |
| 6466 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6467 | */ |
| 6468 | int |
| 6469 | xfs_bmap_finish_one( |
| 6470 | struct xfs_trans *tp, |
| 6471 | struct xfs_defer_ops *dfops, |
| 6472 | struct xfs_inode *ip, |
| 6473 | enum xfs_bmap_intent_type type, |
| 6474 | int whichfork, |
| 6475 | xfs_fileoff_t startoff, |
| 6476 | xfs_fsblock_t startblock, |
| 6477 | xfs_filblks_t blockcount, |
| 6478 | xfs_exntst_t state) |
| 6479 | { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6480 | int error = 0, done; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6481 | |
| 6482 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6483 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6484 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
| 6485 | ip->i_ino, whichfork, startoff, blockcount, state); |
| 6486 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6487 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6488 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6489 | |
| 6490 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
| 6491 | XFS_ERRTAG_BMAP_FINISH_ONE, |
| 6492 | XFS_RANDOM_BMAP_FINISH_ONE)) |
| 6493 | return -EIO; |
| 6494 | |
| 6495 | switch (type) { |
| 6496 | case XFS_BMAP_MAP: |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6497 | error = xfs_bmapi_remap(tp, ip, startoff, blockcount, |
| 6498 | startblock, dfops); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6499 | break; |
| 6500 | case XFS_BMAP_UNMAP: |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6501 | error = xfs_bunmapi(tp, ip, startoff, blockcount, |
| 6502 | XFS_BMAPI_REMAP, 1, &startblock, dfops, &done); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6503 | ASSERT(done); |
| 6504 | break; |
| 6505 | default: |
| 6506 | ASSERT(0); |
| 6507 | error = -EFSCORRUPTED; |
| 6508 | } |
| 6509 | |
| 6510 | return error; |
| 6511 | } |