Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
| 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_sb.h" |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 27 | #include "xfs_defer.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 28 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_da_btree.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 30 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_btree.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 33 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_extfree_item.h" |
| 36 | #include "xfs_alloc.h" |
| 37 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 38 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 39 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_rtalloc.h" |
| 41 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_quota.h" |
| 43 | #include "xfs_trans_space.h" |
| 44 | #include "xfs_buf_item.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 45 | #include "xfs_trace.h" |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 46 | #include "xfs_symlink.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 47 | #include "xfs_attr_leaf.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 48 | #include "xfs_filestream.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 49 | #include "xfs_rmap.h" |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 50 | #include "xfs_ag_resv.h" |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 51 | #include "xfs_refcount.h" |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 52 | #include "xfs_rmap_btree.h" |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 53 | #include "xfs_icache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 57 | |
| 58 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 59 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 63 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 64 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 66 | void |
| 67 | xfs_bmap_compute_maxlevels( |
| 68 | xfs_mount_t *mp, /* file system mount structure */ |
| 69 | int whichfork) /* data or attr fork */ |
| 70 | { |
| 71 | int level; /* btree level */ |
| 72 | uint maxblocks; /* max blocks at this level */ |
| 73 | uint maxleafents; /* max leaf entries possible */ |
| 74 | int maxrootrecs; /* max records in root block */ |
| 75 | int minleafrecs; /* min records in leaf block */ |
| 76 | int minnoderecs; /* min records in node block */ |
| 77 | int sz; /* root block size */ |
| 78 | |
| 79 | /* |
| 80 | * The maximum number of extents in a file, hence the maximum |
| 81 | * number of leaf entries, is controlled by the type of di_nextents |
| 82 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 83 | * (a signed 16-bit number, xfs_aextnum_t). |
| 84 | * |
| 85 | * Note that we can no longer assume that if we are in ATTR1 that |
| 86 | * the fork offset of all the inodes will be |
| 87 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 88 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 89 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 90 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 91 | * of a minimum size available. |
| 92 | */ |
| 93 | if (whichfork == XFS_DATA_FORK) { |
| 94 | maxleafents = MAXEXTNUM; |
| 95 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 96 | } else { |
| 97 | maxleafents = MAXAEXTNUM; |
| 98 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 99 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 100 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 101 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 102 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 103 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 104 | for (level = 1; maxblocks > 1; level++) { |
| 105 | if (maxblocks <= maxrootrecs) |
| 106 | maxblocks = 1; |
| 107 | else |
| 108 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 109 | } |
| 110 | mp->m_bm_maxlevels[whichfork] = level; |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 113 | STATIC int /* error */ |
| 114 | xfs_bmbt_lookup_eq( |
| 115 | struct xfs_btree_cur *cur, |
| 116 | xfs_fileoff_t off, |
| 117 | xfs_fsblock_t bno, |
| 118 | xfs_filblks_t len, |
| 119 | int *stat) /* success/failure */ |
| 120 | { |
| 121 | cur->bc_rec.b.br_startoff = off; |
| 122 | cur->bc_rec.b.br_startblock = bno; |
| 123 | cur->bc_rec.b.br_blockcount = len; |
| 124 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 125 | } |
| 126 | |
| 127 | STATIC int /* error */ |
| 128 | xfs_bmbt_lookup_ge( |
| 129 | struct xfs_btree_cur *cur, |
| 130 | xfs_fileoff_t off, |
| 131 | xfs_fsblock_t bno, |
| 132 | xfs_filblks_t len, |
| 133 | int *stat) /* success/failure */ |
| 134 | { |
| 135 | cur->bc_rec.b.br_startoff = off; |
| 136 | cur->bc_rec.b.br_startblock = bno; |
| 137 | cur->bc_rec.b.br_blockcount = len; |
| 138 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 139 | } |
| 140 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 141 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 142 | * Check if the inode needs to be converted to btree format. |
| 143 | */ |
| 144 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 145 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 146 | return whichfork != XFS_COW_FORK && |
| 147 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 148 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 149 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Check if the inode should be converted to extent format. |
| 154 | */ |
| 155 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 156 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 157 | return whichfork != XFS_COW_FORK && |
| 158 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 159 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 160 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Update the record referred to by cur to the value given |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 165 | * by [off, bno, len, state]. |
| 166 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 167 | */ |
| 168 | STATIC int |
| 169 | xfs_bmbt_update( |
| 170 | struct xfs_btree_cur *cur, |
| 171 | xfs_fileoff_t off, |
| 172 | xfs_fsblock_t bno, |
| 173 | xfs_filblks_t len, |
| 174 | xfs_exntst_t state) |
| 175 | { |
| 176 | union xfs_btree_rec rec; |
| 177 | |
| 178 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 179 | return xfs_btree_update(cur, &rec); |
| 180 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 183 | * Compute the worst-case number of indirect blocks that will be used |
| 184 | * for ip's delayed extent of length "len". |
| 185 | */ |
| 186 | STATIC xfs_filblks_t |
| 187 | xfs_bmap_worst_indlen( |
| 188 | xfs_inode_t *ip, /* incore inode pointer */ |
| 189 | xfs_filblks_t len) /* delayed extent length */ |
| 190 | { |
| 191 | int level; /* btree level number */ |
| 192 | int maxrecs; /* maximum record count at this level */ |
| 193 | xfs_mount_t *mp; /* mount structure */ |
| 194 | xfs_filblks_t rval; /* return value */ |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 195 | xfs_filblks_t orig_len; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 196 | |
| 197 | mp = ip->i_mount; |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 198 | |
| 199 | /* Calculate the worst-case size of the bmbt. */ |
| 200 | orig_len = len; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 201 | maxrecs = mp->m_bmap_dmxr[0]; |
| 202 | for (level = 0, rval = 0; |
| 203 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 204 | level++) { |
| 205 | len += maxrecs - 1; |
| 206 | do_div(len, maxrecs); |
| 207 | rval += len; |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 208 | if (len == 1) { |
| 209 | rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 210 | level - 1; |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 211 | break; |
| 212 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 213 | if (level == 0) |
| 214 | maxrecs = mp->m_bmap_dmxr[1]; |
| 215 | } |
Darrick J. Wong | fd26a88 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 216 | |
| 217 | /* Calculate the worst-case size of the rmapbt. */ |
| 218 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 219 | rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) + |
| 220 | mp->m_rmap_maxlevels; |
| 221 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 222 | return rval; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Calculate the default attribute fork offset for newly created inodes. |
| 227 | */ |
| 228 | uint |
| 229 | xfs_default_attroffset( |
| 230 | struct xfs_inode *ip) |
| 231 | { |
| 232 | struct xfs_mount *mp = ip->i_mount; |
| 233 | uint offset; |
| 234 | |
| 235 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 236 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 237 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 238 | } else { |
| 239 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 240 | } |
| 241 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 242 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 243 | return offset; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Helper routine to reset inode di_forkoff field when switching |
| 248 | * attribute fork from local to extent format - we reset it where |
| 249 | * possible to make space available for inline data fork extents. |
| 250 | */ |
| 251 | STATIC void |
| 252 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 253 | xfs_inode_t *ip, |
| 254 | int whichfork) |
| 255 | { |
| 256 | if (whichfork == XFS_ATTR_FORK && |
| 257 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 258 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 259 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 260 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 261 | |
| 262 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 263 | ip->i_d.di_forkoff = dfl_forkoff; |
| 264 | } |
| 265 | } |
| 266 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 267 | #ifdef DEBUG |
| 268 | STATIC struct xfs_buf * |
| 269 | xfs_bmap_get_bp( |
| 270 | struct xfs_btree_cur *cur, |
| 271 | xfs_fsblock_t bno) |
| 272 | { |
| 273 | struct xfs_log_item_desc *lidp; |
| 274 | int i; |
| 275 | |
| 276 | if (!cur) |
| 277 | return NULL; |
| 278 | |
| 279 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 280 | if (!cur->bc_bufs[i]) |
| 281 | break; |
| 282 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 283 | return cur->bc_bufs[i]; |
| 284 | } |
| 285 | |
| 286 | /* Chase down all the log items to see if the bp is there */ |
| 287 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 288 | struct xfs_buf_log_item *bip; |
| 289 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 290 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 291 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 292 | return bip->bli_buf; |
| 293 | } |
| 294 | |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | STATIC void |
| 299 | xfs_check_block( |
| 300 | struct xfs_btree_block *block, |
| 301 | xfs_mount_t *mp, |
| 302 | int root, |
| 303 | short sz) |
| 304 | { |
| 305 | int i, j, dmxr; |
| 306 | __be64 *pp, *thispa; /* pointer to block address */ |
| 307 | xfs_bmbt_key_t *prevp, *keyp; |
| 308 | |
| 309 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 310 | |
| 311 | prevp = NULL; |
| 312 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 313 | dmxr = mp->m_bmap_dmxr[0]; |
| 314 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 315 | |
| 316 | if (prevp) { |
| 317 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 318 | be64_to_cpu(keyp->br_startoff)); |
| 319 | } |
| 320 | prevp = keyp; |
| 321 | |
| 322 | /* |
| 323 | * Compare the block numbers to see if there are dups. |
| 324 | */ |
| 325 | if (root) |
| 326 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 327 | else |
| 328 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 329 | |
| 330 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 331 | if (root) |
| 332 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 333 | else |
| 334 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 335 | if (*thispa == *pp) { |
| 336 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 337 | __func__, j, i, |
| 338 | (unsigned long long)be64_to_cpu(*thispa)); |
| 339 | panic("%s: ptrs are equal in node\n", |
| 340 | __func__); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Check that the extents for the inode ip are in the right order in all |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 348 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 349 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 350 | * them. The btree record ordering checks will still be done, so for such large |
| 351 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 352 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 353 | STATIC void |
| 354 | xfs_bmap_check_leaf_extents( |
| 355 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 356 | xfs_inode_t *ip, /* incore inode pointer */ |
| 357 | int whichfork) /* data or attr fork */ |
| 358 | { |
| 359 | struct xfs_btree_block *block; /* current btree block */ |
| 360 | xfs_fsblock_t bno; /* block # of "block" */ |
| 361 | xfs_buf_t *bp; /* buffer for "block" */ |
| 362 | int error; /* error return value */ |
| 363 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 364 | xfs_ifork_t *ifp; /* fork structure */ |
| 365 | int level; /* btree level, for checking */ |
| 366 | xfs_mount_t *mp; /* file system mount structure */ |
| 367 | __be64 *pp; /* pointer to block address */ |
| 368 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 369 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 370 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 371 | int bp_release = 0; |
| 372 | |
| 373 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 374 | return; |
| 375 | } |
| 376 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 377 | /* skip large extent count inodes */ |
| 378 | if (ip->i_d.di_nextents > 10000) |
| 379 | return; |
| 380 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 381 | bno = NULLFSBLOCK; |
| 382 | mp = ip->i_mount; |
| 383 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 384 | block = ifp->if_broot; |
| 385 | /* |
| 386 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 387 | */ |
| 388 | level = be16_to_cpu(block->bb_level); |
| 389 | ASSERT(level > 0); |
| 390 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 391 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 392 | bno = be64_to_cpu(*pp); |
| 393 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 394 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 395 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 396 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 397 | |
| 398 | /* |
| 399 | * Go down the tree until leaf level is reached, following the first |
| 400 | * pointer (leftmost) at each level. |
| 401 | */ |
| 402 | while (level-- > 0) { |
| 403 | /* See if buf is in cur first */ |
| 404 | bp_release = 0; |
| 405 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 406 | if (!bp) { |
| 407 | bp_release = 1; |
| 408 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 409 | XFS_BMAP_BTREE_REF, |
| 410 | &xfs_bmbt_buf_ops); |
| 411 | if (error) |
| 412 | goto error_norelse; |
| 413 | } |
| 414 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 415 | if (level == 0) |
| 416 | break; |
| 417 | |
| 418 | /* |
| 419 | * Check this block for basic sanity (increasing keys and |
| 420 | * no duplicate blocks). |
| 421 | */ |
| 422 | |
| 423 | xfs_check_block(block, mp, 0, 0); |
| 424 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 425 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 426 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 427 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 428 | if (bp_release) { |
| 429 | bp_release = 0; |
| 430 | xfs_trans_brelse(NULL, bp); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 436 | */ |
| 437 | i = 0; |
| 438 | |
| 439 | /* |
| 440 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 441 | */ |
| 442 | for (;;) { |
| 443 | xfs_fsblock_t nextbno; |
| 444 | xfs_extnum_t num_recs; |
| 445 | |
| 446 | |
| 447 | num_recs = xfs_btree_get_numrecs(block); |
| 448 | |
| 449 | /* |
| 450 | * Read-ahead the next leaf block, if any. |
| 451 | */ |
| 452 | |
| 453 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 454 | |
| 455 | /* |
| 456 | * Check all the extents to make sure they are OK. |
| 457 | * If we had a previous block, the last entry should |
| 458 | * conform with the first entry in this one. |
| 459 | */ |
| 460 | |
| 461 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 462 | if (i) { |
| 463 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 464 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 465 | xfs_bmbt_disk_get_startoff(ep)); |
| 466 | } |
| 467 | for (j = 1; j < num_recs; j++) { |
| 468 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 469 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 470 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 471 | xfs_bmbt_disk_get_startoff(nextp)); |
| 472 | ep = nextp; |
| 473 | } |
| 474 | |
| 475 | last = *ep; |
| 476 | i += num_recs; |
| 477 | if (bp_release) { |
| 478 | bp_release = 0; |
| 479 | xfs_trans_brelse(NULL, bp); |
| 480 | } |
| 481 | bno = nextbno; |
| 482 | /* |
| 483 | * If we've reached the end, stop. |
| 484 | */ |
| 485 | if (bno == NULLFSBLOCK) |
| 486 | break; |
| 487 | |
| 488 | bp_release = 0; |
| 489 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 490 | if (!bp) { |
| 491 | bp_release = 1; |
| 492 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 493 | XFS_BMAP_BTREE_REF, |
| 494 | &xfs_bmbt_buf_ops); |
| 495 | if (error) |
| 496 | goto error_norelse; |
| 497 | } |
| 498 | block = XFS_BUF_TO_BLOCK(bp); |
| 499 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 500 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 501 | return; |
| 502 | |
| 503 | error0: |
| 504 | xfs_warn(mp, "%s: at error0", __func__); |
| 505 | if (bp_release) |
| 506 | xfs_trans_brelse(NULL, bp); |
| 507 | error_norelse: |
| 508 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 509 | __func__, i); |
| 510 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 511 | return; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Add bmap trace insert entries for all the contents of the extent records. |
| 516 | */ |
| 517 | void |
| 518 | xfs_bmap_trace_exlist( |
| 519 | xfs_inode_t *ip, /* incore inode pointer */ |
| 520 | xfs_extnum_t cnt, /* count of entries in the list */ |
Eric Sandeen | c44a1f2 | 2016-12-05 12:32:00 +1100 | [diff] [blame] | 521 | int whichfork, /* data or attr or cow fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 522 | unsigned long caller_ip) |
| 523 | { |
| 524 | xfs_extnum_t idx; /* extent record index */ |
| 525 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 526 | int state = 0; |
| 527 | |
| 528 | if (whichfork == XFS_ATTR_FORK) |
| 529 | state |= BMAP_ATTRFORK; |
Eric Sandeen | c44a1f2 | 2016-12-05 12:32:00 +1100 | [diff] [blame] | 530 | else if (whichfork == XFS_COW_FORK) |
| 531 | state |= BMAP_COWFORK; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 532 | |
| 533 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 534 | ASSERT(cnt == xfs_iext_count(ifp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 535 | for (idx = 0; idx < cnt; idx++) |
Eric Sandeen | 7710517 | 2016-12-05 12:31:50 +1100 | [diff] [blame] | 536 | trace_xfs_extlist(ip, idx, state, caller_ip); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 541 | * given the caller's original parameters. Specifically check the |
| 542 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 543 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 544 | */ |
| 545 | STATIC void |
| 546 | xfs_bmap_validate_ret( |
| 547 | xfs_fileoff_t bno, |
| 548 | xfs_filblks_t len, |
| 549 | int flags, |
| 550 | xfs_bmbt_irec_t *mval, |
| 551 | int nmap, |
| 552 | int ret_nmap) |
| 553 | { |
| 554 | int i; /* index to map values */ |
| 555 | |
| 556 | ASSERT(ret_nmap <= nmap); |
| 557 | |
| 558 | for (i = 0; i < ret_nmap; i++) { |
| 559 | ASSERT(mval[i].br_blockcount > 0); |
| 560 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 561 | ASSERT(mval[i].br_startoff >= bno); |
| 562 | ASSERT(mval[i].br_blockcount <= len); |
| 563 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 564 | bno + len); |
| 565 | } else { |
| 566 | ASSERT(mval[i].br_startoff < bno + len); |
| 567 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 568 | bno); |
| 569 | } |
| 570 | ASSERT(i == 0 || |
| 571 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 572 | mval[i].br_startoff); |
| 573 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 574 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 575 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 576 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | #else |
| 581 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
| 582 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 583 | #endif /* DEBUG */ |
| 584 | |
| 585 | /* |
| 586 | * bmap free list manipulation functions |
| 587 | */ |
| 588 | |
| 589 | /* |
| 590 | * Add the extent to the list of extents to be free at transaction end. |
| 591 | * The list is maintained sorted (by block number). |
| 592 | */ |
| 593 | void |
| 594 | xfs_bmap_add_free( |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 595 | struct xfs_mount *mp, |
| 596 | struct xfs_defer_ops *dfops, |
| 597 | xfs_fsblock_t bno, |
| 598 | xfs_filblks_t len, |
| 599 | struct xfs_owner_info *oinfo) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 600 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 601 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 602 | #ifdef DEBUG |
| 603 | xfs_agnumber_t agno; |
| 604 | xfs_agblock_t agbno; |
| 605 | |
| 606 | ASSERT(bno != NULLFSBLOCK); |
| 607 | ASSERT(len > 0); |
| 608 | ASSERT(len <= MAXEXTLEN); |
| 609 | ASSERT(!isnullstartblock(bno)); |
| 610 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 611 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 612 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 613 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 614 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 615 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 616 | #endif |
| 617 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 618 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 619 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 620 | new->xefi_startblock = bno; |
| 621 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 622 | if (oinfo) |
| 623 | new->xefi_oinfo = *oinfo; |
| 624 | else |
| 625 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 626 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
| 627 | XFS_FSB_TO_AGBNO(mp, bno), len); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 628 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Inode fork format manipulation functions |
| 633 | */ |
| 634 | |
| 635 | /* |
| 636 | * Transform a btree format file with only one leaf node, where the |
| 637 | * extents list will fit in the inode, into an extents format file. |
| 638 | * Since the file extents are already in-core, all we have to do is |
| 639 | * give up the space for the btree root and pitch the leaf block. |
| 640 | */ |
| 641 | STATIC int /* error */ |
| 642 | xfs_bmap_btree_to_extents( |
| 643 | xfs_trans_t *tp, /* transaction pointer */ |
| 644 | xfs_inode_t *ip, /* incore inode pointer */ |
| 645 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 646 | int *logflagsp, /* inode logging flags */ |
| 647 | int whichfork) /* data or attr fork */ |
| 648 | { |
| 649 | /* REFERENCED */ |
| 650 | struct xfs_btree_block *cblock;/* child btree block */ |
| 651 | xfs_fsblock_t cbno; /* child block number */ |
| 652 | xfs_buf_t *cbp; /* child block's buffer */ |
| 653 | int error; /* error return value */ |
| 654 | xfs_ifork_t *ifp; /* inode fork data */ |
| 655 | xfs_mount_t *mp; /* mount point structure */ |
| 656 | __be64 *pp; /* ptr to block address */ |
| 657 | struct xfs_btree_block *rblock;/* root btree block */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 658 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 659 | |
| 660 | mp = ip->i_mount; |
| 661 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 662 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 663 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 664 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 665 | rblock = ifp->if_broot; |
| 666 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 667 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 668 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 669 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 670 | cbno = be64_to_cpu(*pp); |
| 671 | *logflagsp = 0; |
| 672 | #ifdef DEBUG |
| 673 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
| 674 | return error; |
| 675 | #endif |
| 676 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 677 | &xfs_bmbt_buf_ops); |
| 678 | if (error) |
| 679 | return error; |
| 680 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 681 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 682 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 683 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
| 684 | xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 685 | ip->i_d.di_nblocks--; |
| 686 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 687 | xfs_trans_binval(tp, cbp); |
| 688 | if (cur->bc_bufs[0] == cbp) |
| 689 | cur->bc_bufs[0] = NULL; |
| 690 | xfs_iroot_realloc(ip, -1, whichfork); |
| 691 | ASSERT(ifp->if_broot == NULL); |
| 692 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 693 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 694 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Convert an extents-format file into a btree-format file. |
| 700 | * The new file will have a root block (in the inode) and a single child block. |
| 701 | */ |
| 702 | STATIC int /* error */ |
| 703 | xfs_bmap_extents_to_btree( |
| 704 | xfs_trans_t *tp, /* transaction pointer */ |
| 705 | xfs_inode_t *ip, /* incore inode pointer */ |
| 706 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 707 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 708 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 709 | int wasdel, /* converting a delayed alloc */ |
| 710 | int *logflagsp, /* inode logging flags */ |
| 711 | int whichfork) /* data or attr fork */ |
| 712 | { |
| 713 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 714 | xfs_buf_t *abp; /* buffer for ablock */ |
| 715 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 716 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 717 | struct xfs_btree_block *block; /* btree root block */ |
| 718 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 719 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 720 | int error; /* error return value */ |
| 721 | xfs_extnum_t i, cnt; /* extent record index */ |
| 722 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 723 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 724 | xfs_mount_t *mp; /* mount structure */ |
| 725 | xfs_extnum_t nextents; /* number of file extents */ |
| 726 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 727 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 728 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 729 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 730 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 731 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 732 | |
| 733 | /* |
| 734 | * Make space in the inode incore. |
| 735 | */ |
| 736 | xfs_iroot_realloc(ip, 1, whichfork); |
| 737 | ifp->if_flags |= XFS_IFBROOT; |
| 738 | |
| 739 | /* |
| 740 | * Fill in the root. |
| 741 | */ |
| 742 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 743 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 744 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 745 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 746 | /* |
| 747 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 748 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 749 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 750 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 751 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 752 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 753 | /* |
| 754 | * Convert to a btree with two levels, one record in root. |
| 755 | */ |
| 756 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 757 | memset(&args, 0, sizeof(args)); |
| 758 | args.tp = tp; |
| 759 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 760 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 761 | args.firstblock = *firstblock; |
| 762 | if (*firstblock == NULLFSBLOCK) { |
| 763 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 764 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 765 | } else if (dfops->dop_low) { |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 766 | try_another_ag: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 767 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 768 | args.fsbno = *firstblock; |
| 769 | } else { |
| 770 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 771 | args.fsbno = *firstblock; |
| 772 | } |
| 773 | args.minlen = args.maxlen = args.prod = 1; |
| 774 | args.wasdel = wasdel; |
| 775 | *logflagsp = 0; |
| 776 | if ((error = xfs_alloc_vextent(&args))) { |
| 777 | xfs_iroot_realloc(ip, -1, whichfork); |
| 778 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 779 | return error; |
| 780 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 781 | |
| 782 | /* |
| 783 | * During a CoW operation, the allocation and bmbt updates occur in |
| 784 | * different transactions. The mapping code tries to put new bmbt |
| 785 | * blocks near extents being mapped, but the only way to guarantee this |
| 786 | * is if the alloc and the mapping happen in a single transaction that |
| 787 | * has a block reservation. That isn't the case here, so if we run out |
| 788 | * of space we'll try again with another AG. |
| 789 | */ |
| 790 | if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) && |
| 791 | args.fsbno == NULLFSBLOCK && |
| 792 | args.type == XFS_ALLOCTYPE_NEAR_BNO) { |
| 793 | dfops->dop_low = true; |
| 794 | goto try_another_ag; |
| 795 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 796 | /* |
| 797 | * Allocation can't fail, the space was reserved. |
| 798 | */ |
| 799 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 800 | ASSERT(*firstblock == NULLFSBLOCK || |
| 801 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 802 | (dfops->dop_low && |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 803 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 804 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 805 | cur->bc_private.b.allocated++; |
| 806 | ip->i_d.di_nblocks++; |
| 807 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 808 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 809 | /* |
| 810 | * Fill in the child block. |
| 811 | */ |
| 812 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 813 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 814 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 815 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 816 | XFS_BTREE_LONG_PTRS); |
| 817 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 818 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 819 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 820 | for (cnt = i = 0; i < nextents; i++) { |
| 821 | ep = xfs_iext_get_ext(ifp, i); |
| 822 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
| 823 | arp->l0 = cpu_to_be64(ep->l0); |
| 824 | arp->l1 = cpu_to_be64(ep->l1); |
| 825 | arp++; cnt++; |
| 826 | } |
| 827 | } |
| 828 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 829 | xfs_btree_set_numrecs(ablock, cnt); |
| 830 | |
| 831 | /* |
| 832 | * Fill in the root key and pointer. |
| 833 | */ |
| 834 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 835 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 836 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 837 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 838 | be16_to_cpu(block->bb_level))); |
| 839 | *pp = cpu_to_be64(args.fsbno); |
| 840 | |
| 841 | /* |
| 842 | * Do all this logging at the end so that |
| 843 | * the root is at the right level. |
| 844 | */ |
| 845 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 846 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 847 | ASSERT(*curp == NULL); |
| 848 | *curp = cur; |
| 849 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Convert a local file to an extents file. |
| 855 | * This code is out of bounds for data forks of regular files, |
| 856 | * since the file data needs to get logged so things will stay consistent. |
| 857 | * (The bmap-level manipulations are ok, though). |
| 858 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 859 | void |
| 860 | xfs_bmap_local_to_extents_empty( |
| 861 | struct xfs_inode *ip, |
| 862 | int whichfork) |
| 863 | { |
| 864 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 865 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 866 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 867 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 868 | ASSERT(ifp->if_bytes == 0); |
| 869 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 870 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 871 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 872 | ifp->if_flags &= ~XFS_IFINLINE; |
| 873 | ifp->if_flags |= XFS_IFEXTENTS; |
| 874 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 875 | } |
| 876 | |
| 877 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 878 | STATIC int /* error */ |
| 879 | xfs_bmap_local_to_extents( |
| 880 | xfs_trans_t *tp, /* transaction pointer */ |
| 881 | xfs_inode_t *ip, /* incore inode pointer */ |
| 882 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 883 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 884 | int *logflagsp, /* inode logging flags */ |
| 885 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 886 | void (*init_fn)(struct xfs_trans *tp, |
| 887 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 888 | struct xfs_inode *ip, |
| 889 | struct xfs_ifork *ifp)) |
| 890 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 891 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 892 | int flags; /* logging flags returned */ |
| 893 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 894 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 895 | xfs_buf_t *bp; /* buffer for extent block */ |
| 896 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 897 | |
| 898 | /* |
| 899 | * We don't want to deal with the case of keeping inode data inline yet. |
| 900 | * So sending the data fork of a regular inode is invalid. |
| 901 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 902 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 903 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 904 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 905 | |
| 906 | if (!ifp->if_bytes) { |
| 907 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 908 | flags = XFS_ILOG_CORE; |
| 909 | goto done; |
| 910 | } |
| 911 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 912 | flags = 0; |
| 913 | error = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 914 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
| 915 | XFS_IFINLINE); |
| 916 | memset(&args, 0, sizeof(args)); |
| 917 | args.tp = tp; |
| 918 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 919 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 920 | args.firstblock = *firstblock; |
| 921 | /* |
| 922 | * Allocate a block. We know we need only one, since the |
| 923 | * file currently fits in an inode. |
| 924 | */ |
| 925 | if (*firstblock == NULLFSBLOCK) { |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 926 | try_another_ag: |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 927 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 928 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 929 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 930 | args.fsbno = *firstblock; |
| 931 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 932 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 933 | args.total = total; |
| 934 | args.minlen = args.maxlen = args.prod = 1; |
| 935 | error = xfs_alloc_vextent(&args); |
| 936 | if (error) |
| 937 | goto done; |
| 938 | |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 939 | /* |
| 940 | * During a CoW operation, the allocation and bmbt updates occur in |
| 941 | * different transactions. The mapping code tries to put new bmbt |
| 942 | * blocks near extents being mapped, but the only way to guarantee this |
| 943 | * is if the alloc and the mapping happen in a single transaction that |
| 944 | * has a block reservation. That isn't the case here, so if we run out |
| 945 | * of space we'll try again with another AG. |
| 946 | */ |
| 947 | if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) && |
| 948 | args.fsbno == NULLFSBLOCK && |
| 949 | args.type == XFS_ALLOCTYPE_NEAR_BNO) { |
| 950 | goto try_another_ag; |
| 951 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 952 | /* Can't fail, the space was reserved. */ |
| 953 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 954 | ASSERT(args.len == 1); |
| 955 | *firstblock = args.fsbno; |
| 956 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 957 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 958 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 959 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 960 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 961 | * The callout is responsible for logging because the remote format |
| 962 | * might differ from the local format and thus we don't know how much to |
| 963 | * log here. Note that init_fn must also set the buffer log item type |
| 964 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 965 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 966 | init_fn(tp, bp, ip, ifp); |
| 967 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 968 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 969 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 970 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 971 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 972 | |
| 973 | xfs_iext_add(ifp, 0, 1); |
| 974 | ep = xfs_iext_get_ext(ifp, 0); |
| 975 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
| 976 | trace_xfs_bmap_post_update(ip, 0, |
| 977 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 978 | _THIS_IP_); |
| 979 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 980 | ip->i_d.di_nblocks = 1; |
| 981 | xfs_trans_mod_dquot_byino(tp, ip, |
| 982 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 983 | flags |= xfs_ilog_fext(whichfork); |
| 984 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 985 | done: |
| 986 | *logflagsp = flags; |
| 987 | return error; |
| 988 | } |
| 989 | |
| 990 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 992 | */ |
| 993 | STATIC int /* error */ |
| 994 | xfs_bmap_add_attrfork_btree( |
| 995 | xfs_trans_t *tp, /* transaction pointer */ |
| 996 | xfs_inode_t *ip, /* incore inode pointer */ |
| 997 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 998 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | int *flags) /* inode logging flags */ |
| 1000 | { |
| 1001 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 1002 | int error; /* error return value */ |
| 1003 | xfs_mount_t *mp; /* file system mount struct */ |
| 1004 | int stat; /* newroot status */ |
| 1005 | |
| 1006 | mp = ip->i_mount; |
| 1007 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1008 | *flags |= XFS_ILOG_DBROOT; |
| 1009 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1010 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1011 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | cur->bc_private.b.firstblock = *firstblock; |
| 1013 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 1014 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1015 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1016 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 1017 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | goto error0; |
| 1019 | if (stat == 0) { |
| 1020 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1021 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | } |
| 1023 | *firstblock = cur->bc_private.b.firstblock; |
| 1024 | cur->bc_private.b.allocated = 0; |
| 1025 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1026 | } |
| 1027 | return 0; |
| 1028 | error0: |
| 1029 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1030 | return error; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
| 1034 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 1035 | */ |
| 1036 | STATIC int /* error */ |
| 1037 | xfs_bmap_add_attrfork_extents( |
| 1038 | xfs_trans_t *tp, /* transaction pointer */ |
| 1039 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1040 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1041 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | int *flags) /* inode logging flags */ |
| 1043 | { |
| 1044 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 1045 | int error; /* error return value */ |
| 1046 | |
| 1047 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 1048 | return 0; |
| 1049 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1050 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | flags, XFS_DATA_FORK); |
| 1052 | if (cur) { |
| 1053 | cur->bc_private.b.allocated = 0; |
| 1054 | xfs_btree_del_cursor(cur, |
| 1055 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 1056 | } |
| 1057 | return error; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1061 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 1062 | * different data fork content type needs a different callout to do the |
| 1063 | * conversion. Some are basic and only require special block initialisation |
| 1064 | * callouts for the data formating, others (directories) are so specialised they |
| 1065 | * handle everything themselves. |
| 1066 | * |
| 1067 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1068 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1069 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | */ |
| 1071 | STATIC int /* error */ |
| 1072 | xfs_bmap_add_attrfork_local( |
| 1073 | xfs_trans_t *tp, /* transaction pointer */ |
| 1074 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1075 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1076 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | int *flags) /* inode logging flags */ |
| 1078 | { |
| 1079 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
| 1081 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1082 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1083 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1084 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1086 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | dargs.dp = ip; |
| 1088 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1089 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1090 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | dargs.whichfork = XFS_DATA_FORK; |
| 1092 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1093 | return xfs_dir2_sf_to_block(&dargs); |
| 1094 | } |
| 1095 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1096 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1097 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1098 | flags, XFS_DATA_FORK, |
| 1099 | xfs_symlink_local_to_remote); |
| 1100 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1101 | /* should only be called for types that support local format data */ |
| 1102 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1103 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1107 | * Convert inode from non-attributed to attributed. |
| 1108 | * Must not be in a transaction, ip must not be locked. |
| 1109 | */ |
| 1110 | int /* error code */ |
| 1111 | xfs_bmap_add_attrfork( |
| 1112 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1113 | int size, /* space new attribute needs */ |
| 1114 | int rsvd) /* xact may use reserved blks */ |
| 1115 | { |
| 1116 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1117 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1118 | xfs_mount_t *mp; /* mount structure */ |
| 1119 | xfs_trans_t *tp; /* transaction pointer */ |
| 1120 | int blks; /* space reservation */ |
| 1121 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1122 | int logflags; /* logging flags */ |
| 1123 | int error; /* error return value */ |
| 1124 | |
| 1125 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1126 | |
| 1127 | mp = ip->i_mount; |
| 1128 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1129 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1130 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1131 | |
| 1132 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1133 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1134 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1135 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1136 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1137 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1138 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1139 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1140 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1141 | if (error) |
| 1142 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1143 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1144 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1145 | if (ip->i_d.di_anextents != 0) { |
| 1146 | error = -EFSCORRUPTED; |
| 1147 | goto trans_cancel; |
| 1148 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1149 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1150 | /* |
| 1151 | * For inodes coming from pre-6.2 filesystems. |
| 1152 | */ |
| 1153 | ASSERT(ip->i_d.di_aformat == 0); |
| 1154 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1155 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1156 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1157 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1158 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1159 | |
| 1160 | switch (ip->i_d.di_format) { |
| 1161 | case XFS_DINODE_FMT_DEV: |
| 1162 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1163 | break; |
| 1164 | case XFS_DINODE_FMT_UUID: |
| 1165 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 1166 | break; |
| 1167 | case XFS_DINODE_FMT_LOCAL: |
| 1168 | case XFS_DINODE_FMT_EXTENTS: |
| 1169 | case XFS_DINODE_FMT_BTREE: |
| 1170 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1171 | if (!ip->i_d.di_forkoff) |
| 1172 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1173 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1174 | version = 2; |
| 1175 | break; |
| 1176 | default: |
| 1177 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1178 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1179 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | ASSERT(ip->i_afp == NULL); |
| 1183 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1184 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1185 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1186 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1187 | switch (ip->i_d.di_format) { |
| 1188 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1189 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1190 | &logflags); |
| 1191 | break; |
| 1192 | case XFS_DINODE_FMT_EXTENTS: |
| 1193 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1194 | &dfops, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1195 | break; |
| 1196 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1197 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1198 | &logflags); |
| 1199 | break; |
| 1200 | default: |
| 1201 | error = 0; |
| 1202 | break; |
| 1203 | } |
| 1204 | if (logflags) |
| 1205 | xfs_trans_log_inode(tp, ip, logflags); |
| 1206 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1207 | goto bmap_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1208 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1209 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1210 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1211 | |
| 1212 | spin_lock(&mp->m_sb_lock); |
| 1213 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1214 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1215 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1216 | } |
| 1217 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1218 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1219 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1220 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1221 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1222 | if (log_sb) |
| 1223 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1224 | } |
| 1225 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1226 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1227 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1228 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1229 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1230 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1231 | return error; |
| 1232 | |
| 1233 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1234 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1235 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1236 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1237 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1238 | return error; |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * Internal and external extent tree search functions. |
| 1243 | */ |
| 1244 | |
| 1245 | /* |
| 1246 | * Read in the extents to if_extents. |
| 1247 | * All inode fields are set up by caller, we just traverse the btree |
| 1248 | * and copy the records in. If the file system cannot contain unwritten |
| 1249 | * extents, the records are checked for no "state" flags. |
| 1250 | */ |
| 1251 | int /* error */ |
| 1252 | xfs_bmap_read_extents( |
| 1253 | xfs_trans_t *tp, /* transaction pointer */ |
| 1254 | xfs_inode_t *ip, /* incore inode */ |
| 1255 | int whichfork) /* data or attr fork */ |
| 1256 | { |
| 1257 | struct xfs_btree_block *block; /* current btree block */ |
| 1258 | xfs_fsblock_t bno; /* block # of "block" */ |
| 1259 | xfs_buf_t *bp; /* buffer for "block" */ |
| 1260 | int error; /* error return value */ |
| 1261 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
| 1262 | xfs_extnum_t i, j; /* index into the extents list */ |
| 1263 | xfs_ifork_t *ifp; /* fork structure */ |
| 1264 | int level; /* btree level, for checking */ |
| 1265 | xfs_mount_t *mp; /* file system mount structure */ |
| 1266 | __be64 *pp; /* pointer to block address */ |
| 1267 | /* REFERENCED */ |
| 1268 | xfs_extnum_t room; /* number of entries there's room for */ |
| 1269 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1270 | mp = ip->i_mount; |
| 1271 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1272 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 1273 | XFS_EXTFMT_INODE(ip); |
| 1274 | block = ifp->if_broot; |
| 1275 | /* |
| 1276 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1277 | */ |
| 1278 | level = be16_to_cpu(block->bb_level); |
| 1279 | ASSERT(level > 0); |
| 1280 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1281 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame^] | 1282 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1283 | /* |
| 1284 | * Go down the tree until leaf level is reached, following the first |
| 1285 | * pointer (leftmost) at each level. |
| 1286 | */ |
| 1287 | while (level-- > 0) { |
| 1288 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1289 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1290 | if (error) |
| 1291 | return error; |
| 1292 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1293 | if (level == 0) |
| 1294 | break; |
| 1295 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1296 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1297 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 1298 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1299 | xfs_trans_brelse(tp, bp); |
| 1300 | } |
| 1301 | /* |
| 1302 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1303 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1304 | room = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1305 | i = 0; |
| 1306 | /* |
| 1307 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1308 | */ |
| 1309 | for (;;) { |
| 1310 | xfs_bmbt_rec_t *frp; |
| 1311 | xfs_fsblock_t nextbno; |
| 1312 | xfs_extnum_t num_recs; |
| 1313 | xfs_extnum_t start; |
| 1314 | |
| 1315 | num_recs = xfs_btree_get_numrecs(block); |
| 1316 | if (unlikely(i + num_recs > room)) { |
| 1317 | ASSERT(i + num_recs <= room); |
| 1318 | xfs_warn(ip->i_mount, |
| 1319 | "corrupt dinode %Lu, (btree extents).", |
| 1320 | (unsigned long long) ip->i_ino); |
| 1321 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 1322 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
| 1323 | goto error0; |
| 1324 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1325 | /* |
| 1326 | * Read-ahead the next leaf block, if any. |
| 1327 | */ |
| 1328 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1329 | if (nextbno != NULLFSBLOCK) |
| 1330 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1331 | &xfs_bmbt_buf_ops); |
| 1332 | /* |
| 1333 | * Copy records into the extent records. |
| 1334 | */ |
| 1335 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 1336 | start = i; |
| 1337 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 1338 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
| 1339 | trp->l0 = be64_to_cpu(frp->l0); |
| 1340 | trp->l1 = be64_to_cpu(frp->l1); |
| 1341 | } |
| 1342 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 1343 | /* |
| 1344 | * Check all attribute bmap btree records and |
| 1345 | * any "older" data bmap btree records for a |
| 1346 | * set bit in the "extent flag" position. |
| 1347 | */ |
| 1348 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 1349 | start, num_recs))) { |
| 1350 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 1351 | XFS_ERRLEVEL_LOW, |
| 1352 | ip->i_mount); |
| 1353 | goto error0; |
| 1354 | } |
| 1355 | } |
| 1356 | xfs_trans_brelse(tp, bp); |
| 1357 | bno = nextbno; |
| 1358 | /* |
| 1359 | * If we've reached the end, stop. |
| 1360 | */ |
| 1361 | if (bno == NULLFSBLOCK) |
| 1362 | break; |
| 1363 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1364 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1365 | if (error) |
| 1366 | return error; |
| 1367 | block = XFS_BUF_TO_BLOCK(bp); |
| 1368 | } |
Darrick J. Wong | 356a322 | 2016-12-05 12:36:56 +1100 | [diff] [blame] | 1369 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) |
| 1370 | return -EFSCORRUPTED; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1371 | ASSERT(i == xfs_iext_count(ifp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1372 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
| 1373 | return 0; |
| 1374 | error0: |
| 1375 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1376 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1377 | } |
| 1378 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1379 | /* |
| 1380 | * Returns the file-relative block number of the first unused block(s) |
| 1381 | * in the file with at least "len" logically contiguous blocks free. |
| 1382 | * This is the lowest-address hole if the file has holes, else the first block |
| 1383 | * past the end of file. |
| 1384 | * Return 0 if the file is currently local (in-inode). |
| 1385 | */ |
| 1386 | int /* error */ |
| 1387 | xfs_bmap_first_unused( |
| 1388 | xfs_trans_t *tp, /* transaction pointer */ |
| 1389 | xfs_inode_t *ip, /* incore inode */ |
| 1390 | xfs_extlen_t len, /* size of hole to find */ |
| 1391 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1392 | int whichfork) /* data or attr fork */ |
| 1393 | { |
| 1394 | int error; /* error return value */ |
| 1395 | int idx; /* extent record index */ |
| 1396 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1397 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 1398 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 1399 | xfs_fileoff_t max; /* starting useful block */ |
| 1400 | xfs_fileoff_t off; /* offset for this block */ |
| 1401 | xfs_extnum_t nextents; /* number of extent entries */ |
| 1402 | |
| 1403 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1404 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1405 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 1406 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1407 | *first_unused = 0; |
| 1408 | return 0; |
| 1409 | } |
| 1410 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1411 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1412 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1413 | return error; |
| 1414 | lowest = *first_unused; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1415 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1416 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
| 1417 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
| 1418 | off = xfs_bmbt_get_startoff(ep); |
| 1419 | /* |
| 1420 | * See if the hole before this extent will work. |
| 1421 | */ |
| 1422 | if (off >= lowest + len && off - max >= len) { |
| 1423 | *first_unused = max; |
| 1424 | return 0; |
| 1425 | } |
| 1426 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 1427 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1428 | } |
| 1429 | *first_unused = max; |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
| 1433 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1434 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1435 | * last_block (input value) in the file. |
| 1436 | * This is not based on i_size, it is based on the extent records. |
| 1437 | * Returns 0 for local files, as they do not have extent records. |
| 1438 | */ |
| 1439 | int /* error */ |
| 1440 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1441 | struct xfs_trans *tp, /* transaction pointer */ |
| 1442 | struct xfs_inode *ip, /* incore inode */ |
| 1443 | xfs_fileoff_t *last_block, /* last block */ |
| 1444 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1445 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1446 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1447 | struct xfs_bmbt_irec got; |
| 1448 | xfs_extnum_t idx; |
| 1449 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1450 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1451 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1452 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1453 | *last_block = 0; |
| 1454 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1455 | case XFS_DINODE_FMT_BTREE: |
| 1456 | case XFS_DINODE_FMT_EXTENTS: |
| 1457 | break; |
| 1458 | default: |
| 1459 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1460 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1461 | |
| 1462 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1463 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1464 | if (error) |
| 1465 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1466 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1467 | |
| 1468 | if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) { |
| 1469 | if (got.br_startoff <= *last_block - 1) |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | if (xfs_iext_get_extent(ifp, idx - 1, &got)) { |
| 1474 | *last_block = got.br_startoff + got.br_blockcount; |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1479 | return 0; |
| 1480 | } |
| 1481 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1482 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1483 | xfs_bmap_last_extent( |
| 1484 | struct xfs_trans *tp, |
| 1485 | struct xfs_inode *ip, |
| 1486 | int whichfork, |
| 1487 | struct xfs_bmbt_irec *rec, |
| 1488 | int *is_empty) |
| 1489 | { |
| 1490 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1491 | int error; |
| 1492 | int nextents; |
| 1493 | |
| 1494 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1495 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1496 | if (error) |
| 1497 | return error; |
| 1498 | } |
| 1499 | |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1500 | nextents = xfs_iext_count(ifp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1501 | if (nextents == 0) { |
| 1502 | *is_empty = 1; |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 1507 | *is_empty = 0; |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | /* |
| 1512 | * Check the last inode extent to determine whether this allocation will result |
| 1513 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1514 | * blocks at the end of the file which do not start at the previous data block, |
| 1515 | * we will try to align the new blocks at stripe unit boundaries. |
| 1516 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1517 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1518 | * at, or past the EOF. |
| 1519 | */ |
| 1520 | STATIC int |
| 1521 | xfs_bmap_isaeof( |
| 1522 | struct xfs_bmalloca *bma, |
| 1523 | int whichfork) |
| 1524 | { |
| 1525 | struct xfs_bmbt_irec rec; |
| 1526 | int is_empty; |
| 1527 | int error; |
| 1528 | |
| 1529 | bma->aeof = 0; |
| 1530 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1531 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1532 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1533 | return error; |
| 1534 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1535 | if (is_empty) { |
| 1536 | bma->aeof = 1; |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1540 | /* |
| 1541 | * Check if we are allocation or past the last extent, or at least into |
| 1542 | * the last delayed allocated extent. |
| 1543 | */ |
| 1544 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1545 | (bma->offset >= rec.br_startoff && |
| 1546 | isnullstartblock(rec.br_startblock)); |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1551 | * Returns the file-relative block number of the first block past eof in |
| 1552 | * the file. This is not based on i_size, it is based on the extent records. |
| 1553 | * Returns 0 for local files, as they do not have extent records. |
| 1554 | */ |
| 1555 | int |
| 1556 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1557 | struct xfs_inode *ip, |
| 1558 | xfs_fileoff_t *last_block, |
| 1559 | int whichfork) |
| 1560 | { |
| 1561 | struct xfs_bmbt_irec rec; |
| 1562 | int is_empty; |
| 1563 | int error; |
| 1564 | |
| 1565 | *last_block = 0; |
| 1566 | |
| 1567 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1568 | return 0; |
| 1569 | |
| 1570 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1571 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1572 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1573 | |
| 1574 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1575 | if (error || is_empty) |
| 1576 | return error; |
| 1577 | |
| 1578 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * Returns whether the selected fork of the inode has exactly one |
| 1584 | * block or not. For the data fork we check this matches di_size, |
| 1585 | * implying the file's range is 0..bsize-1. |
| 1586 | */ |
| 1587 | int /* 1=>1 block, 0=>otherwise */ |
| 1588 | xfs_bmap_one_block( |
| 1589 | xfs_inode_t *ip, /* incore inode */ |
| 1590 | int whichfork) /* data or attr fork */ |
| 1591 | { |
| 1592 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
| 1593 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1594 | int rval; /* return value */ |
| 1595 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 1596 | |
| 1597 | #ifndef DEBUG |
| 1598 | if (whichfork == XFS_DATA_FORK) |
| 1599 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1600 | #endif /* !DEBUG */ |
| 1601 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1602 | return 0; |
| 1603 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1604 | return 0; |
| 1605 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1606 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 1607 | ep = xfs_iext_get_ext(ifp, 0); |
| 1608 | xfs_bmbt_get_all(ep, &s); |
| 1609 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1610 | if (rval && whichfork == XFS_DATA_FORK) |
| 1611 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1612 | return rval; |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Extent tree manipulation functions used during allocation. |
| 1617 | */ |
| 1618 | |
| 1619 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1620 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | */ |
| 1622 | STATIC int /* error */ |
| 1623 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1624 | struct xfs_bmalloca *bma, |
| 1625 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1627 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1629 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1632 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1634 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1635 | /* left is 0, right is 1, prev is 2 */ |
| 1636 | int rval=0; /* return value (logging flags) */ |
| 1637 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1638 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1639 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1640 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 1641 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1643 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1644 | xfs_extnum_t *nextents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1646 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1647 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1648 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1649 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1650 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1651 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1652 | ASSERT(bma->idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1653 | ASSERT(bma->idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1654 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1655 | ASSERT(!bma->cur || |
| 1656 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1657 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1658 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | #define LEFT r[0] |
| 1661 | #define RIGHT r[1] |
| 1662 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1664 | if (whichfork == XFS_COW_FORK) |
| 1665 | state |= BMAP_COWFORK; |
| 1666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | /* |
| 1668 | * Set up a bunch of variables to make the tests simpler. |
| 1669 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1670 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | xfs_bmbt_get_all(ep, &PREV); |
| 1672 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1673 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1674 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1675 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1676 | da_old = startblockval(PREV.br_startblock); |
| 1677 | da_new = 0; |
| 1678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | /* |
| 1680 | * Set flags determining what part of the previous delayed allocation |
| 1681 | * extent is being replaced by a real allocation. |
| 1682 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1683 | if (PREV.br_startoff == new->br_startoff) |
| 1684 | state |= BMAP_LEFT_FILLING; |
| 1685 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1686 | state |= BMAP_RIGHT_FILLING; |
| 1687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | /* |
| 1689 | * Check and set flags if this segment has a left neighbor. |
| 1690 | * Don't set contiguous if the combined extent would be too large. |
| 1691 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1692 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1693 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1694 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1695 | |
| 1696 | if (isnullstartblock(LEFT.br_startblock)) |
| 1697 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1699 | |
| 1700 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1701 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1702 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1703 | LEFT.br_state == new->br_state && |
| 1704 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1705 | state |= BMAP_LEFT_CONTIG; |
| 1706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | /* |
| 1708 | * Check and set flags if this segment has a right neighbor. |
| 1709 | * Don't set contiguous if the combined extent would be too large. |
| 1710 | * Also check for all-three-contiguous being too large. |
| 1711 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1712 | if (bma->idx < xfs_iext_count(ifp) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1713 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1714 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1715 | |
| 1716 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1717 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1719 | |
| 1720 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1721 | new_endoff == RIGHT.br_startoff && |
| 1722 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1723 | new->br_state == RIGHT.br_state && |
| 1724 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1725 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1726 | BMAP_RIGHT_FILLING)) != |
| 1727 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1728 | BMAP_RIGHT_FILLING) || |
| 1729 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1730 | <= MAXEXTLEN)) |
| 1731 | state |= BMAP_RIGHT_CONTIG; |
| 1732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | error = 0; |
| 1734 | /* |
| 1735 | * Switch out based on the FILLING and CONTIG state bits. |
| 1736 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1737 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1738 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1739 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1740 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | /* |
| 1742 | * Filling in all of a previously delayed allocation extent. |
| 1743 | * The left and right neighbors are both contiguous with new. |
| 1744 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1745 | bma->idx--; |
| 1746 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1747 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1749 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1750 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1751 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1752 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1753 | (*nextents)--; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1754 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1756 | else { |
| 1757 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1758 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1760 | RIGHT.br_blockcount, &i); |
| 1761 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1763 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1764 | error = xfs_btree_delete(bma->cur, &i); |
| 1765 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1767 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1768 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1769 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1771 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1772 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | LEFT.br_startblock, |
| 1774 | LEFT.br_blockcount + |
| 1775 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1776 | RIGHT.br_blockcount, LEFT.br_state); |
| 1777 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | goto done; |
| 1779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | break; |
| 1781 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1782 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | /* |
| 1784 | * Filling in all of a previously delayed allocation extent. |
| 1785 | * The left neighbor is contiguous, the right is not. |
| 1786 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1787 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1788 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1789 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1790 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1791 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1792 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1793 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1794 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1795 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | rval = XFS_ILOG_DEXT; |
| 1797 | else { |
| 1798 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1799 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1801 | &i); |
| 1802 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1804 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1805 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | LEFT.br_startblock, |
| 1807 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1808 | PREV.br_blockcount, LEFT.br_state); |
| 1809 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | goto done; |
| 1811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | break; |
| 1813 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1814 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | /* |
| 1816 | * Filling in all of a previously delayed allocation extent. |
| 1817 | * The right neighbor is contiguous, the left is not. |
| 1818 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1819 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 1821 | xfs_bmbt_set_blockcount(ep, |
| 1822 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1823 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1824 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1825 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1826 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | rval = XFS_ILOG_DEXT; |
| 1828 | else { |
| 1829 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1830 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1832 | RIGHT.br_blockcount, &i); |
| 1833 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1835 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1836 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | new->br_startblock, |
| 1838 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1839 | RIGHT.br_blockcount, PREV.br_state); |
| 1840 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | goto done; |
| 1842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | break; |
| 1844 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1845 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | /* |
| 1847 | * Filling in all of a previously delayed allocation extent. |
| 1848 | * Neither the left nor right neighbors are contiguous with |
| 1849 | * the new one. |
| 1850 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1851 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1853 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1854 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1855 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1856 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1858 | else { |
| 1859 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1860 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1862 | &i); |
| 1863 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1865 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1866 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1867 | error = xfs_btree_insert(bma->cur, &i); |
| 1868 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1870 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | break; |
| 1873 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1874 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | /* |
| 1876 | * Filling in the first part of a previous delayed allocation. |
| 1877 | * The left neighbor is contiguous. |
| 1878 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1879 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 1880 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | LEFT.br_blockcount + new->br_blockcount); |
| 1882 | xfs_bmbt_set_startoff(ep, |
| 1883 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1884 | 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] | 1885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1887 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1889 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | rval = XFS_ILOG_DEXT; |
| 1891 | else { |
| 1892 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1893 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1895 | &i); |
| 1896 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1898 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1899 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | LEFT.br_startblock, |
| 1901 | LEFT.br_blockcount + |
| 1902 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1903 | LEFT.br_state); |
| 1904 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | goto done; |
| 1906 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1907 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1908 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1909 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1910 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1911 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1912 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | break; |
| 1914 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1915 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | /* |
| 1917 | * Filling in the first part of a previous delayed allocation. |
| 1918 | * The left neighbor is not contiguous. |
| 1919 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1920 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1922 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1923 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1924 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1925 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1926 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1928 | else { |
| 1929 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1930 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1932 | &i); |
| 1933 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1935 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1936 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1937 | error = xfs_btree_insert(bma->cur, &i); |
| 1938 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1940 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1942 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1943 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1944 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1945 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1946 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | rval |= tmp_rval; |
| 1948 | if (error) |
| 1949 | goto done; |
| 1950 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1951 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1952 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1953 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 1954 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1955 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1956 | 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] | 1957 | break; |
| 1958 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1959 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | /* |
| 1961 | * Filling in the last part of a previous delayed allocation. |
| 1962 | * The right neighbor is contiguous with the new allocation. |
| 1963 | */ |
| 1964 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1965 | 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] | 1966 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1967 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1968 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | new->br_blockcount + RIGHT.br_blockcount, |
| 1970 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1971 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 1972 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | rval = XFS_ILOG_DEXT; |
| 1974 | else { |
| 1975 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1976 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1978 | RIGHT.br_blockcount, &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 == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1982 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | new->br_startblock, |
| 1984 | new->br_blockcount + |
| 1985 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1986 | RIGHT.br_state); |
| 1987 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | goto done; |
| 1989 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1990 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1991 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1992 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1993 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1994 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1995 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1996 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1997 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | break; |
| 1999 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2000 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | /* |
| 2002 | * Filling in the last part of a previous delayed allocation. |
| 2003 | * The right neighbor is not contiguous. |
| 2004 | */ |
| 2005 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2006 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2008 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2009 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2010 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2012 | else { |
| 2013 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2014 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2016 | &i); |
| 2017 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2019 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2020 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2021 | error = xfs_btree_insert(bma->cur, &i); |
| 2022 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2024 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2026 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2027 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2028 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2029 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2030 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | rval |= tmp_rval; |
| 2032 | if (error) |
| 2033 | goto done; |
| 2034 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2035 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2036 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2037 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 2038 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2039 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2040 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2041 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2042 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | break; |
| 2044 | |
| 2045 | case 0: |
| 2046 | /* |
| 2047 | * Filling in the middle part of a previous delayed allocation. |
| 2048 | * Contiguity is impossible here. |
| 2049 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2050 | * |
| 2051 | * We start with a delayed allocation: |
| 2052 | * |
| 2053 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 2054 | * PREV @ idx |
| 2055 | * |
| 2056 | * and we are allocating: |
| 2057 | * +rrrrrrrrrrrrrrrrr+ |
| 2058 | * new |
| 2059 | * |
| 2060 | * and we set it up for insertion as: |
| 2061 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 2062 | * new |
| 2063 | * PREV @ idx LEFT RIGHT |
| 2064 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | */ |
| 2066 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2068 | 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] | 2069 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 2070 | LEFT = *new; |
| 2071 | RIGHT.br_state = PREV.br_state; |
| 2072 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2073 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2074 | RIGHT.br_startoff = new_endoff; |
| 2075 | RIGHT.br_blockcount = temp2; |
| 2076 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2077 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2078 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2079 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2081 | else { |
| 2082 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2083 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2085 | &i); |
| 2086 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2088 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2089 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2090 | error = xfs_btree_insert(bma->cur, &i); |
| 2091 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2093 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2095 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2096 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2097 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2098 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2099 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | rval |= tmp_rval; |
| 2101 | if (error) |
| 2102 | goto done; |
| 2103 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2104 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 2105 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2106 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2107 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2108 | if (diff > 0) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2109 | error = xfs_mod_fdblocks(bma->ip->i_mount, |
| 2110 | -((int64_t)diff), false); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2111 | ASSERT(!error); |
| 2112 | if (error) |
| 2113 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2115 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2116 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2117 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2118 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2119 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 2120 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2121 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2122 | 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] | 2123 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2124 | bma->idx++; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2125 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | break; |
| 2127 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2128 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2129 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2130 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2131 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2132 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2133 | case BMAP_LEFT_CONTIG: |
| 2134 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | /* |
| 2136 | * These cases are all impossible. |
| 2137 | */ |
| 2138 | ASSERT(0); |
| 2139 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2140 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2141 | /* add reverse mapping */ |
| 2142 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 2143 | if (error) |
| 2144 | goto done; |
| 2145 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2146 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2147 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2148 | int tmp_logflags; /* partial log flag return val */ |
| 2149 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2150 | ASSERT(bma->cur == NULL); |
| 2151 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2152 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2153 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2154 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2155 | if (error) |
| 2156 | goto done; |
| 2157 | } |
| 2158 | |
| 2159 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2160 | if (da_old || da_new) { |
| 2161 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2162 | if (bma->cur) |
| 2163 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2164 | ASSERT(temp <= da_old); |
| 2165 | if (temp < da_old) |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2166 | xfs_mod_fdblocks(bma->ip->i_mount, |
| 2167 | (int64_t)(da_old - temp), false); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2171 | if (bma->cur) |
| 2172 | bma->cur->bc_private.b.allocated = 0; |
| 2173 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2174 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2176 | if (whichfork != XFS_COW_FORK) |
| 2177 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | return error; |
| 2179 | #undef LEFT |
| 2180 | #undef RIGHT |
| 2181 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2185 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | */ |
| 2187 | STATIC int /* error */ |
| 2188 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2189 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2191 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2193 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2194 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2195 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2196 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2199 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2202 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 2204 | xfs_exntst_t newext; /* new extent state */ |
| 2205 | xfs_exntst_t oldext; /* old extent state */ |
| 2206 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2207 | /* left is 0, right is 1, prev is 2 */ |
| 2208 | int rval=0; /* return value (logging flags) */ |
| 2209 | int state = 0;/* state bits, accessed thru macros */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2210 | struct xfs_mount *mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2212 | *logflagsp = 0; |
| 2213 | |
| 2214 | cur = *curp; |
| 2215 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 2216 | |
| 2217 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2218 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2219 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2220 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2221 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | #define LEFT r[0] |
| 2224 | #define RIGHT r[1] |
| 2225 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | /* |
| 2228 | * Set up a bunch of variables to make the tests simpler. |
| 2229 | */ |
| 2230 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2231 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | xfs_bmbt_get_all(ep, &PREV); |
| 2233 | newext = new->br_state; |
| 2234 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 2235 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 2236 | ASSERT(PREV.br_state == oldext); |
| 2237 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2238 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2239 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | /* |
| 2242 | * Set flags determining what part of the previous oldext allocation |
| 2243 | * extent is being replaced by a newext allocation. |
| 2244 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2245 | if (PREV.br_startoff == new->br_startoff) |
| 2246 | state |= BMAP_LEFT_FILLING; |
| 2247 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2248 | state |= BMAP_RIGHT_FILLING; |
| 2249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | /* |
| 2251 | * Check and set flags if this segment has a left neighbor. |
| 2252 | * Don't set contiguous if the combined extent would be too large. |
| 2253 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2254 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2255 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2256 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2257 | |
| 2258 | if (isnullstartblock(LEFT.br_startblock)) |
| 2259 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2261 | |
| 2262 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2263 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2264 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 2265 | LEFT.br_state == newext && |
| 2266 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2267 | state |= BMAP_LEFT_CONTIG; |
| 2268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | /* |
| 2270 | * Check and set flags if this segment has a right neighbor. |
| 2271 | * Don't set contiguous if the combined extent would be too large. |
| 2272 | * Also check for all-three-contiguous being too large. |
| 2273 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2274 | if (*idx < xfs_iext_count(&ip->i_df) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2275 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2276 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2277 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2278 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2280 | |
| 2281 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2282 | new_endoff == RIGHT.br_startoff && |
| 2283 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 2284 | newext == RIGHT.br_state && |
| 2285 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2286 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2287 | BMAP_RIGHT_FILLING)) != |
| 2288 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2289 | BMAP_RIGHT_FILLING) || |
| 2290 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2291 | <= MAXEXTLEN)) |
| 2292 | state |= BMAP_RIGHT_CONTIG; |
| 2293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | /* |
| 2295 | * Switch out based on the FILLING and CONTIG state bits. |
| 2296 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2297 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2298 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2299 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2300 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | /* |
| 2302 | * Setting all of a previous oldext extent to newext. |
| 2303 | * The left and right neighbors are both contiguous with new. |
| 2304 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2305 | --*idx; |
| 2306 | |
| 2307 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2308 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2310 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2311 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2312 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2313 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | ip->i_d.di_nextents -= 2; |
| 2315 | if (cur == NULL) |
| 2316 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2317 | else { |
| 2318 | rval = XFS_ILOG_CORE; |
| 2319 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2320 | RIGHT.br_startblock, |
| 2321 | RIGHT.br_blockcount, &i))) |
| 2322 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2323 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2324 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2326 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2327 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2329 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2330 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2332 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2333 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2335 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2337 | LEFT.br_startblock, |
| 2338 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2339 | RIGHT.br_blockcount, LEFT.br_state))) |
| 2340 | goto done; |
| 2341 | } |
| 2342 | break; |
| 2343 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2344 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | /* |
| 2346 | * Setting all of a previous oldext extent to newext. |
| 2347 | * The left neighbor is contiguous, the right is not. |
| 2348 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2349 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2350 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2351 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2352 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 2353 | LEFT.br_blockcount + PREV.br_blockcount); |
| 2354 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2355 | |
| 2356 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | ip->i_d.di_nextents--; |
| 2358 | if (cur == NULL) |
| 2359 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2360 | else { |
| 2361 | rval = XFS_ILOG_CORE; |
| 2362 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2363 | PREV.br_startblock, PREV.br_blockcount, |
| 2364 | &i))) |
| 2365 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2366 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2367 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2369 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2370 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2372 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2374 | LEFT.br_startblock, |
| 2375 | LEFT.br_blockcount + PREV.br_blockcount, |
| 2376 | LEFT.br_state))) |
| 2377 | goto done; |
| 2378 | } |
| 2379 | break; |
| 2380 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2381 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | /* |
| 2383 | * Setting all of a previous oldext extent to newext. |
| 2384 | * The right neighbor is contiguous, the left is not. |
| 2385 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2386 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | xfs_bmbt_set_blockcount(ep, |
| 2388 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 2389 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2390 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2391 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | ip->i_d.di_nextents--; |
| 2393 | if (cur == NULL) |
| 2394 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2395 | else { |
| 2396 | rval = XFS_ILOG_CORE; |
| 2397 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2398 | RIGHT.br_startblock, |
| 2399 | RIGHT.br_blockcount, &i))) |
| 2400 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2401 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2402 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2404 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2405 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2407 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2409 | new->br_startblock, |
| 2410 | new->br_blockcount + RIGHT.br_blockcount, |
| 2411 | newext))) |
| 2412 | goto done; |
| 2413 | } |
| 2414 | break; |
| 2415 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2416 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | /* |
| 2418 | * Setting all of a previous oldext extent to newext. |
| 2419 | * Neither the left nor right neighbors are contiguous with |
| 2420 | * the new one. |
| 2421 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2422 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2424 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | if (cur == NULL) |
| 2427 | rval = XFS_ILOG_DEXT; |
| 2428 | else { |
| 2429 | rval = 0; |
| 2430 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2431 | new->br_startblock, new->br_blockcount, |
| 2432 | &i))) |
| 2433 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2434 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2436 | new->br_startblock, new->br_blockcount, |
| 2437 | newext))) |
| 2438 | goto done; |
| 2439 | } |
| 2440 | break; |
| 2441 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2442 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | /* |
| 2444 | * Setting the first part of a previous oldext extent to newext. |
| 2445 | * The left neighbor is contiguous. |
| 2446 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2447 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 2448 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | LEFT.br_blockcount + new->br_blockcount); |
| 2450 | xfs_bmbt_set_startoff(ep, |
| 2451 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2452 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2453 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2454 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | xfs_bmbt_set_startblock(ep, |
| 2456 | new->br_startblock + new->br_blockcount); |
| 2457 | xfs_bmbt_set_blockcount(ep, |
| 2458 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2459 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2460 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2461 | --*idx; |
| 2462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | if (cur == NULL) |
| 2464 | rval = XFS_ILOG_DEXT; |
| 2465 | else { |
| 2466 | rval = 0; |
| 2467 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2468 | PREV.br_startblock, PREV.br_blockcount, |
| 2469 | &i))) |
| 2470 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2471 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | if ((error = xfs_bmbt_update(cur, |
| 2473 | PREV.br_startoff + new->br_blockcount, |
| 2474 | PREV.br_startblock + new->br_blockcount, |
| 2475 | PREV.br_blockcount - new->br_blockcount, |
| 2476 | oldext))) |
| 2477 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2478 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2480 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | LEFT.br_startblock, |
| 2482 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2483 | LEFT.br_state); |
| 2484 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | goto done; |
| 2486 | } |
| 2487 | break; |
| 2488 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2489 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | /* |
| 2491 | * Setting the first part of a previous oldext extent to newext. |
| 2492 | * The left neighbor is not contiguous. |
| 2493 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2494 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 2496 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 2497 | xfs_bmbt_set_blockcount(ep, |
| 2498 | PREV.br_blockcount - new->br_blockcount); |
| 2499 | xfs_bmbt_set_startblock(ep, |
| 2500 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2501 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2502 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2503 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | ip->i_d.di_nextents++; |
| 2505 | if (cur == NULL) |
| 2506 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2507 | else { |
| 2508 | rval = XFS_ILOG_CORE; |
| 2509 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2510 | PREV.br_startblock, PREV.br_blockcount, |
| 2511 | &i))) |
| 2512 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2513 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | if ((error = xfs_bmbt_update(cur, |
| 2515 | PREV.br_startoff + new->br_blockcount, |
| 2516 | PREV.br_startblock + new->br_blockcount, |
| 2517 | PREV.br_blockcount - new->br_blockcount, |
| 2518 | oldext))) |
| 2519 | goto done; |
| 2520 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2521 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
| 2525 | break; |
| 2526 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2527 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | /* |
| 2529 | * Setting the last part of a previous oldext extent to newext. |
| 2530 | * The right neighbor is contiguous with the new allocation. |
| 2531 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2532 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | xfs_bmbt_set_blockcount(ep, |
| 2534 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2535 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2536 | |
| 2537 | ++*idx; |
| 2538 | |
| 2539 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2540 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2541 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2543 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | if (cur == NULL) |
| 2546 | rval = XFS_ILOG_DEXT; |
| 2547 | else { |
| 2548 | rval = 0; |
| 2549 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2550 | PREV.br_startblock, |
| 2551 | PREV.br_blockcount, &i))) |
| 2552 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2553 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2555 | PREV.br_startblock, |
| 2556 | PREV.br_blockcount - new->br_blockcount, |
| 2557 | oldext))) |
| 2558 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2559 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | goto done; |
| 2561 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2562 | new->br_startblock, |
| 2563 | new->br_blockcount + RIGHT.br_blockcount, |
| 2564 | newext))) |
| 2565 | goto done; |
| 2566 | } |
| 2567 | break; |
| 2568 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2569 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | /* |
| 2571 | * Setting the last part of a previous oldext extent to newext. |
| 2572 | * The right neighbor is not contiguous. |
| 2573 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2574 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | xfs_bmbt_set_blockcount(ep, |
| 2576 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2577 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2578 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2579 | ++*idx; |
| 2580 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | ip->i_d.di_nextents++; |
| 2583 | if (cur == NULL) |
| 2584 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2585 | else { |
| 2586 | rval = XFS_ILOG_CORE; |
| 2587 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2588 | PREV.br_startblock, PREV.br_blockcount, |
| 2589 | &i))) |
| 2590 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2591 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2593 | PREV.br_startblock, |
| 2594 | PREV.br_blockcount - new->br_blockcount, |
| 2595 | oldext))) |
| 2596 | goto done; |
| 2597 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2598 | new->br_startblock, new->br_blockcount, |
| 2599 | &i))) |
| 2600 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2601 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2603 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2605 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | } |
| 2607 | break; |
| 2608 | |
| 2609 | case 0: |
| 2610 | /* |
| 2611 | * Setting the middle part of a previous oldext extent to |
| 2612 | * newext. Contiguity is impossible here. |
| 2613 | * One extent becomes three extents. |
| 2614 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2615 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | xfs_bmbt_set_blockcount(ep, |
| 2617 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2618 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | r[0] = *new; |
| 2621 | r[1].br_startoff = new_endoff; |
| 2622 | r[1].br_blockcount = |
| 2623 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 2624 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 2625 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2626 | |
| 2627 | ++*idx; |
| 2628 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 2629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | ip->i_d.di_nextents += 2; |
| 2631 | if (cur == NULL) |
| 2632 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2633 | else { |
| 2634 | rval = XFS_ILOG_CORE; |
| 2635 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2636 | PREV.br_startblock, PREV.br_blockcount, |
| 2637 | &i))) |
| 2638 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2639 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | /* new right extent - oldext */ |
| 2641 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 2642 | r[1].br_startblock, r[1].br_blockcount, |
| 2643 | r[1].br_state))) |
| 2644 | goto done; |
| 2645 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 2647 | cur->bc_rec.b.br_blockcount = |
| 2648 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2649 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2651 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2652 | /* |
| 2653 | * Reset the cursor to the position of the new extent |
| 2654 | * we are about to insert as we can't trust it after |
| 2655 | * the previous insert. |
| 2656 | */ |
| 2657 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2658 | new->br_startblock, new->br_blockcount, |
| 2659 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2661 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2663 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2664 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2666 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | } |
| 2668 | break; |
| 2669 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2670 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2671 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2672 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2673 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2674 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2675 | case BMAP_LEFT_CONTIG: |
| 2676 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | /* |
| 2678 | * These cases are all impossible. |
| 2679 | */ |
| 2680 | ASSERT(0); |
| 2681 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2682 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2683 | /* update reverse mappings */ |
| 2684 | error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new); |
| 2685 | if (error) |
| 2686 | goto done; |
| 2687 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2688 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2689 | if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2690 | int tmp_logflags; /* partial log flag return val */ |
| 2691 | |
| 2692 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2693 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2694 | 0, &tmp_logflags, XFS_DATA_FORK); |
| 2695 | *logflagsp |= tmp_logflags; |
| 2696 | if (error) |
| 2697 | goto done; |
| 2698 | } |
| 2699 | |
| 2700 | /* clear out the allocated field, done with it now in any case. */ |
| 2701 | if (cur) { |
| 2702 | cur->bc_private.b.allocated = 0; |
| 2703 | *curp = cur; |
| 2704 | } |
| 2705 | |
| 2706 | xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2708 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | return error; |
| 2710 | #undef LEFT |
| 2711 | #undef RIGHT |
| 2712 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2716 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2718 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | xfs_bmap_add_extent_hole_delay( |
| 2720 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2721 | int whichfork, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2722 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2723 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2725 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2727 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2728 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2729 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 2730 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2731 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2733 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | state = 0; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2735 | if (whichfork == XFS_COW_FORK) |
| 2736 | state |= BMAP_COWFORK; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2737 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | /* |
| 2740 | * Check and set flags if this segment has a left neighbor |
| 2741 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2742 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2743 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2744 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2745 | |
| 2746 | if (isnullstartblock(left.br_startblock)) |
| 2747 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | /* |
| 2751 | * Check and set flags if the current (right) segment exists. |
| 2752 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2753 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2754 | if (*idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2755 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2756 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2757 | |
| 2758 | if (isnullstartblock(right.br_startblock)) |
| 2759 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | /* |
| 2763 | * Set contiguity flags on the left and right neighbors. |
| 2764 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2765 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2766 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2767 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2768 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2769 | state |= BMAP_LEFT_CONTIG; |
| 2770 | |
| 2771 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2772 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2773 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2774 | (!(state & BMAP_LEFT_CONTIG) || |
| 2775 | (left.br_blockcount + new->br_blockcount + |
| 2776 | right.br_blockcount <= MAXEXTLEN))) |
| 2777 | state |= BMAP_RIGHT_CONTIG; |
| 2778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | /* |
| 2780 | * Switch out based on the contiguity flags. |
| 2781 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2782 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2783 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | /* |
| 2785 | * New allocation is contiguous with delayed allocations |
| 2786 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2787 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2789 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | temp = left.br_blockcount + new->br_blockcount + |
| 2791 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2792 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2793 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2794 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2795 | oldlen = startblockval(left.br_startblock) + |
| 2796 | startblockval(new->br_startblock) + |
| 2797 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2799 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2800 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2801 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2802 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2803 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | break; |
| 2805 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2806 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | /* |
| 2808 | * New allocation is contiguous with a delayed allocation |
| 2809 | * on the left. |
| 2810 | * Merge the new allocation with the left neighbor. |
| 2811 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2812 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2814 | |
| 2815 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2816 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2817 | oldlen = startblockval(left.br_startblock) + |
| 2818 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2820 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2821 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2822 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | break; |
| 2824 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2825 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | /* |
| 2827 | * New allocation is contiguous with a delayed allocation |
| 2828 | * on the right. |
| 2829 | * Merge the new allocation with the right neighbor. |
| 2830 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2831 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2833 | oldlen = startblockval(new->br_startblock) + |
| 2834 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2836 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 2837 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2838 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2839 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | break; |
| 2841 | |
| 2842 | case 0: |
| 2843 | /* |
| 2844 | * New allocation is not contiguous with another |
| 2845 | * delayed allocation. |
| 2846 | * Insert a new entry. |
| 2847 | */ |
| 2848 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2849 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | break; |
| 2851 | } |
| 2852 | if (oldlen != newlen) { |
| 2853 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2854 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2855 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | /* |
| 2857 | * Nothing to do for disk quota accounting here. |
| 2858 | */ |
| 2859 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2863 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | */ |
| 2865 | STATIC int /* error */ |
| 2866 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2867 | struct xfs_bmalloca *bma, |
| 2868 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | { |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2870 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | int i; /* temp state */ |
| 2873 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2874 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2875 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2876 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | int state; /* state bits, accessed thru macros */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2878 | struct xfs_mount *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 2880 | mp = bma->ip->i_mount; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2881 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2882 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2883 | ASSERT(bma->idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2884 | ASSERT(bma->idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2885 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2886 | ASSERT(!bma->cur || |
| 2887 | !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2888 | ASSERT(whichfork != XFS_COW_FORK); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2889 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2890 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2891 | |
| 2892 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2893 | if (whichfork == XFS_ATTR_FORK) |
| 2894 | state |= BMAP_ATTRFORK; |
| 2895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | /* |
| 2897 | * Check and set flags if this segment has a left neighbor. |
| 2898 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2899 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2900 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2901 | 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] | 2902 | if (isnullstartblock(left.br_startblock)) |
| 2903 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | /* |
| 2907 | * Check and set flags if this segment has a current value. |
| 2908 | * Not true if we're inserting into the "hole" at eof. |
| 2909 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 2910 | if (bma->idx < xfs_iext_count(ifp)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2911 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2912 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2913 | if (isnullstartblock(right.br_startblock)) |
| 2914 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | /* |
| 2918 | * We're inserting a real allocation between "left" and "right". |
| 2919 | * Set the contiguity flags. Don't let extents get too large. |
| 2920 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2921 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2922 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2923 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2924 | left.br_state == new->br_state && |
| 2925 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2926 | state |= BMAP_LEFT_CONTIG; |
| 2927 | |
| 2928 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2929 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2930 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2931 | new->br_state == right.br_state && |
| 2932 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2933 | (!(state & BMAP_LEFT_CONTIG) || |
| 2934 | left.br_blockcount + new->br_blockcount + |
| 2935 | right.br_blockcount <= MAXEXTLEN)) |
| 2936 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2938 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | /* |
| 2940 | * Select which case we're in here, and implement it. |
| 2941 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2942 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2943 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | /* |
| 2945 | * New allocation is contiguous with real allocations on the |
| 2946 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2947 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2949 | --bma->idx; |
| 2950 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2951 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | left.br_blockcount + new->br_blockcount + |
| 2953 | right.br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2954 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2955 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2956 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2957 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2958 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 2959 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1); |
| 2960 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2961 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2962 | } else { |
| 2963 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2964 | error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff, |
| 2965 | right.br_startblock, right.br_blockcount, |
| 2966 | &i); |
| 2967 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2968 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2969 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2970 | error = xfs_btree_delete(bma->cur, &i); |
| 2971 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2972 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2973 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2974 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 2975 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2976 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2977 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2978 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2979 | left.br_startblock, |
| 2980 | left.br_blockcount + |
| 2981 | new->br_blockcount + |
| 2982 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2983 | left.br_state); |
| 2984 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2985 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2987 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2989 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | /* |
| 2991 | * New allocation is contiguous with a real allocation |
| 2992 | * on the left. |
| 2993 | * Merge the new allocation with the left neighbor. |
| 2994 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2995 | --bma->idx; |
| 2996 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2997 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2999 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3000 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3001 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3002 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3003 | } else { |
| 3004 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3005 | error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff, |
| 3006 | left.br_startblock, left.br_blockcount, |
| 3007 | &i); |
| 3008 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3009 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3010 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3011 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3012 | left.br_startblock, |
| 3013 | left.br_blockcount + |
| 3014 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3015 | left.br_state); |
| 3016 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3017 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3019 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3021 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | /* |
| 3023 | * New allocation is contiguous with a real allocation |
| 3024 | * on the right. |
| 3025 | * Merge the new allocation with the right neighbor. |
| 3026 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3027 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3028 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 3029 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | new->br_blockcount + right.br_blockcount, |
| 3031 | right.br_state); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3032 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3033 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3034 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3035 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3036 | } else { |
| 3037 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3038 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3039 | right.br_startoff, |
| 3040 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3041 | right.br_blockcount, &i); |
| 3042 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3043 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3044 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3045 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3046 | new->br_startblock, |
| 3047 | new->br_blockcount + |
| 3048 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3049 | right.br_state); |
| 3050 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3051 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3053 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | |
| 3055 | case 0: |
| 3056 | /* |
| 3057 | * New allocation is not contiguous with another |
| 3058 | * real allocation. |
| 3059 | * Insert a new entry. |
| 3060 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3061 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 3062 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 3063 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1); |
| 3064 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3065 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3066 | } else { |
| 3067 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3068 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3069 | new->br_startoff, |
| 3070 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3071 | new->br_blockcount, &i); |
| 3072 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3073 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3074 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3075 | bma->cur->bc_rec.b.br_state = new->br_state; |
| 3076 | error = xfs_btree_insert(bma->cur, &i); |
| 3077 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3078 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3079 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3080 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3081 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3083 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 3084 | /* add reverse mapping */ |
| 3085 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 3086 | if (error) |
| 3087 | goto done; |
| 3088 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3089 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3090 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3091 | int tmp_logflags; /* partial log flag return val */ |
| 3092 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3093 | ASSERT(bma->cur == NULL); |
| 3094 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3095 | bma->firstblock, bma->dfops, &bma->cur, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3096 | 0, &tmp_logflags, whichfork); |
| 3097 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3098 | if (error) |
| 3099 | goto done; |
| 3100 | } |
| 3101 | |
| 3102 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3103 | if (bma->cur) |
| 3104 | bma->cur->bc_private.b.allocated = 0; |
| 3105 | |
| 3106 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3107 | done: |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3108 | bma->logflags |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3109 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | } |
| 3111 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3112 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 3113 | * Functions used in the extent read, allocate and remove paths |
| 3114 | */ |
| 3115 | |
| 3116 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3117 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 3118 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3119 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3120 | xfs_bmap_extsize_align( |
| 3121 | xfs_mount_t *mp, |
| 3122 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 3123 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 3124 | xfs_extlen_t extsz, /* align to this extent size */ |
| 3125 | int rt, /* is this a realtime inode? */ |
| 3126 | int eof, /* is extent at end-of-file? */ |
| 3127 | int delay, /* creating delalloc extent? */ |
| 3128 | int convert, /* overwriting unwritten extent? */ |
| 3129 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 3130 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 3131 | { |
| 3132 | xfs_fileoff_t orig_off; /* original offset */ |
| 3133 | xfs_extlen_t orig_alen; /* original length */ |
| 3134 | xfs_fileoff_t orig_end; /* original off+len */ |
| 3135 | xfs_fileoff_t nexto; /* next file offset */ |
| 3136 | xfs_fileoff_t prevo; /* previous file offset */ |
| 3137 | xfs_fileoff_t align_off; /* temp for offset */ |
| 3138 | xfs_extlen_t align_alen; /* temp for length */ |
| 3139 | xfs_extlen_t temp; /* temp for calculations */ |
| 3140 | |
| 3141 | if (convert) |
| 3142 | return 0; |
| 3143 | |
| 3144 | orig_off = align_off = *offp; |
| 3145 | orig_alen = align_alen = *lenp; |
| 3146 | orig_end = orig_off + orig_alen; |
| 3147 | |
| 3148 | /* |
| 3149 | * If this request overlaps an existing extent, then don't |
| 3150 | * attempt to perform any additional alignment. |
| 3151 | */ |
| 3152 | if (!delay && !eof && |
| 3153 | (orig_off >= gotp->br_startoff) && |
| 3154 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 3155 | return 0; |
| 3156 | } |
| 3157 | |
| 3158 | /* |
| 3159 | * If the file offset is unaligned vs. the extent size |
| 3160 | * we need to align it. This will be possible unless |
| 3161 | * the file was previously written with a kernel that didn't |
| 3162 | * perform this alignment, or if a truncate shot us in the |
| 3163 | * foot. |
| 3164 | */ |
| 3165 | temp = do_mod(orig_off, extsz); |
| 3166 | if (temp) { |
| 3167 | align_alen += temp; |
| 3168 | align_off -= temp; |
| 3169 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3170 | |
| 3171 | /* Same adjustment for the end of the requested area. */ |
| 3172 | temp = (align_alen % extsz); |
| 3173 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3174 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3175 | |
| 3176 | /* |
| 3177 | * For large extent hint sizes, the aligned extent might be larger than |
| 3178 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 3179 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 3180 | * short allocation just fine, so it is safe to do this. We only want to |
| 3181 | * do it when we are forced to, though, because it means more allocation |
| 3182 | * operations are required. |
| 3183 | */ |
| 3184 | while (align_alen > MAXEXTLEN) |
| 3185 | align_alen -= extsz; |
| 3186 | ASSERT(align_alen <= MAXEXTLEN); |
| 3187 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3188 | /* |
| 3189 | * If the previous block overlaps with this proposed allocation |
| 3190 | * then move the start forward without adjusting the length. |
| 3191 | */ |
| 3192 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3193 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3194 | prevo = prevp->br_startoff; |
| 3195 | else |
| 3196 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3197 | } else |
| 3198 | prevo = 0; |
| 3199 | if (align_off != orig_off && align_off < prevo) |
| 3200 | align_off = prevo; |
| 3201 | /* |
| 3202 | * If the next block overlaps with this proposed allocation |
| 3203 | * then move the start back without adjusting the length, |
| 3204 | * but not before offset 0. |
| 3205 | * This may of course make the start overlap previous block, |
| 3206 | * and if we hit the offset 0 limit then the next block |
| 3207 | * can still overlap too. |
| 3208 | */ |
| 3209 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3210 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3211 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3212 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3213 | else |
| 3214 | nexto = gotp->br_startoff; |
| 3215 | } else |
| 3216 | nexto = NULLFILEOFF; |
| 3217 | if (!eof && |
| 3218 | align_off + align_alen != orig_end && |
| 3219 | align_off + align_alen > nexto) |
| 3220 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3221 | /* |
| 3222 | * If we're now overlapping the next or previous extent that |
| 3223 | * means we can't fit an extsz piece in this hole. Just move |
| 3224 | * the start forward to the first valid spot and set |
| 3225 | * the length so we hit the end. |
| 3226 | */ |
| 3227 | if (align_off != orig_off && align_off < prevo) |
| 3228 | align_off = prevo; |
| 3229 | if (align_off + align_alen != orig_end && |
| 3230 | align_off + align_alen > nexto && |
| 3231 | nexto != NULLFILEOFF) { |
| 3232 | ASSERT(nexto > prevo); |
| 3233 | align_alen = nexto - align_off; |
| 3234 | } |
| 3235 | |
| 3236 | /* |
| 3237 | * If realtime, and the result isn't a multiple of the realtime |
| 3238 | * extent size we need to remove blocks until it is. |
| 3239 | */ |
| 3240 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3241 | /* |
| 3242 | * We're not covering the original request, or |
| 3243 | * we won't be able to once we fix the length. |
| 3244 | */ |
| 3245 | if (orig_off < align_off || |
| 3246 | orig_end > align_off + align_alen || |
| 3247 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3248 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3249 | /* |
| 3250 | * Try to fix it by moving the start up. |
| 3251 | */ |
| 3252 | if (align_off + temp <= orig_off) { |
| 3253 | align_alen -= temp; |
| 3254 | align_off += temp; |
| 3255 | } |
| 3256 | /* |
| 3257 | * Try to fix it by moving the end in. |
| 3258 | */ |
| 3259 | else if (align_off + align_alen - temp >= orig_end) |
| 3260 | align_alen -= temp; |
| 3261 | /* |
| 3262 | * Set the start to the minimum then trim the length. |
| 3263 | */ |
| 3264 | else { |
| 3265 | align_alen -= orig_off - align_off; |
| 3266 | align_off = orig_off; |
| 3267 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3268 | } |
| 3269 | /* |
| 3270 | * Result doesn't cover the request, fail it. |
| 3271 | */ |
| 3272 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3273 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3274 | } else { |
| 3275 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3276 | /* see MAXEXTLEN handling above */ |
| 3277 | ASSERT(orig_end <= align_off + align_alen || |
| 3278 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | #ifdef DEBUG |
| 3282 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3283 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3284 | if (prevp->br_startoff != NULLFILEOFF) |
| 3285 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3286 | #endif |
| 3287 | |
| 3288 | *lenp = align_alen; |
| 3289 | *offp = align_off; |
| 3290 | return 0; |
| 3291 | } |
| 3292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3293 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3294 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3295 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3296 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3297 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | { |
| 3299 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3301 | xfs_mount_t *mp; /* mount point structure */ |
| 3302 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3303 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | |
| 3305 | #define ISVALID(x,y) \ |
| 3306 | (rt ? \ |
| 3307 | (x) < mp->m_sb.sb_rblocks : \ |
| 3308 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3309 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3310 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3313 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3314 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3315 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3316 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | /* |
| 3318 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3319 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3321 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3322 | !isnullstartblock(ap->prev.br_startblock) && |
| 3323 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3324 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3325 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | /* |
| 3327 | * Adjust for the gap between prevp and us. |
| 3328 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3329 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3330 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3332 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3333 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | } |
| 3335 | /* |
| 3336 | * If not at eof, then compare the two neighbor blocks. |
| 3337 | * Figure out whether either one gives us a good starting point, |
| 3338 | * and pick the better one. |
| 3339 | */ |
| 3340 | else if (!ap->eof) { |
| 3341 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3342 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3343 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3344 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3345 | |
| 3346 | /* |
| 3347 | * If there's a previous (left) block, select a requested |
| 3348 | * start block based on it. |
| 3349 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3350 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3351 | !isnullstartblock(ap->prev.br_startblock) && |
| 3352 | (prevbno = ap->prev.br_startblock + |
| 3353 | ap->prev.br_blockcount) && |
| 3354 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3355 | /* |
| 3356 | * Calculate gap to end of previous block. |
| 3357 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3358 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3359 | (ap->prev.br_startoff + |
| 3360 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | /* |
| 3362 | * Figure the startblock based on the previous block's |
| 3363 | * end and the gap size. |
| 3364 | * Heuristic! |
| 3365 | * If the gap is large relative to the piece we're |
| 3366 | * allocating, or using it gives us an invalid block |
| 3367 | * number, then just use the end of the previous block. |
| 3368 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3369 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3371 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | prevbno += adjust; |
| 3373 | else |
| 3374 | prevdiff += adjust; |
| 3375 | /* |
| 3376 | * If the firstblock forbids it, can't use it, |
| 3377 | * must use default. |
| 3378 | */ |
| 3379 | if (!rt && !nullfb && |
| 3380 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3381 | prevbno = NULLFSBLOCK; |
| 3382 | } |
| 3383 | /* |
| 3384 | * No previous block or can't follow it, just default. |
| 3385 | */ |
| 3386 | else |
| 3387 | prevbno = NULLFSBLOCK; |
| 3388 | /* |
| 3389 | * If there's a following (right) block, select a requested |
| 3390 | * start block based on it. |
| 3391 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3392 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | /* |
| 3394 | * Calculate gap to start of next block. |
| 3395 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3396 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | /* |
| 3398 | * Figure the startblock based on the next block's |
| 3399 | * start and the gap size. |
| 3400 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3401 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | /* |
| 3403 | * Heuristic! |
| 3404 | * If the gap is large relative to the piece we're |
| 3405 | * allocating, or using it gives us an invalid block |
| 3406 | * number, then just use the start of the next block |
| 3407 | * offset by our length. |
| 3408 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3409 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3411 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3412 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3413 | gotbno -= ap->length; |
| 3414 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | } else |
| 3416 | gotdiff += adjust; |
| 3417 | /* |
| 3418 | * If the firstblock forbids it, can't use it, |
| 3419 | * must use default. |
| 3420 | */ |
| 3421 | if (!rt && !nullfb && |
| 3422 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3423 | gotbno = NULLFSBLOCK; |
| 3424 | } |
| 3425 | /* |
| 3426 | * No next block, just default. |
| 3427 | */ |
| 3428 | else |
| 3429 | gotbno = NULLFSBLOCK; |
| 3430 | /* |
| 3431 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3432 | * 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] | 3433 | */ |
| 3434 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3435 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3437 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3439 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3441 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3442 | } |
| 3443 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3444 | static int |
| 3445 | xfs_bmap_longest_free_extent( |
| 3446 | struct xfs_trans *tp, |
| 3447 | xfs_agnumber_t ag, |
| 3448 | xfs_extlen_t *blen, |
| 3449 | int *notinit) |
| 3450 | { |
| 3451 | struct xfs_mount *mp = tp->t_mountp; |
| 3452 | struct xfs_perag *pag; |
| 3453 | xfs_extlen_t longest; |
| 3454 | int error = 0; |
| 3455 | |
| 3456 | pag = xfs_perag_get(mp, ag); |
| 3457 | if (!pag->pagf_init) { |
| 3458 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3459 | if (error) |
| 3460 | goto out; |
| 3461 | |
| 3462 | if (!pag->pagf_init) { |
| 3463 | *notinit = 1; |
| 3464 | goto out; |
| 3465 | } |
| 3466 | } |
| 3467 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 3468 | longest = xfs_alloc_longest_free_extent(mp, pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3469 | xfs_alloc_min_freelist(mp, pag), |
| 3470 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3471 | if (*blen < longest) |
| 3472 | *blen = longest; |
| 3473 | |
| 3474 | out: |
| 3475 | xfs_perag_put(pag); |
| 3476 | return error; |
| 3477 | } |
| 3478 | |
| 3479 | static void |
| 3480 | xfs_bmap_select_minlen( |
| 3481 | struct xfs_bmalloca *ap, |
| 3482 | struct xfs_alloc_arg *args, |
| 3483 | xfs_extlen_t *blen, |
| 3484 | int notinit) |
| 3485 | { |
| 3486 | if (notinit || *blen < ap->minlen) { |
| 3487 | /* |
| 3488 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3489 | * there is space for this request. |
| 3490 | */ |
| 3491 | args->minlen = ap->minlen; |
| 3492 | } else if (*blen < args->maxlen) { |
| 3493 | /* |
| 3494 | * If the best seen length is less than the request length, |
| 3495 | * use the best as the minimum. |
| 3496 | */ |
| 3497 | args->minlen = *blen; |
| 3498 | } else { |
| 3499 | /* |
| 3500 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3501 | * as the minimum. |
| 3502 | */ |
| 3503 | args->minlen = args->maxlen; |
| 3504 | } |
| 3505 | } |
| 3506 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3507 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3508 | xfs_bmap_btalloc_nullfb( |
| 3509 | struct xfs_bmalloca *ap, |
| 3510 | struct xfs_alloc_arg *args, |
| 3511 | xfs_extlen_t *blen) |
| 3512 | { |
| 3513 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3514 | xfs_agnumber_t ag, startag; |
| 3515 | int notinit = 0; |
| 3516 | int error; |
| 3517 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3518 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3519 | args->total = ap->total; |
| 3520 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3521 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3522 | if (startag == NULLAGNUMBER) |
| 3523 | startag = ag = 0; |
| 3524 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3525 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3526 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3527 | ¬init); |
| 3528 | if (error) |
| 3529 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3530 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3531 | if (++ag == mp->m_sb.sb_agcount) |
| 3532 | ag = 0; |
| 3533 | if (ag == startag) |
| 3534 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3535 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3536 | |
| 3537 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3538 | return 0; |
| 3539 | } |
| 3540 | |
| 3541 | STATIC int |
| 3542 | xfs_bmap_btalloc_filestreams( |
| 3543 | struct xfs_bmalloca *ap, |
| 3544 | struct xfs_alloc_arg *args, |
| 3545 | xfs_extlen_t *blen) |
| 3546 | { |
| 3547 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3548 | xfs_agnumber_t ag; |
| 3549 | int notinit = 0; |
| 3550 | int error; |
| 3551 | |
| 3552 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3553 | args->total = ap->total; |
| 3554 | |
| 3555 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3556 | if (ag == NULLAGNUMBER) |
| 3557 | ag = 0; |
| 3558 | |
| 3559 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3560 | if (error) |
| 3561 | return error; |
| 3562 | |
| 3563 | if (*blen < args->maxlen) { |
| 3564 | error = xfs_filestream_new_ag(ap, &ag); |
| 3565 | if (error) |
| 3566 | return error; |
| 3567 | |
| 3568 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3569 | ¬init); |
| 3570 | if (error) |
| 3571 | return error; |
| 3572 | |
| 3573 | } |
| 3574 | |
| 3575 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3576 | |
| 3577 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3578 | * Set the failure fallback case to look in the selected AG as stream |
| 3579 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3580 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3581 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3582 | return 0; |
| 3583 | } |
| 3584 | |
| 3585 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3586 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3587 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3588 | { |
| 3589 | xfs_mount_t *mp; /* mount point structure */ |
| 3590 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3591 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3592 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3593 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3594 | xfs_alloc_arg_t args; |
| 3595 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3596 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3597 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3598 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3599 | int tryagain; |
| 3600 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3601 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3602 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3603 | ASSERT(ap->length); |
| 3604 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3605 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3606 | |
| 3607 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3608 | stripe_align = 0; |
| 3609 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3610 | stripe_align = mp->m_swidth; |
| 3611 | else if (mp->m_dalign) |
| 3612 | stripe_align = mp->m_dalign; |
| 3613 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3614 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3615 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3616 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3617 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3618 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3619 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3620 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3621 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3622 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3623 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3624 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3625 | |
| 3626 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3627 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3628 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3629 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3630 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3631 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3632 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3633 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3634 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3635 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3636 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3637 | } |
| 3638 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3639 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3640 | |
| 3641 | xfs_bmap_adjacent(ap); |
| 3642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3644 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | * it's in the right allocation group. |
| 3646 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3647 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | ; |
| 3649 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3650 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3652 | * Normal allocation, done through xfs_alloc_vextent. |
| 3653 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3654 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3655 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3656 | args.tp = ap->tp; |
| 3657 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3658 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3659 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3660 | |
| 3661 | /* Trim the allocation back to the maximum an AG can fit. */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 3662 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3663 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3664 | blen = 0; |
| 3665 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3666 | /* |
| 3667 | * Search for an allocation group with a single extent large |
| 3668 | * enough for the request. If one isn't found, then adjust |
| 3669 | * the minimum allocation size to the largest space found. |
| 3670 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3671 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3672 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3673 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3674 | else |
| 3675 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3676 | if (error) |
| 3677 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3678 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3679 | if (xfs_inode_is_filestream(ap->ip)) |
| 3680 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3681 | else |
| 3682 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3683 | args.total = args.minlen = ap->minlen; |
| 3684 | } else { |
| 3685 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3686 | args.total = ap->total; |
| 3687 | args.minlen = ap->minlen; |
| 3688 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3689 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3690 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3691 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3692 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3693 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3694 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3695 | args.prod = 1; |
| 3696 | args.mod = 0; |
| 3697 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3698 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3699 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3700 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3701 | } |
| 3702 | /* |
| 3703 | * If we are not low on available data blocks, and the |
| 3704 | * underlying logical volume manager is a stripe, and |
| 3705 | * the file offset is zero then try to allocate data |
| 3706 | * blocks on stripe unit boundary. |
| 3707 | * NOTE: ap->aeof is only set if the allocation length |
| 3708 | * is >= the stripe unit and the allocation offset is |
| 3709 | * at the end of file. |
| 3710 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3711 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3712 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3713 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3714 | atype = args.type; |
| 3715 | isaligned = 1; |
| 3716 | /* |
| 3717 | * Adjust for alignment |
| 3718 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3719 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3720 | args.minlen = blen - args.alignment; |
| 3721 | args.minalignslop = 0; |
| 3722 | } else { |
| 3723 | /* |
| 3724 | * First try an exact bno allocation. |
| 3725 | * If it fails then do a near or start bno |
| 3726 | * allocation with alignment turned on. |
| 3727 | */ |
| 3728 | atype = args.type; |
| 3729 | tryagain = 1; |
| 3730 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3731 | args.alignment = 1; |
| 3732 | /* |
| 3733 | * Compute the minlen+alignment for the |
| 3734 | * next case. Set slop so that the value |
| 3735 | * of minlen+alignment+slop doesn't go up |
| 3736 | * between the calls. |
| 3737 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3738 | if (blen > stripe_align && blen <= args.maxlen) |
| 3739 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3740 | else |
| 3741 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3742 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3743 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3744 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3745 | args.minlen - 1; |
| 3746 | else |
| 3747 | args.minalignslop = 0; |
| 3748 | } |
| 3749 | } else { |
| 3750 | args.alignment = 1; |
| 3751 | args.minalignslop = 0; |
| 3752 | } |
| 3753 | args.minleft = ap->minleft; |
| 3754 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3755 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3756 | args.datatype = ap->datatype; |
| 3757 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3758 | args.ip = ap->ip; |
| 3759 | |
| 3760 | error = xfs_alloc_vextent(&args); |
| 3761 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3762 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3763 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3764 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3765 | /* |
| 3766 | * Exact allocation failed. Now try with alignment |
| 3767 | * turned on. |
| 3768 | */ |
| 3769 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3770 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3771 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3772 | args.minlen = nextminlen; |
| 3773 | args.minalignslop = 0; |
| 3774 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | if ((error = xfs_alloc_vextent(&args))) |
| 3776 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3777 | } |
| 3778 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3779 | /* |
| 3780 | * allocation failed, so turn off alignment and |
| 3781 | * try again. |
| 3782 | */ |
| 3783 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3784 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3785 | args.alignment = 0; |
| 3786 | if ((error = xfs_alloc_vextent(&args))) |
| 3787 | return error; |
| 3788 | } |
| 3789 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3790 | args.minlen > ap->minlen) { |
| 3791 | args.minlen = ap->minlen; |
| 3792 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3793 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3794 | if ((error = xfs_alloc_vextent(&args))) |
| 3795 | return error; |
| 3796 | } |
| 3797 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3798 | args.fsbno = 0; |
| 3799 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3800 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3801 | if ((error = xfs_alloc_vextent(&args))) |
| 3802 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3803 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3804 | } |
| 3805 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3806 | /* |
| 3807 | * check the allocation happened at the same or higher AG than |
| 3808 | * the first block that was allocated. |
| 3809 | */ |
| 3810 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
| 3811 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == |
| 3812 | XFS_FSB_TO_AGNO(mp, args.fsbno) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3813 | (ap->dfops->dop_low && |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3814 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < |
| 3815 | XFS_FSB_TO_AGNO(mp, args.fsbno))); |
| 3816 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3817 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3818 | if (*ap->firstblock == NULLFSBLOCK) |
| 3819 | *ap->firstblock = args.fsbno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3820 | ASSERT(nullfb || fb_agno == args.agno || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3821 | (ap->dfops->dop_low && fb_agno < args.agno)); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3822 | ap->length = args.len; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 3823 | if (!(ap->flags & XFS_BMAPI_COWFORK)) |
| 3824 | ap->ip->i_d.di_nblocks += args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3825 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3826 | if (ap->wasdel) |
| 3827 | ap->ip->i_delayed_blks -= args.len; |
| 3828 | /* |
| 3829 | * Adjust the disk quota also. This was reserved |
| 3830 | * earlier. |
| 3831 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3832 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3833 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3834 | XFS_TRANS_DQ_BCOUNT, |
| 3835 | (long) args.len); |
| 3836 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3837 | ap->blkno = NULLFSBLOCK; |
| 3838 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | } |
| 3840 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | /* |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 3844 | * For a remap operation, just "allocate" an extent at the address that the |
| 3845 | * caller passed in, and ensure that the AGFL is the right size. The caller |
| 3846 | * will then map the "allocated" extent into the file somewhere. |
| 3847 | */ |
| 3848 | STATIC int |
| 3849 | xfs_bmap_remap_alloc( |
| 3850 | struct xfs_bmalloca *ap) |
| 3851 | { |
| 3852 | struct xfs_trans *tp = ap->tp; |
| 3853 | struct xfs_mount *mp = tp->t_mountp; |
| 3854 | xfs_agblock_t bno; |
| 3855 | struct xfs_alloc_arg args; |
| 3856 | int error; |
| 3857 | |
| 3858 | /* |
| 3859 | * validate that the block number is legal - the enables us to detect |
| 3860 | * and handle a silent filesystem corruption rather than crashing. |
| 3861 | */ |
| 3862 | memset(&args, 0, sizeof(struct xfs_alloc_arg)); |
| 3863 | args.tp = ap->tp; |
| 3864 | args.mp = ap->tp->t_mountp; |
| 3865 | bno = *ap->firstblock; |
| 3866 | args.agno = XFS_FSB_TO_AGNO(mp, bno); |
| 3867 | args.agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 3868 | if (args.agno >= mp->m_sb.sb_agcount || |
| 3869 | args.agbno >= mp->m_sb.sb_agblocks) |
| 3870 | return -EFSCORRUPTED; |
| 3871 | |
| 3872 | /* "Allocate" the extent from the range we passed in. */ |
| 3873 | trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length); |
| 3874 | ap->blkno = bno; |
| 3875 | ap->ip->i_d.di_nblocks += ap->length; |
| 3876 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3877 | |
| 3878 | /* Fix the freelist, like a real allocator does. */ |
| 3879 | args.datatype = ap->datatype; |
| 3880 | args.pag = xfs_perag_get(args.mp, args.agno); |
| 3881 | ASSERT(args.pag); |
| 3882 | |
| 3883 | /* |
| 3884 | * The freelist fixing code will decline the allocation if |
| 3885 | * the size and shape of the free space doesn't allow for |
| 3886 | * allocating the extent and updating all the metadata that |
| 3887 | * happens during an allocation. We're remapping, not |
| 3888 | * allocating, so skip that check by pretending to be freeing. |
| 3889 | */ |
| 3890 | error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING); |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 3891 | xfs_perag_put(args.pag); |
| 3892 | if (error) |
| 3893 | trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_); |
| 3894 | return error; |
| 3895 | } |
| 3896 | |
| 3897 | /* |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3898 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3899 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3900 | */ |
| 3901 | STATIC int |
| 3902 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3903 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3904 | { |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 3905 | if (ap->flags & XFS_BMAPI_REMAP) |
| 3906 | return xfs_bmap_remap_alloc(ap); |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3907 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3908 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3909 | return xfs_bmap_rtalloc(ap); |
| 3910 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3913 | /* Trim extent to fit a logical block range. */ |
| 3914 | void |
| 3915 | xfs_trim_extent( |
| 3916 | struct xfs_bmbt_irec *irec, |
| 3917 | xfs_fileoff_t bno, |
| 3918 | xfs_filblks_t len) |
| 3919 | { |
| 3920 | xfs_fileoff_t distance; |
| 3921 | xfs_fileoff_t end = bno + len; |
| 3922 | |
| 3923 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3924 | irec->br_startoff >= end) { |
| 3925 | irec->br_blockcount = 0; |
| 3926 | return; |
| 3927 | } |
| 3928 | |
| 3929 | if (irec->br_startoff < bno) { |
| 3930 | distance = bno - irec->br_startoff; |
| 3931 | if (isnullstartblock(irec->br_startblock)) |
| 3932 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3933 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3934 | irec->br_startblock != HOLESTARTBLOCK) |
| 3935 | irec->br_startblock += distance; |
| 3936 | irec->br_startoff += distance; |
| 3937 | irec->br_blockcount -= distance; |
| 3938 | } |
| 3939 | |
| 3940 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3941 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3942 | irec->br_blockcount -= distance; |
| 3943 | } |
| 3944 | } |
| 3945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3947 | * Trim the returned map to the required bounds |
| 3948 | */ |
| 3949 | STATIC void |
| 3950 | xfs_bmapi_trim_map( |
| 3951 | struct xfs_bmbt_irec *mval, |
| 3952 | struct xfs_bmbt_irec *got, |
| 3953 | xfs_fileoff_t *bno, |
| 3954 | xfs_filblks_t len, |
| 3955 | xfs_fileoff_t obno, |
| 3956 | xfs_fileoff_t end, |
| 3957 | int n, |
| 3958 | int flags) |
| 3959 | { |
| 3960 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3961 | got->br_startoff + got->br_blockcount <= obno) { |
| 3962 | *mval = *got; |
| 3963 | if (isnullstartblock(got->br_startblock)) |
| 3964 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3965 | return; |
| 3966 | } |
| 3967 | |
| 3968 | if (obno > *bno) |
| 3969 | *bno = obno; |
| 3970 | ASSERT((*bno >= obno) || (n == 0)); |
| 3971 | ASSERT(*bno < end); |
| 3972 | mval->br_startoff = *bno; |
| 3973 | if (isnullstartblock(got->br_startblock)) |
| 3974 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3975 | else |
| 3976 | mval->br_startblock = got->br_startblock + |
| 3977 | (*bno - got->br_startoff); |
| 3978 | /* |
| 3979 | * Return the minimum of what we got and what we asked for for |
| 3980 | * the length. We can use the len variable here because it is |
| 3981 | * modified below and we could have been there before coming |
| 3982 | * here if the first part of the allocation didn't overlap what |
| 3983 | * was asked for. |
| 3984 | */ |
| 3985 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3986 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3987 | mval->br_state = got->br_state; |
| 3988 | ASSERT(mval->br_blockcount <= len); |
| 3989 | return; |
| 3990 | } |
| 3991 | |
| 3992 | /* |
| 3993 | * Update and validate the extent map to return |
| 3994 | */ |
| 3995 | STATIC void |
| 3996 | xfs_bmapi_update_map( |
| 3997 | struct xfs_bmbt_irec **map, |
| 3998 | xfs_fileoff_t *bno, |
| 3999 | xfs_filblks_t *len, |
| 4000 | xfs_fileoff_t obno, |
| 4001 | xfs_fileoff_t end, |
| 4002 | int *n, |
| 4003 | int flags) |
| 4004 | { |
| 4005 | xfs_bmbt_irec_t *mval = *map; |
| 4006 | |
| 4007 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 4008 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 4009 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 4010 | (mval->br_startoff < obno)); |
| 4011 | |
| 4012 | *bno = mval->br_startoff + mval->br_blockcount; |
| 4013 | *len = end - *bno; |
| 4014 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 4015 | /* update previous map with new information */ |
| 4016 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 4017 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 4018 | ASSERT(mval->br_state == mval[-1].br_state); |
| 4019 | mval[-1].br_blockcount = mval->br_blockcount; |
| 4020 | mval[-1].br_state = mval->br_state; |
| 4021 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 4022 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 4023 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 4024 | mval->br_startblock == mval[-1].br_startblock + |
| 4025 | mval[-1].br_blockcount && |
| 4026 | ((flags & XFS_BMAPI_IGSTATE) || |
| 4027 | mval[-1].br_state == mval->br_state)) { |
| 4028 | ASSERT(mval->br_startoff == |
| 4029 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 4030 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4031 | } else if (*n > 0 && |
| 4032 | mval->br_startblock == DELAYSTARTBLOCK && |
| 4033 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 4034 | mval->br_startoff == |
| 4035 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 4036 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4037 | mval[-1].br_state = mval->br_state; |
| 4038 | } else if (!((*n == 0) && |
| 4039 | ((mval->br_startoff + mval->br_blockcount) <= |
| 4040 | obno))) { |
| 4041 | mval++; |
| 4042 | (*n)++; |
| 4043 | } |
| 4044 | *map = mval; |
| 4045 | } |
| 4046 | |
| 4047 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4048 | * Map file blocks to filesystem blocks without allocation. |
| 4049 | */ |
| 4050 | int |
| 4051 | xfs_bmapi_read( |
| 4052 | struct xfs_inode *ip, |
| 4053 | xfs_fileoff_t bno, |
| 4054 | xfs_filblks_t len, |
| 4055 | struct xfs_bmbt_irec *mval, |
| 4056 | int *nmap, |
| 4057 | int flags) |
| 4058 | { |
| 4059 | struct xfs_mount *mp = ip->i_mount; |
| 4060 | struct xfs_ifork *ifp; |
| 4061 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4062 | xfs_fileoff_t obno; |
| 4063 | xfs_fileoff_t end; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4064 | xfs_extnum_t idx; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4065 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4066 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4067 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4068 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4069 | |
| 4070 | ASSERT(*nmap >= 1); |
| 4071 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4072 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4073 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4074 | |
| 4075 | if (unlikely(XFS_TEST_ERROR( |
| 4076 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4077 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 4078 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4079 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4080 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4084 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4085 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4086 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4087 | |
| 4088 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4089 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4090 | /* No CoW fork? Return a hole. */ |
| 4091 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 4092 | mval->br_startoff = bno; |
| 4093 | mval->br_startblock = HOLESTARTBLOCK; |
| 4094 | mval->br_blockcount = len; |
| 4095 | mval->br_state = XFS_EXT_NORM; |
| 4096 | *nmap = 1; |
| 4097 | return 0; |
| 4098 | } |
| 4099 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4100 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4101 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4102 | if (error) |
| 4103 | return error; |
| 4104 | } |
| 4105 | |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4106 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) |
| 4107 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4108 | end = bno + len; |
| 4109 | obno = bno; |
| 4110 | |
| 4111 | while (bno < end && n < *nmap) { |
| 4112 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4113 | if (eof) |
| 4114 | got.br_startoff = end; |
| 4115 | if (got.br_startoff > bno) { |
| 4116 | /* Reading in a hole. */ |
| 4117 | mval->br_startoff = bno; |
| 4118 | mval->br_startblock = HOLESTARTBLOCK; |
| 4119 | mval->br_blockcount = |
| 4120 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4121 | mval->br_state = XFS_EXT_NORM; |
| 4122 | bno += mval->br_blockcount; |
| 4123 | len -= mval->br_blockcount; |
| 4124 | mval++; |
| 4125 | n++; |
| 4126 | continue; |
| 4127 | } |
| 4128 | |
| 4129 | /* set up the extent map to return. */ |
| 4130 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4131 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4132 | |
| 4133 | /* If we're done, stop now. */ |
| 4134 | if (bno >= end || n >= *nmap) |
| 4135 | break; |
| 4136 | |
| 4137 | /* Else go on to the next record. */ |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4138 | if (!xfs_iext_get_extent(ifp, ++idx, &got)) |
| 4139 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4140 | } |
| 4141 | *nmap = n; |
| 4142 | return 0; |
| 4143 | } |
| 4144 | |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 4145 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4146 | xfs_bmapi_reserve_delalloc( |
| 4147 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4148 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4149 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4150 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4151 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4152 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4153 | xfs_extnum_t *lastx, |
| 4154 | int eof) |
| 4155 | { |
| 4156 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4157 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4158 | xfs_extlen_t alen; |
| 4159 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4160 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4161 | xfs_extlen_t extsz; |
| 4162 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4163 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4164 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4165 | /* |
| 4166 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 4167 | * tag the inode before we return. |
| 4168 | */ |
| 4169 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4170 | if (!eof) |
| 4171 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4172 | if (prealloc && alen >= len) |
| 4173 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4174 | |
| 4175 | /* Figure out the extent size, adjust alen */ |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 4176 | if (whichfork == XFS_COW_FORK) |
| 4177 | extsz = xfs_get_cowextsz_hint(ip); |
| 4178 | else |
| 4179 | extsz = xfs_get_extsz_hint(ip); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4180 | if (extsz) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 4181 | struct xfs_bmbt_irec prev; |
| 4182 | |
| 4183 | if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev)) |
| 4184 | prev.br_startoff = NULLFILEOFF; |
| 4185 | |
| 4186 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4187 | 1, 0, &aoff, &alen); |
| 4188 | ASSERT(!error); |
| 4189 | } |
| 4190 | |
| 4191 | if (rt) |
| 4192 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4193 | |
| 4194 | /* |
| 4195 | * Make a transaction-less quota reservation for delayed allocation |
| 4196 | * blocks. This number gets adjusted later. We return if we haven't |
| 4197 | * allocated blocks already inside this loop. |
| 4198 | */ |
| 4199 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4200 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4201 | if (error) |
| 4202 | return error; |
| 4203 | |
| 4204 | /* |
| 4205 | * Split changing sb for alen and indlen since they could be coming |
| 4206 | * from different places. |
| 4207 | */ |
| 4208 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4209 | ASSERT(indlen > 0); |
| 4210 | |
| 4211 | if (rt) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4212 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4213 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4214 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | if (error) |
| 4218 | goto out_unreserve_quota; |
| 4219 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4220 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4221 | if (error) |
| 4222 | goto out_unreserve_blocks; |
| 4223 | |
| 4224 | |
| 4225 | ip->i_delayed_blks += alen; |
| 4226 | |
| 4227 | got->br_startoff = aoff; |
| 4228 | got->br_startblock = nullstartblock(indlen); |
| 4229 | got->br_blockcount = alen; |
| 4230 | got->br_state = XFS_EXT_NORM; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4231 | xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4232 | |
| 4233 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4234 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay |
| 4235 | * might have merged it into one of the neighbouring ones. |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4236 | */ |
| 4237 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); |
| 4238 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4239 | /* |
| 4240 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 4241 | * preallocation can occur at the start or end of the extent, even when |
| 4242 | * prealloc == 0, so we must also check the aligned offset and length. |
| 4243 | */ |
| 4244 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4245 | xfs_inode_set_eofblocks_tag(ip); |
| 4246 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4247 | xfs_inode_set_cowblocks_tag(ip); |
| 4248 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4249 | ASSERT(got->br_startoff <= aoff); |
| 4250 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); |
| 4251 | ASSERT(isnullstartblock(got->br_startblock)); |
| 4252 | ASSERT(got->br_state == XFS_EXT_NORM); |
| 4253 | return 0; |
| 4254 | |
| 4255 | out_unreserve_blocks: |
| 4256 | if (rt) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4257 | xfs_mod_frextents(mp, extsz); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4258 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4259 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4260 | out_unreserve_quota: |
| 4261 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 4262 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4263 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4264 | return error; |
| 4265 | } |
| 4266 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4267 | static int |
| 4268 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4269 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4270 | { |
| 4271 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4272 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4273 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4274 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4275 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4276 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4277 | ASSERT(bma->length > 0); |
| 4278 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4279 | /* |
| 4280 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4281 | * for in this bmap call but that wouldn't be as good. |
| 4282 | */ |
| 4283 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4284 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4285 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | 0e8d630 | 2016-11-24 11:40:32 +1100 | [diff] [blame] | 4286 | if (bma->idx) { |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4287 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4288 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4289 | } |
| 4290 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4291 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4292 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4293 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4294 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4298 | * Set the data type being allocated. For the data fork, the first data |
| 4299 | * in the file is treated differently to all other allocations. For the |
| 4300 | * attribute fork, we only need to ensure the allocated range is not on |
| 4301 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4302 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4303 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4304 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4305 | if (whichfork == XFS_DATA_FORK) { |
| 4306 | if (bma->offset == 0) |
| 4307 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4308 | else |
| 4309 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4310 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4311 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4312 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4313 | } |
| 4314 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4315 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4316 | |
| 4317 | /* |
| 4318 | * Only want to do the alignment at the eof if it is userdata and |
| 4319 | * allocation length is larger than a stripe unit. |
| 4320 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4321 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4322 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4323 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4324 | if (error) |
| 4325 | return error; |
| 4326 | } |
| 4327 | |
| 4328 | error = xfs_bmap_alloc(bma); |
| 4329 | if (error) |
| 4330 | return error; |
| 4331 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4332 | if (bma->cur) |
| 4333 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4334 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4335 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4336 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4337 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4338 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4339 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4340 | } |
| 4341 | /* |
| 4342 | * Bump the number of extents we've allocated |
| 4343 | * in this call. |
| 4344 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4345 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4346 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4347 | if (bma->cur) |
| 4348 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4349 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4350 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4351 | bma->got.br_startoff = bma->offset; |
| 4352 | bma->got.br_startblock = bma->blkno; |
| 4353 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4354 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4355 | |
| 4356 | /* |
| 4357 | * A wasdelay extent has been initialized, so shouldn't be flagged |
| 4358 | * as unwritten. |
| 4359 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4360 | if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4361 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4362 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4363 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4364 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4365 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4366 | else |
| 4367 | error = xfs_bmap_add_extent_hole_real(bma, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4368 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4369 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4370 | if (error) |
| 4371 | return error; |
| 4372 | |
| 4373 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4374 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4375 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4376 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4377 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4378 | 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] | 4379 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4380 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4381 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4382 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4383 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4384 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4385 | return 0; |
| 4386 | } |
| 4387 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4388 | STATIC int |
| 4389 | xfs_bmapi_convert_unwritten( |
| 4390 | struct xfs_bmalloca *bma, |
| 4391 | struct xfs_bmbt_irec *mval, |
| 4392 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4393 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4394 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4395 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4396 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4397 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4398 | int error; |
| 4399 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4400 | /* check if we need to do unwritten->real conversion */ |
| 4401 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4402 | (flags & XFS_BMAPI_PREALLOC)) |
| 4403 | return 0; |
| 4404 | |
| 4405 | /* check if we need to do real->unwritten conversion */ |
| 4406 | if (mval->br_state == XFS_EXT_NORM && |
| 4407 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4408 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4409 | return 0; |
| 4410 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4411 | ASSERT(whichfork != XFS_COW_FORK); |
| 4412 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4413 | /* |
| 4414 | * Modify (by adding) the state flag, if writing. |
| 4415 | */ |
| 4416 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4417 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4418 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4419 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4420 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4421 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4422 | } |
| 4423 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4424 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4425 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4426 | /* |
| 4427 | * Before insertion into the bmbt, zero the range being converted |
| 4428 | * if required. |
| 4429 | */ |
| 4430 | if (flags & XFS_BMAPI_ZERO) { |
| 4431 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4432 | mval->br_blockcount); |
| 4433 | if (error) |
| 4434 | return error; |
| 4435 | } |
| 4436 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4437 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4438 | &bma->cur, mval, bma->firstblock, bma->dfops, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4439 | &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4440 | /* |
| 4441 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4442 | * path because the conversion might not have done so (e.g., if the |
| 4443 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4444 | * in the transaction for the sake of fsync(), even if nothing has |
| 4445 | * changed, because fsync() will not force the log for this transaction |
| 4446 | * unless it sees the inode pinned. |
| 4447 | */ |
| 4448 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4449 | if (error) |
| 4450 | return error; |
| 4451 | |
| 4452 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4453 | * Update our extent pointer, given that |
| 4454 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4455 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4456 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4457 | 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] | 4458 | |
| 4459 | /* |
| 4460 | * We may have combined previously unwritten space with written space, |
| 4461 | * so generate another request. |
| 4462 | */ |
| 4463 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4464 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4465 | return 0; |
| 4466 | } |
| 4467 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4468 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4469 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4470 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4471 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4472 | * locking problems. |
| 4473 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | * The returned value in "firstblock" from the first call in a transaction |
| 4475 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4476 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4477 | * the first call in "total"; if no allocation group has that many free |
| 4478 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4479 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4480 | int |
| 4481 | xfs_bmapi_write( |
| 4482 | struct xfs_trans *tp, /* transaction pointer */ |
| 4483 | struct xfs_inode *ip, /* incore inode */ |
| 4484 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4485 | xfs_filblks_t len, /* length to map in file */ |
| 4486 | int flags, /* XFS_BMAPI_... */ |
| 4487 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4488 | controls a.g. for allocs */ |
| 4489 | xfs_extlen_t total, /* total blocks needed */ |
| 4490 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4491 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4492 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4494 | struct xfs_mount *mp = ip->i_mount; |
| 4495 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4496 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4497 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4498 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4499 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4500 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4501 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4502 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4505 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4506 | int orig_flags; /* original flags arg value */ |
| 4507 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4508 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4509 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | |
| 4511 | orig_bno = bno; |
| 4512 | orig_len = len; |
| 4513 | orig_flags = flags; |
| 4514 | orig_mval = mval; |
| 4515 | orig_nmap = *nmap; |
| 4516 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4517 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4520 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4521 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
| 4522 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4523 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4524 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4525 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4526 | ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK); |
| 4527 | ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP)); |
| 4528 | ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP)); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4529 | ASSERT(!(flags & XFS_BMAPI_PREALLOC) || whichfork != XFS_COW_FORK); |
| 4530 | ASSERT(!(flags & XFS_BMAPI_CONVERT) || whichfork != XFS_COW_FORK); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4531 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4532 | /* zeroing is for currently only for data extents, not metadata */ |
| 4533 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4534 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4535 | /* |
| 4536 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4537 | * but it makes no sense to do both at once. This would result in |
| 4538 | * zeroing the unwritten extent twice, but it still being an |
| 4539 | * unwritten extent.... |
| 4540 | */ |
| 4541 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4542 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | if (unlikely(XFS_TEST_ERROR( |
| 4545 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4546 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4548 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4549 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4553 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4556 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4557 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4558 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4559 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4561 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4563 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4564 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4565 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4569 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4570 | if (error) |
| 4571 | goto error0; |
| 4572 | } |
| 4573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | n = 0; |
| 4575 | end = bno + len; |
| 4576 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4577 | |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4578 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got)) |
| 4579 | eof = true; |
| 4580 | if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev)) |
| 4581 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4582 | bma.tp = tp; |
| 4583 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4584 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4585 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4586 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4587 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4590 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4591 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4592 | /* in hole or beyoned EOF? */ |
| 4593 | if (eof || bma.got.br_startoff > bno) { |
| 4594 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4595 | /* |
| 4596 | * For the COW fork we can reasonably get a |
| 4597 | * request for converting an extent that races |
| 4598 | * with other threads already having converted |
| 4599 | * part of it, as there converting COW to |
| 4600 | * regular blocks is not protected using the |
| 4601 | * IOLOCK. |
| 4602 | */ |
| 4603 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4604 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4605 | error = -EIO; |
| 4606 | goto error0; |
| 4607 | } |
| 4608 | |
| 4609 | if (eof || bno >= end) |
| 4610 | break; |
| 4611 | } else { |
| 4612 | need_alloc = true; |
| 4613 | } |
| 4614 | } else { |
| 4615 | /* |
| 4616 | * Make sure we only reflink into a hole. |
| 4617 | */ |
| 4618 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
| 4619 | if (isnullstartblock(bma.got.br_startblock)) |
| 4620 | wasdelay = true; |
| 4621 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4622 | |
| 4623 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | * First, deal with the hole before the allocated space |
| 4625 | * that we found, if any. |
| 4626 | */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4627 | if (need_alloc || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4628 | bma.eof = eof; |
| 4629 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4630 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4631 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4632 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4633 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4634 | /* |
| 4635 | * There's a 32/64 bit type mismatch between the |
| 4636 | * allocation length request (which can be 64 bits in |
| 4637 | * length) and the bma length request, which is |
| 4638 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4639 | * check for 32-bit overflows and handle them here. |
| 4640 | */ |
| 4641 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4642 | bma.length = MAXEXTLEN; |
| 4643 | else |
| 4644 | bma.length = len; |
| 4645 | |
| 4646 | ASSERT(len > 0); |
| 4647 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4648 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | if (error) |
| 4650 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4651 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4652 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4653 | |
| 4654 | /* |
| 4655 | * If this is a CoW allocation, record the data in |
| 4656 | * the refcount btree for orphan recovery. |
| 4657 | */ |
| 4658 | if (whichfork == XFS_COW_FORK) { |
| 4659 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4660 | bma.blkno, bma.length); |
| 4661 | if (error) |
| 4662 | goto error0; |
| 4663 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4665 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4666 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4667 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4668 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4670 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4671 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4672 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4673 | continue; |
| 4674 | if (error) |
| 4675 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4677 | /* update the extent map to return */ |
| 4678 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | /* |
| 4681 | * If we're done, stop now. Stop when we've allocated |
| 4682 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4683 | * the transaction may get too big. |
| 4684 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4685 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4687 | |
| 4688 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4689 | bma.prev = bma.got; |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4690 | if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got)) |
| 4691 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | /* |
| 4696 | * Transform from btree to extents, give it cur. |
| 4697 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4698 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4699 | int tmp_logflags = 0; |
| 4700 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4701 | ASSERT(bma.cur); |
| 4702 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4704 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4705 | if (error) |
| 4706 | goto error0; |
| 4707 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4710 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4711 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | error0: |
| 4714 | /* |
| 4715 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4716 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4718 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4720 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4721 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4723 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | /* |
| 4725 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4726 | * detecting a case where the data is changed, there's an error, |
| 4727 | * and it's not logged so we don't shutdown when we should. |
| 4728 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4729 | if (bma.logflags) |
| 4730 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4731 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4732 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | if (!error) { |
| 4734 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4735 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4736 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4737 | bma.cur->bc_private.b.firstblock) || |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4738 | (dfops->dop_low && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4740 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4741 | bma.cur->bc_private.b.firstblock))); |
| 4742 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4744 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4745 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4746 | } |
| 4747 | if (!error) |
| 4748 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4749 | orig_nmap, *nmap); |
| 4750 | return error; |
| 4751 | } |
| 4752 | |
| 4753 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4754 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4755 | * indlen reservation must be shared across the two new extents that are left |
| 4756 | * behind. |
| 4757 | * |
| 4758 | * Given the original reservation and the worst case indlen for the two new |
| 4759 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4760 | * reservation fairly across the two new extents. If necessary, steal available |
| 4761 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4762 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4763 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4764 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4765 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4766 | xfs_bmap_split_indlen( |
| 4767 | xfs_filblks_t ores, /* original res. */ |
| 4768 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4769 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4770 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4771 | { |
| 4772 | xfs_filblks_t len1 = *indlen1; |
| 4773 | xfs_filblks_t len2 = *indlen2; |
| 4774 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4775 | xfs_filblks_t stolen = 0; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4776 | |
| 4777 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4778 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4779 | * indlen for both new extents. |
| 4780 | */ |
| 4781 | while (nres > ores && avail) { |
| 4782 | nres--; |
| 4783 | avail--; |
| 4784 | stolen++; |
| 4785 | } |
| 4786 | |
| 4787 | /* |
| 4788 | * The only blocks available are those reserved for the original |
| 4789 | * extent and what we can steal from the extent being removed. |
| 4790 | * If this still isn't enough to satisfy the combined |
| 4791 | * requirements for the two new extents, skim blocks off of each |
| 4792 | * of the new reservations until they match what is available. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4793 | */ |
| 4794 | while (nres > ores) { |
| 4795 | if (len1) { |
| 4796 | len1--; |
| 4797 | nres--; |
| 4798 | } |
| 4799 | if (nres == ores) |
| 4800 | break; |
| 4801 | if (len2) { |
| 4802 | len2--; |
| 4803 | nres--; |
| 4804 | } |
| 4805 | } |
| 4806 | |
| 4807 | *indlen1 = len1; |
| 4808 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4809 | |
| 4810 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4811 | } |
| 4812 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4813 | int |
| 4814 | xfs_bmap_del_extent_delay( |
| 4815 | struct xfs_inode *ip, |
| 4816 | int whichfork, |
| 4817 | xfs_extnum_t *idx, |
| 4818 | struct xfs_bmbt_irec *got, |
| 4819 | struct xfs_bmbt_irec *del) |
| 4820 | { |
| 4821 | struct xfs_mount *mp = ip->i_mount; |
| 4822 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4823 | struct xfs_bmbt_irec new; |
| 4824 | int64_t da_old, da_new, da_diff = 0; |
| 4825 | xfs_fileoff_t del_endoff, got_endoff; |
| 4826 | xfs_filblks_t got_indlen, new_indlen, stolen; |
| 4827 | int error = 0, state = 0; |
| 4828 | bool isrt; |
| 4829 | |
| 4830 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4831 | |
| 4832 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4833 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4834 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4835 | da_old = startblockval(got->br_startblock); |
| 4836 | da_new = 0; |
| 4837 | |
| 4838 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 4839 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4840 | ASSERT(del->br_blockcount > 0); |
| 4841 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4842 | ASSERT(got_endoff >= del_endoff); |
| 4843 | |
| 4844 | if (isrt) { |
| 4845 | int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
| 4846 | |
| 4847 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4848 | xfs_mod_frextents(mp, rtexts); |
| 4849 | } |
| 4850 | |
| 4851 | /* |
| 4852 | * Update the inode delalloc counter now and wait to update the |
| 4853 | * sb counters as we might have to borrow some blocks for the |
| 4854 | * indirect block accounting. |
| 4855 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4856 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4857 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4858 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4859 | if (error) |
| 4860 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4861 | ip->i_delayed_blks -= del->br_blockcount; |
| 4862 | |
| 4863 | if (whichfork == XFS_COW_FORK) |
| 4864 | state |= BMAP_COWFORK; |
| 4865 | |
| 4866 | if (got->br_startoff == del->br_startoff) |
| 4867 | state |= BMAP_LEFT_CONTIG; |
| 4868 | if (got_endoff == del_endoff) |
| 4869 | state |= BMAP_RIGHT_CONTIG; |
| 4870 | |
| 4871 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 4872 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 4873 | /* |
| 4874 | * Matches the whole extent. Delete the entry. |
| 4875 | */ |
| 4876 | xfs_iext_remove(ip, *idx, 1, state); |
| 4877 | --*idx; |
| 4878 | break; |
| 4879 | case BMAP_LEFT_CONTIG: |
| 4880 | /* |
| 4881 | * Deleting the first part of the extent. |
| 4882 | */ |
| 4883 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4884 | got->br_startoff = del_endoff; |
| 4885 | got->br_blockcount -= del->br_blockcount; |
| 4886 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4887 | got->br_blockcount), da_old); |
| 4888 | got->br_startblock = nullstartblock((int)da_new); |
| 4889 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4890 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4891 | break; |
| 4892 | case BMAP_RIGHT_CONTIG: |
| 4893 | /* |
| 4894 | * Deleting the last part of the extent. |
| 4895 | */ |
| 4896 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4897 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4898 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4899 | got->br_blockcount), da_old); |
| 4900 | got->br_startblock = nullstartblock((int)da_new); |
| 4901 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4902 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4903 | break; |
| 4904 | case 0: |
| 4905 | /* |
| 4906 | * Deleting the middle of the extent. |
| 4907 | * |
| 4908 | * Distribute the original indlen reservation across the two new |
| 4909 | * extents. Steal blocks from the deleted extent if necessary. |
| 4910 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4911 | * Warn if either of the new indlen reservations is zero as this |
| 4912 | * can lead to delalloc problems. |
| 4913 | */ |
| 4914 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4915 | |
| 4916 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4917 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4918 | |
| 4919 | new.br_blockcount = got_endoff - del_endoff; |
| 4920 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4921 | |
| 4922 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4923 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4924 | del->br_blockcount); |
| 4925 | |
| 4926 | got->br_startblock = nullstartblock((int)got_indlen); |
| 4927 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4928 | trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_); |
| 4929 | |
| 4930 | new.br_startoff = del_endoff; |
| 4931 | new.br_state = got->br_state; |
| 4932 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4933 | |
| 4934 | ++*idx; |
| 4935 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 4936 | |
| 4937 | da_new = got_indlen + new_indlen - stolen; |
| 4938 | del->br_blockcount -= stolen; |
| 4939 | break; |
| 4940 | } |
| 4941 | |
| 4942 | ASSERT(da_old >= da_new); |
| 4943 | da_diff = da_old - da_new; |
| 4944 | if (!isrt) |
| 4945 | da_diff += del->br_blockcount; |
| 4946 | if (da_diff) |
| 4947 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4948 | return error; |
| 4949 | } |
| 4950 | |
| 4951 | void |
| 4952 | xfs_bmap_del_extent_cow( |
| 4953 | struct xfs_inode *ip, |
| 4954 | xfs_extnum_t *idx, |
| 4955 | struct xfs_bmbt_irec *got, |
| 4956 | struct xfs_bmbt_irec *del) |
| 4957 | { |
| 4958 | struct xfs_mount *mp = ip->i_mount; |
| 4959 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4960 | struct xfs_bmbt_irec new; |
| 4961 | xfs_fileoff_t del_endoff, got_endoff; |
| 4962 | int state = BMAP_COWFORK; |
| 4963 | |
| 4964 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4965 | |
| 4966 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4967 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4968 | |
| 4969 | ASSERT(*idx >= 0); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 4970 | ASSERT(*idx <= xfs_iext_count(ifp)); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4971 | ASSERT(del->br_blockcount > 0); |
| 4972 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4973 | ASSERT(got_endoff >= del_endoff); |
| 4974 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4975 | |
| 4976 | if (got->br_startoff == del->br_startoff) |
| 4977 | state |= BMAP_LEFT_CONTIG; |
| 4978 | if (got_endoff == del_endoff) |
| 4979 | state |= BMAP_RIGHT_CONTIG; |
| 4980 | |
| 4981 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 4982 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 4983 | /* |
| 4984 | * Matches the whole extent. Delete the entry. |
| 4985 | */ |
| 4986 | xfs_iext_remove(ip, *idx, 1, state); |
| 4987 | --*idx; |
| 4988 | break; |
| 4989 | case BMAP_LEFT_CONTIG: |
| 4990 | /* |
| 4991 | * Deleting the first part of the extent. |
| 4992 | */ |
| 4993 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4994 | got->br_startoff = del_endoff; |
| 4995 | got->br_blockcount -= del->br_blockcount; |
| 4996 | got->br_startblock = del->br_startblock + del->br_blockcount; |
| 4997 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 4998 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4999 | break; |
| 5000 | case BMAP_RIGHT_CONTIG: |
| 5001 | /* |
| 5002 | * Deleting the last part of the extent. |
| 5003 | */ |
| 5004 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5005 | got->br_blockcount -= del->br_blockcount; |
| 5006 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5007 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5008 | break; |
| 5009 | case 0: |
| 5010 | /* |
| 5011 | * Deleting the middle of the extent. |
| 5012 | */ |
| 5013 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5014 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 5015 | xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got); |
| 5016 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5017 | |
| 5018 | new.br_startoff = del_endoff; |
| 5019 | new.br_blockcount = got_endoff - del_endoff; |
| 5020 | new.br_state = got->br_state; |
| 5021 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 5022 | |
| 5023 | ++*idx; |
| 5024 | xfs_iext_insert(ip, *idx, 1, &new, state); |
| 5025 | break; |
| 5026 | } |
| 5027 | } |
| 5028 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5029 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5030 | * Called by xfs_bmapi to update file extent records and the btree |
| 5031 | * after removing space (or undoing a delayed allocation). |
| 5032 | */ |
| 5033 | STATIC int /* error */ |
| 5034 | xfs_bmap_del_extent( |
| 5035 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5036 | xfs_trans_t *tp, /* current transaction pointer */ |
| 5037 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5038 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5039 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 5040 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 5041 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5042 | int whichfork, /* data or attr fork */ |
| 5043 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5044 | { |
| 5045 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 5046 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 5047 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 5048 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 5049 | int delay; /* current block is delayed allocated */ |
| 5050 | int do_fx; /* free extent at end of routine */ |
| 5051 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
| 5052 | int error; /* error return value */ |
| 5053 | int flags; /* inode logging flags */ |
| 5054 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 5055 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 5056 | int i; /* temp state */ |
| 5057 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5058 | xfs_mount_t *mp; /* mount structure */ |
| 5059 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 5060 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 5061 | /* REFERENCED */ |
| 5062 | uint qfield; /* quota field to update */ |
| 5063 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 5064 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 5065 | int state = 0; |
| 5066 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5067 | mp = ip->i_mount; |
| 5068 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5069 | |
| 5070 | if (whichfork == XFS_ATTR_FORK) |
| 5071 | state |= BMAP_ATTRFORK; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5072 | else if (whichfork == XFS_COW_FORK) |
| 5073 | state |= BMAP_COWFORK; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5074 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5075 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5076 | ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp))); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5077 | ASSERT(del->br_blockcount > 0); |
| 5078 | ep = xfs_iext_get_ext(ifp, *idx); |
| 5079 | xfs_bmbt_get_all(ep, &got); |
| 5080 | ASSERT(got.br_startoff <= del->br_startoff); |
| 5081 | del_endoff = del->br_startoff + del->br_blockcount; |
| 5082 | got_endoff = got.br_startoff + got.br_blockcount; |
| 5083 | ASSERT(got_endoff >= del_endoff); |
| 5084 | delay = isnullstartblock(got.br_startblock); |
| 5085 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
| 5086 | flags = 0; |
| 5087 | qfield = 0; |
| 5088 | error = 0; |
| 5089 | /* |
| 5090 | * If deleting a real allocation, must free up the disk space. |
| 5091 | */ |
| 5092 | if (!delay) { |
| 5093 | flags = XFS_ILOG_CORE; |
| 5094 | /* |
| 5095 | * Realtime allocation. Free it and record di_nblocks update. |
| 5096 | */ |
| 5097 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 5098 | xfs_fsblock_t bno; |
| 5099 | xfs_filblks_t len; |
| 5100 | |
| 5101 | ASSERT(do_mod(del->br_blockcount, |
| 5102 | mp->m_sb.sb_rextsize) == 0); |
| 5103 | ASSERT(do_mod(del->br_startblock, |
| 5104 | mp->m_sb.sb_rextsize) == 0); |
| 5105 | bno = del->br_startblock; |
| 5106 | len = del->br_blockcount; |
| 5107 | do_div(bno, mp->m_sb.sb_rextsize); |
| 5108 | do_div(len, mp->m_sb.sb_rextsize); |
| 5109 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 5110 | if (error) |
| 5111 | goto done; |
| 5112 | do_fx = 0; |
| 5113 | nblks = len * mp->m_sb.sb_rextsize; |
| 5114 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 5115 | } |
| 5116 | /* |
| 5117 | * Ordinary allocation. |
| 5118 | */ |
| 5119 | else { |
| 5120 | do_fx = 1; |
| 5121 | nblks = del->br_blockcount; |
| 5122 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 5123 | } |
| 5124 | /* |
| 5125 | * Set up del_endblock and cur for later. |
| 5126 | */ |
| 5127 | del_endblock = del->br_startblock + del->br_blockcount; |
| 5128 | if (cur) { |
| 5129 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 5130 | got.br_startblock, got.br_blockcount, |
| 5131 | &i))) |
| 5132 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5133 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5134 | } |
| 5135 | da_old = da_new = 0; |
| 5136 | } else { |
| 5137 | da_old = startblockval(got.br_startblock); |
| 5138 | da_new = 0; |
| 5139 | nblks = 0; |
| 5140 | do_fx = 0; |
| 5141 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 5142 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5143 | /* |
| 5144 | * Set flag value to use in switch statement. |
| 5145 | * Left-contig is 2, right-contig is 1. |
| 5146 | */ |
| 5147 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 5148 | (got_endoff == del_endoff)) { |
| 5149 | case 3: |
| 5150 | /* |
| 5151 | * Matches the whole extent. Delete the entry. |
| 5152 | */ |
Darrick J. Wong | 4862cfe | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 5153 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5154 | xfs_iext_remove(ip, *idx, 1, |
| 5155 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 5156 | --*idx; |
| 5157 | if (delay) |
| 5158 | break; |
| 5159 | |
| 5160 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5161 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5162 | flags |= XFS_ILOG_CORE; |
| 5163 | if (!cur) { |
| 5164 | flags |= xfs_ilog_fext(whichfork); |
| 5165 | break; |
| 5166 | } |
| 5167 | if ((error = xfs_btree_delete(cur, &i))) |
| 5168 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5169 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5170 | break; |
| 5171 | |
| 5172 | case 2: |
| 5173 | /* |
| 5174 | * Deleting the first part of the extent. |
| 5175 | */ |
| 5176 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5177 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 5178 | temp = got.br_blockcount - del->br_blockcount; |
| 5179 | xfs_bmbt_set_blockcount(ep, temp); |
| 5180 | if (delay) { |
| 5181 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 5182 | da_old); |
| 5183 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 5184 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5185 | da_new = temp; |
| 5186 | break; |
| 5187 | } |
| 5188 | xfs_bmbt_set_startblock(ep, del_endblock); |
| 5189 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5190 | if (!cur) { |
| 5191 | flags |= xfs_ilog_fext(whichfork); |
| 5192 | break; |
| 5193 | } |
| 5194 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 5195 | got.br_blockcount - del->br_blockcount, |
| 5196 | got.br_state))) |
| 5197 | goto done; |
| 5198 | break; |
| 5199 | |
| 5200 | case 1: |
| 5201 | /* |
| 5202 | * Deleting the last part of the extent. |
| 5203 | */ |
| 5204 | temp = got.br_blockcount - del->br_blockcount; |
| 5205 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5206 | xfs_bmbt_set_blockcount(ep, temp); |
| 5207 | if (delay) { |
| 5208 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 5209 | da_old); |
| 5210 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 5211 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5212 | da_new = temp; |
| 5213 | break; |
| 5214 | } |
| 5215 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5216 | if (!cur) { |
| 5217 | flags |= xfs_ilog_fext(whichfork); |
| 5218 | break; |
| 5219 | } |
| 5220 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 5221 | got.br_startblock, |
| 5222 | got.br_blockcount - del->br_blockcount, |
| 5223 | got.br_state))) |
| 5224 | goto done; |
| 5225 | break; |
| 5226 | |
| 5227 | case 0: |
| 5228 | /* |
| 5229 | * Deleting the middle of the extent. |
| 5230 | */ |
| 5231 | temp = del->br_startoff - got.br_startoff; |
| 5232 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 5233 | xfs_bmbt_set_blockcount(ep, temp); |
| 5234 | new.br_startoff = del_endoff; |
| 5235 | temp2 = got_endoff - del_endoff; |
| 5236 | new.br_blockcount = temp2; |
| 5237 | new.br_state = got.br_state; |
| 5238 | if (!delay) { |
| 5239 | new.br_startblock = del_endblock; |
| 5240 | flags |= XFS_ILOG_CORE; |
| 5241 | if (cur) { |
| 5242 | if ((error = xfs_bmbt_update(cur, |
| 5243 | got.br_startoff, |
| 5244 | got.br_startblock, temp, |
| 5245 | got.br_state))) |
| 5246 | goto done; |
| 5247 | if ((error = xfs_btree_increment(cur, 0, &i))) |
| 5248 | goto done; |
| 5249 | cur->bc_rec.b = new; |
| 5250 | error = xfs_btree_insert(cur, &i); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5251 | if (error && error != -ENOSPC) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5252 | goto done; |
| 5253 | /* |
| 5254 | * If get no-space back from btree insert, |
| 5255 | * it tried a split, and we have a zero |
| 5256 | * block reservation. |
| 5257 | * Fix up our state and return the error. |
| 5258 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5259 | if (error == -ENOSPC) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5260 | /* |
| 5261 | * Reset the cursor, don't trust |
| 5262 | * it after any insert operation. |
| 5263 | */ |
| 5264 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 5265 | got.br_startoff, |
| 5266 | got.br_startblock, |
| 5267 | temp, &i))) |
| 5268 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5269 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 5270 | i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5271 | /* |
| 5272 | * Update the btree record back |
| 5273 | * to the original value. |
| 5274 | */ |
| 5275 | if ((error = xfs_bmbt_update(cur, |
| 5276 | got.br_startoff, |
| 5277 | got.br_startblock, |
| 5278 | got.br_blockcount, |
| 5279 | got.br_state))) |
| 5280 | goto done; |
| 5281 | /* |
| 5282 | * Reset the extent record back |
| 5283 | * to the original value. |
| 5284 | */ |
| 5285 | xfs_bmbt_set_blockcount(ep, |
| 5286 | got.br_blockcount); |
| 5287 | flags = 0; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5288 | error = -ENOSPC; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5289 | goto done; |
| 5290 | } |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5291 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5292 | } else |
| 5293 | flags |= xfs_ilog_fext(whichfork); |
| 5294 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5295 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5296 | } else { |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5297 | xfs_filblks_t stolen; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5298 | ASSERT(whichfork == XFS_DATA_FORK); |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5299 | |
| 5300 | /* |
| 5301 | * Distribute the original indlen reservation across the |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5302 | * two new extents. Steal blocks from the deleted extent |
| 5303 | * if necessary. Stealing blocks simply fudges the |
| 5304 | * fdblocks accounting in xfs_bunmapi(). |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5305 | */ |
| 5306 | temp = xfs_bmap_worst_indlen(ip, got.br_blockcount); |
| 5307 | temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5308 | stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2, |
| 5309 | del->br_blockcount); |
| 5310 | da_new = temp + temp2 - stolen; |
| 5311 | del->br_blockcount -= stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5312 | |
| 5313 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5314 | * Set the reservation for each extent. Warn if either |
| 5315 | * is zero as this can lead to delalloc problems. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5316 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 5317 | WARN_ON_ONCE(!temp || !temp2); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5318 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5319 | new.br_startblock = nullstartblock((int)temp2); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5320 | } |
| 5321 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 5322 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 5323 | ++*idx; |
| 5324 | break; |
| 5325 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5326 | |
| 5327 | /* remove reverse mapping */ |
| 5328 | if (!delay) { |
| 5329 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5330 | if (error) |
| 5331 | goto done; |
| 5332 | } |
| 5333 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5334 | /* |
| 5335 | * If we need to, add to list of extents to delete. |
| 5336 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5337 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5338 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5339 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5340 | if (error) |
| 5341 | goto done; |
| 5342 | } else |
| 5343 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5344 | del->br_blockcount, NULL); |
| 5345 | } |
| 5346 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5347 | /* |
| 5348 | * Adjust inode # blocks in the file. |
| 5349 | */ |
| 5350 | if (nblks) |
| 5351 | ip->i_d.di_nblocks -= nblks; |
| 5352 | /* |
| 5353 | * Adjust quota data. |
| 5354 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5355 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5356 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5357 | |
| 5358 | /* |
| 5359 | * Account for change in delayed indirect blocks. |
| 5360 | * Nothing to do for disk quota accounting here. |
| 5361 | */ |
| 5362 | ASSERT(da_old >= da_new); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 5363 | if (da_old > da_new) |
| 5364 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5365 | done: |
| 5366 | *logflagsp = flags; |
| 5367 | return error; |
| 5368 | } |
| 5369 | |
| 5370 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | * Unmap (remove) blocks from a file. |
| 5372 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5373 | * that value. If not all extents in the block range can be removed then |
| 5374 | * *done is set. |
| 5375 | */ |
| 5376 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5377 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5378 | xfs_trans_t *tp, /* transaction pointer */ |
| 5379 | struct xfs_inode *ip, /* incore inode */ |
| 5380 | xfs_fileoff_t bno, /* starting offset to unmap */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5381 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | int flags, /* misc flags */ |
| 5383 | xfs_extnum_t nexts, /* number of extents max */ |
| 5384 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5385 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5386 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | { |
| 5388 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5389 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | int error; /* error return value */ |
| 5391 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5392 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5394 | int isrt; /* freeing in rt area */ |
| 5395 | xfs_extnum_t lastx; /* last extent index used */ |
| 5396 | int logflags; /* transaction logging flags */ |
| 5397 | xfs_extlen_t mod; /* rt extent offset */ |
| 5398 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5400 | int tmp_logflags; /* partial logging flags */ |
| 5401 | int wasdel; /* was a delayed alloc extent */ |
| 5402 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5403 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5404 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5406 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5407 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5408 | whichfork = xfs_bmapi_whichfork(flags); |
| 5409 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5411 | if (unlikely( |
| 5412 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5413 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5414 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5415 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5416 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | } |
| 5418 | mp = ip->i_mount; |
| 5419 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5420 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5421 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5422 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | ASSERT(len > 0); |
| 5424 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5427 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5428 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5429 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5430 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | return 0; |
| 5432 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5433 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5434 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5435 | start = bno; |
| 5436 | bno = start + len - 1; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | /* |
| 5439 | * Check to see if the given block number is past the end of the |
| 5440 | * file, back up to the last block if so... |
| 5441 | */ |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5442 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) { |
| 5443 | ASSERT(lastx > 0); |
| 5444 | xfs_iext_get_extent(ifp, --lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5446 | } |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | logflags = 0; |
| 5449 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5450 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5451 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5453 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | cur->bc_private.b.flags = 0; |
| 5455 | } else |
| 5456 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5457 | |
| 5458 | if (isrt) { |
| 5459 | /* |
| 5460 | * Synchronize by locking the bitmap inode. |
| 5461 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5462 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5463 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5464 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5465 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | extno = 0; |
| 5469 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5470 | (nexts == 0 || extno < nexts)) { |
| 5471 | /* |
| 5472 | * Is the found extent after a hole in which bno lives? |
| 5473 | * Just back up to the previous extent, if so. |
| 5474 | */ |
| 5475 | if (got.br_startoff > bno) { |
| 5476 | if (--lastx < 0) |
| 5477 | break; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5478 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | } |
| 5480 | /* |
| 5481 | * Is the last block of this extent before the range |
| 5482 | * we're supposed to delete? If so, we're done. |
| 5483 | */ |
| 5484 | bno = XFS_FILEOFF_MIN(bno, |
| 5485 | got.br_startoff + got.br_blockcount - 1); |
| 5486 | if (bno < start) |
| 5487 | break; |
| 5488 | /* |
| 5489 | * Then deal with the (possibly delayed) allocated space |
| 5490 | * we found. |
| 5491 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5493 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | if (got.br_startoff < start) { |
| 5495 | del.br_startoff = start; |
| 5496 | del.br_blockcount -= start - got.br_startoff; |
| 5497 | if (!wasdel) |
| 5498 | del.br_startblock += start - got.br_startoff; |
| 5499 | } |
| 5500 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5501 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5502 | sum = del.br_startblock + del.br_blockcount; |
| 5503 | if (isrt && |
| 5504 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5505 | /* |
| 5506 | * Realtime extent not lined up at the end. |
| 5507 | * The extent could have been split into written |
| 5508 | * and unwritten pieces, or we could just be |
| 5509 | * unmapping part of it. But we can't really |
| 5510 | * get rid of part of a realtime extent. |
| 5511 | */ |
| 5512 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5513 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | /* |
| 5515 | * This piece is unwritten, or we're not |
| 5516 | * using unwritten extents. Skip over it. |
| 5517 | */ |
| 5518 | ASSERT(bno >= mod); |
| 5519 | bno -= mod > del.br_blockcount ? |
| 5520 | del.br_blockcount : mod; |
| 5521 | if (bno < got.br_startoff) { |
| 5522 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5523 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5524 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | } |
| 5526 | continue; |
| 5527 | } |
| 5528 | /* |
| 5529 | * It's written, turn it unwritten. |
| 5530 | * This is better than zeroing it. |
| 5531 | */ |
| 5532 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5533 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | /* |
| 5535 | * If this spans a realtime extent boundary, |
| 5536 | * chop it back to the start of the one we end at. |
| 5537 | */ |
| 5538 | if (del.br_blockcount > mod) { |
| 5539 | del.br_startoff += del.br_blockcount - mod; |
| 5540 | del.br_startblock += del.br_blockcount - mod; |
| 5541 | del.br_blockcount = mod; |
| 5542 | } |
| 5543 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5544 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5545 | &lastx, &cur, &del, firstblock, dfops, |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5546 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | if (error) |
| 5548 | goto error0; |
| 5549 | goto nodelete; |
| 5550 | } |
| 5551 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5552 | /* |
| 5553 | * Realtime extent is lined up at the end but not |
| 5554 | * at the front. We'll get rid of full extents if |
| 5555 | * we can. |
| 5556 | */ |
| 5557 | mod = mp->m_sb.sb_rextsize - mod; |
| 5558 | if (del.br_blockcount > mod) { |
| 5559 | del.br_blockcount -= mod; |
| 5560 | del.br_startoff += mod; |
| 5561 | del.br_startblock += mod; |
| 5562 | } else if ((del.br_startoff == start && |
| 5563 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5564 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5565 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | /* |
| 5567 | * Can't make it unwritten. There isn't |
| 5568 | * a full extent here so just skip it. |
| 5569 | */ |
| 5570 | ASSERT(bno >= del.br_blockcount); |
| 5571 | bno -= del.br_blockcount; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5572 | if (got.br_startoff > bno && --lastx >= 0) |
| 5573 | xfs_iext_get_extent(ifp, lastx, &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | continue; |
| 5575 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5576 | struct xfs_bmbt_irec prev; |
| 5577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | /* |
| 5579 | * This one is already unwritten. |
| 5580 | * It must have a written left neighbor. |
| 5581 | * Unwrite the killed part of that one and |
| 5582 | * try again. |
| 5583 | */ |
| 5584 | ASSERT(lastx > 0); |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5585 | xfs_iext_get_extent(ifp, lastx - 1, &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5587 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | ASSERT(del.br_startblock == |
| 5589 | prev.br_startblock + prev.br_blockcount); |
| 5590 | if (prev.br_startoff < start) { |
| 5591 | mod = start - prev.br_startoff; |
| 5592 | prev.br_blockcount -= mod; |
| 5593 | prev.br_startblock += mod; |
| 5594 | prev.br_startoff = start; |
| 5595 | } |
| 5596 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5597 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5598 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5599 | ip, &lastx, &cur, &prev, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5600 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | if (error) |
| 5602 | goto error0; |
| 5603 | goto nodelete; |
| 5604 | } else { |
| 5605 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5606 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5607 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5608 | ip, &lastx, &cur, &del, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5609 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | if (error) |
| 5611 | goto error0; |
| 5612 | goto nodelete; |
| 5613 | } |
| 5614 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | /* |
| 5617 | * If it's the case where the directory code is running |
| 5618 | * with no block reservation, and the deleted block is in |
| 5619 | * the middle of its extent, and the resulting insert |
| 5620 | * of an extent would cause transformation to btree format, |
| 5621 | * then reject it. The calling code will then swap |
| 5622 | * blocks around instead. |
| 5623 | * We have to do this now, rather than waiting for the |
| 5624 | * conversion to btree format, since the transaction |
| 5625 | * will be dirty. |
| 5626 | */ |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5627 | if (!wasdel && tp->t_blk_res == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5629 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ |
| 5630 | XFS_IFORK_MAXEXT(ip, whichfork) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | del.br_startoff > got.br_startoff && |
| 5632 | del.br_startoff + del.br_blockcount < |
| 5633 | got.br_startoff + got.br_blockcount) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5634 | error = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | goto error0; |
| 5636 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5637 | |
| 5638 | /* |
| 5639 | * Unreserve quota and update realtime free space, if |
| 5640 | * appropriate. If delayed allocation, update the inode delalloc |
| 5641 | * counter now and wait to update the sb counters as |
| 5642 | * xfs_bmap_del_extent() might need to borrow some blocks. |
| 5643 | */ |
| 5644 | if (wasdel) { |
| 5645 | ASSERT(startblockval(del.br_startblock) > 0); |
| 5646 | if (isrt) { |
| 5647 | xfs_filblks_t rtexts; |
| 5648 | |
| 5649 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5650 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5651 | xfs_mod_frextents(mp, (int64_t)rtexts); |
| 5652 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5653 | ip, -((long)del.br_blockcount), 0, |
| 5654 | XFS_QMOPT_RES_RTBLKS); |
| 5655 | } else { |
| 5656 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5657 | ip, -((long)del.br_blockcount), 0, |
| 5658 | XFS_QMOPT_RES_REGBLKS); |
| 5659 | } |
| 5660 | ip->i_delayed_blks -= del.br_blockcount; |
| 5661 | if (cur) |
| 5662 | cur->bc_private.b.flags |= |
| 5663 | XFS_BTCUR_BPRV_WASDEL; |
| 5664 | } else if (cur) |
| 5665 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5666 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5667 | error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del, |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5668 | &tmp_logflags, whichfork, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | logflags |= tmp_logflags; |
| 5670 | if (error) |
| 5671 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5672 | |
| 5673 | if (!isrt && wasdel) |
| 5674 | xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false); |
| 5675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | bno = del.br_startoff - 1; |
| 5677 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | /* |
| 5679 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5681 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5682 | if (lastx >= 0) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5683 | xfs_iext_get_extent(ifp, lastx, &got); |
| 5684 | if (got.br_startoff > bno && --lastx >= 0) |
| 5685 | xfs_iext_get_extent(ifp, lastx, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | extno++; |
| 5688 | } |
| 5689 | } |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5690 | if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0) |
| 5691 | *rlen = 0; |
| 5692 | else |
| 5693 | *rlen = bno - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | /* |
| 5696 | * Convert to a btree if necessary. |
| 5697 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5698 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5700 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | &cur, 0, &tmp_logflags, whichfork); |
| 5702 | logflags |= tmp_logflags; |
| 5703 | if (error) |
| 5704 | goto error0; |
| 5705 | } |
| 5706 | /* |
| 5707 | * transform from btree to extents, give it cur |
| 5708 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5709 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | ASSERT(cur != NULL); |
| 5711 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5712 | whichfork); |
| 5713 | logflags |= tmp_logflags; |
| 5714 | if (error) |
| 5715 | goto error0; |
| 5716 | } |
| 5717 | /* |
| 5718 | * transform from extents to local? |
| 5719 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5720 | error = 0; |
| 5721 | error0: |
| 5722 | /* |
| 5723 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5724 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5726 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5728 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5729 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5731 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | /* |
| 5733 | * Log inode even in the error case, if the transaction |
| 5734 | * is dirty we'll need to shut down the filesystem. |
| 5735 | */ |
| 5736 | if (logflags) |
| 5737 | xfs_trans_log_inode(tp, ip, logflags); |
| 5738 | if (cur) { |
| 5739 | if (!error) { |
| 5740 | *firstblock = cur->bc_private.b.firstblock; |
| 5741 | cur->bc_private.b.allocated = 0; |
| 5742 | } |
| 5743 | xfs_btree_del_cursor(cur, |
| 5744 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5745 | } |
| 5746 | return error; |
| 5747 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5748 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5749 | /* Unmap a range of a file. */ |
| 5750 | int |
| 5751 | xfs_bunmapi( |
| 5752 | xfs_trans_t *tp, |
| 5753 | struct xfs_inode *ip, |
| 5754 | xfs_fileoff_t bno, |
| 5755 | xfs_filblks_t len, |
| 5756 | int flags, |
| 5757 | xfs_extnum_t nexts, |
| 5758 | xfs_fsblock_t *firstblock, |
| 5759 | struct xfs_defer_ops *dfops, |
| 5760 | int *done) |
| 5761 | { |
| 5762 | int error; |
| 5763 | |
| 5764 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5765 | dfops); |
| 5766 | *done = (len == 0); |
| 5767 | return error; |
| 5768 | } |
| 5769 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5770 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5771 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5772 | * extent that precedes the target hole of the shift. |
| 5773 | */ |
| 5774 | STATIC bool |
| 5775 | xfs_bmse_can_merge( |
| 5776 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5777 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5778 | xfs_fileoff_t shift) /* shift fsb */ |
| 5779 | { |
| 5780 | xfs_fileoff_t startoff; |
| 5781 | |
| 5782 | startoff = got->br_startoff - shift; |
| 5783 | |
| 5784 | /* |
| 5785 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5786 | * the preceding extent. |
| 5787 | */ |
| 5788 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5789 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5790 | (left->br_state != got->br_state) || |
| 5791 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5792 | return false; |
| 5793 | |
| 5794 | return true; |
| 5795 | } |
| 5796 | |
| 5797 | /* |
| 5798 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5799 | * hole in the file. If an extent shift would result in the extent being fully |
| 5800 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5801 | * the preceding extent rather than do the shift. |
| 5802 | * |
| 5803 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5804 | * with the provided extents via xfs_bmse_can_merge(). |
| 5805 | */ |
| 5806 | STATIC int |
| 5807 | xfs_bmse_merge( |
| 5808 | struct xfs_inode *ip, |
| 5809 | int whichfork, |
| 5810 | xfs_fileoff_t shift, /* shift fsb */ |
| 5811 | int current_ext, /* idx of gotp */ |
| 5812 | struct xfs_bmbt_rec_host *gotp, /* extent to shift */ |
| 5813 | struct xfs_bmbt_rec_host *leftp, /* preceding extent */ |
| 5814 | struct xfs_btree_cur *cur, |
| 5815 | int *logflags) /* output */ |
| 5816 | { |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5817 | struct xfs_bmbt_irec got; |
| 5818 | struct xfs_bmbt_irec left; |
| 5819 | xfs_filblks_t blockcount; |
| 5820 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5821 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5822 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5823 | xfs_bmbt_get_all(gotp, &got); |
| 5824 | xfs_bmbt_get_all(leftp, &left); |
| 5825 | blockcount = left.br_blockcount + got.br_blockcount; |
| 5826 | |
| 5827 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5828 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 5829 | ASSERT(xfs_bmse_can_merge(&left, &got, shift)); |
| 5830 | |
| 5831 | /* |
| 5832 | * Merge the in-core extents. Note that the host record pointers and |
| 5833 | * current_ext index are invalid once the extent has been removed via |
| 5834 | * xfs_iext_remove(). |
| 5835 | */ |
| 5836 | xfs_bmbt_set_blockcount(leftp, blockcount); |
| 5837 | xfs_iext_remove(ip, current_ext, 1, 0); |
| 5838 | |
| 5839 | /* |
| 5840 | * Update the on-disk extent count, the btree if necessary and log the |
| 5841 | * inode. |
| 5842 | */ |
| 5843 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5844 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5845 | *logflags |= XFS_ILOG_CORE; |
| 5846 | if (!cur) { |
| 5847 | *logflags |= XFS_ILOG_DEXT; |
| 5848 | return 0; |
| 5849 | } |
| 5850 | |
| 5851 | /* lookup and remove the extent to merge */ |
| 5852 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5853 | got.br_blockcount, &i); |
| 5854 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5855 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5856 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5857 | |
| 5858 | error = xfs_btree_delete(cur, &i); |
| 5859 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5860 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5861 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5862 | |
| 5863 | /* lookup and update size of the previous extent */ |
| 5864 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock, |
| 5865 | left.br_blockcount, &i); |
| 5866 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5867 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5868 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5869 | |
| 5870 | left.br_blockcount = blockcount; |
| 5871 | |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5872 | return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock, |
| 5873 | left.br_blockcount, left.br_state); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5874 | } |
| 5875 | |
| 5876 | /* |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5877 | * Shift a single extent. |
| 5878 | */ |
| 5879 | STATIC int |
| 5880 | xfs_bmse_shift_one( |
| 5881 | struct xfs_inode *ip, |
| 5882 | int whichfork, |
| 5883 | xfs_fileoff_t offset_shift_fsb, |
| 5884 | int *current_ext, |
| 5885 | struct xfs_bmbt_rec_host *gotp, |
| 5886 | struct xfs_btree_cur *cur, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5887 | int *logflags, |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5888 | enum shift_direction direction, |
| 5889 | struct xfs_defer_ops *dfops) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5890 | { |
| 5891 | struct xfs_ifork *ifp; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5892 | struct xfs_mount *mp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5893 | xfs_fileoff_t startoff; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5894 | struct xfs_bmbt_rec_host *adj_irecp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5895 | struct xfs_bmbt_irec got; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5896 | struct xfs_bmbt_irec adj_irec; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5897 | int error; |
| 5898 | int i; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5899 | int total_extents; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5900 | |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5901 | mp = ip->i_mount; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5902 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5903 | total_extents = xfs_iext_count(ifp); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5904 | |
| 5905 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5906 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5907 | /* delalloc extents should be prevented by caller */ |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5908 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5909 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5910 | if (direction == SHIFT_LEFT) { |
| 5911 | startoff = got.br_startoff - offset_shift_fsb; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5912 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5913 | /* |
| 5914 | * Check for merge if we've got an extent to the left, |
| 5915 | * otherwise make sure there's enough room at the start |
| 5916 | * of the file for the shift. |
| 5917 | */ |
| 5918 | if (!*current_ext) { |
| 5919 | if (got.br_startoff < offset_shift_fsb) |
| 5920 | return -EINVAL; |
| 5921 | goto update_current_ext; |
| 5922 | } |
| 5923 | /* |
| 5924 | * grab the left extent and check for a large |
| 5925 | * enough hole. |
| 5926 | */ |
| 5927 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1); |
| 5928 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5929 | |
| 5930 | if (startoff < |
| 5931 | adj_irec.br_startoff + adj_irec.br_blockcount) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5932 | return -EINVAL; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5933 | |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5934 | /* check whether to merge the extent or shift it down */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5935 | if (xfs_bmse_can_merge(&adj_irec, &got, |
| 5936 | offset_shift_fsb)) { |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5937 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
| 5938 | *current_ext, gotp, adj_irecp, |
| 5939 | cur, logflags); |
| 5940 | if (error) |
| 5941 | return error; |
| 5942 | adj_irec = got; |
| 5943 | goto update_rmap; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5944 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5945 | } else { |
| 5946 | startoff = got.br_startoff + offset_shift_fsb; |
| 5947 | /* nothing to move if this is the last extent */ |
| 5948 | if (*current_ext >= (total_extents - 1)) |
| 5949 | goto update_current_ext; |
| 5950 | /* |
| 5951 | * If this is not the last extent in the file, make sure there |
| 5952 | * is enough room between current extent and next extent for |
| 5953 | * accommodating the shift. |
| 5954 | */ |
| 5955 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1); |
| 5956 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5957 | if (startoff + got.br_blockcount > adj_irec.br_startoff) |
| 5958 | return -EINVAL; |
| 5959 | /* |
| 5960 | * Unlike a left shift (which involves a hole punch), |
| 5961 | * a right shift does not modify extent neighbors |
| 5962 | * in any way. We should never find mergeable extents |
| 5963 | * in this scenario. Check anyways and warn if we |
| 5964 | * encounter two extents that could be one. |
| 5965 | */ |
| 5966 | if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb)) |
| 5967 | WARN_ON_ONCE(1); |
| 5968 | } |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5969 | /* |
| 5970 | * Increment the extent index for the next iteration, update the start |
| 5971 | * offset of the in-core extent and update the btree if applicable. |
| 5972 | */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5973 | update_current_ext: |
| 5974 | if (direction == SHIFT_LEFT) |
| 5975 | (*current_ext)++; |
| 5976 | else |
| 5977 | (*current_ext)--; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5978 | xfs_bmbt_set_startoff(gotp, startoff); |
| 5979 | *logflags |= XFS_ILOG_CORE; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5980 | adj_irec = got; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5981 | if (!cur) { |
| 5982 | *logflags |= XFS_ILOG_DEXT; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5983 | goto update_rmap; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5984 | } |
| 5985 | |
| 5986 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5987 | got.br_blockcount, &i); |
| 5988 | if (error) |
| 5989 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5990 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5991 | |
| 5992 | got.br_startoff = startoff; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5993 | error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock, |
| 5994 | got.br_blockcount, got.br_state); |
| 5995 | if (error) |
| 5996 | return error; |
| 5997 | |
| 5998 | update_rmap: |
| 5999 | /* update reverse mapping */ |
| 6000 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec); |
| 6001 | if (error) |
| 6002 | return error; |
| 6003 | adj_irec.br_startoff = startoff; |
| 6004 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6005 | } |
| 6006 | |
| 6007 | /* |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6008 | * Shift extent records to the left/right to cover/create a hole. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6009 | * |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6010 | * 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] | 6011 | * @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] | 6012 | * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb |
| 6013 | * is the length by which each extent is shifted. If there is no hole to shift |
| 6014 | * the extents into, this will be considered invalid operation and we abort |
| 6015 | * immediately. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6016 | */ |
| 6017 | int |
| 6018 | xfs_bmap_shift_extents( |
| 6019 | struct xfs_trans *tp, |
| 6020 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6021 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6022 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6023 | int *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6024 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6025 | xfs_fsblock_t *firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6026 | struct xfs_defer_ops *dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6027 | enum shift_direction direction, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6028 | int num_exts) |
| 6029 | { |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6030 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6031 | struct xfs_bmbt_rec_host *gotp; |
| 6032 | struct xfs_bmbt_irec got; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6033 | struct xfs_mount *mp = ip->i_mount; |
| 6034 | struct xfs_ifork *ifp; |
| 6035 | xfs_extnum_t nexts = 0; |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6036 | xfs_extnum_t current_ext; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6037 | xfs_extnum_t total_extents; |
| 6038 | xfs_extnum_t stop_extent; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6039 | int error = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6040 | int whichfork = XFS_DATA_FORK; |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6041 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6042 | |
| 6043 | if (unlikely(XFS_TEST_ERROR( |
| 6044 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 6045 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 6046 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 6047 | XFS_ERROR_REPORT("xfs_bmap_shift_extents", |
| 6048 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 6049 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6050 | } |
| 6051 | |
| 6052 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 6053 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6054 | |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6055 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 6056 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6057 | ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT); |
| 6058 | ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6059 | |
| 6060 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6061 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6062 | /* Read in all the extents */ |
| 6063 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6064 | if (error) |
| 6065 | return error; |
| 6066 | } |
| 6067 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6068 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6069 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 6070 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6071 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6072 | cur->bc_private.b.flags = 0; |
| 6073 | } |
| 6074 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6075 | /* |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 6076 | * 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] | 6077 | * are collapsing out, so we cannot use the count of real extents here. |
| 6078 | * Instead we have to calculate it from the incore fork. |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 6079 | */ |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 6080 | total_extents = xfs_iext_count(ifp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6081 | if (total_extents == 0) { |
| 6082 | *done = 1; |
| 6083 | goto del_cursor; |
| 6084 | } |
| 6085 | |
| 6086 | /* |
| 6087 | * In case of first right shift, we need to initialize next_fsb |
| 6088 | */ |
| 6089 | if (*next_fsb == NULLFSBLOCK) { |
| 6090 | gotp = xfs_iext_get_ext(ifp, total_extents - 1); |
| 6091 | xfs_bmbt_get_all(gotp, &got); |
| 6092 | *next_fsb = got.br_startoff; |
| 6093 | if (stop_fsb > *next_fsb) { |
| 6094 | *done = 1; |
| 6095 | goto del_cursor; |
| 6096 | } |
| 6097 | } |
| 6098 | |
| 6099 | /* Lookup the extent index at which we have to stop */ |
| 6100 | if (direction == SHIFT_RIGHT) { |
| 6101 | gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent); |
| 6102 | /* Make stop_extent exclusive of shift range */ |
| 6103 | stop_extent--; |
| 6104 | } else |
| 6105 | stop_extent = total_extents; |
| 6106 | |
| 6107 | /* |
| 6108 | * Look up the extent index for the fsb where we start shifting. We can |
| 6109 | * henceforth iterate with current_ext as extent list changes are locked |
| 6110 | * out via ilock. |
| 6111 | * |
| 6112 | * gotp can be null in 2 cases: 1) if there are no extents or 2) |
| 6113 | * *next_fsb lies in a hole beyond which there are no extents. Either |
| 6114 | * way, we are done. |
| 6115 | */ |
| 6116 | gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext); |
| 6117 | if (!gotp) { |
| 6118 | *done = 1; |
| 6119 | goto del_cursor; |
| 6120 | } |
| 6121 | |
| 6122 | /* some sanity checking before we finally start shifting extents */ |
| 6123 | if ((direction == SHIFT_LEFT && current_ext >= stop_extent) || |
| 6124 | (direction == SHIFT_RIGHT && current_ext <= stop_extent)) { |
| 6125 | error = -EIO; |
| 6126 | goto del_cursor; |
| 6127 | } |
| 6128 | |
| 6129 | while (nexts++ < num_exts) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6130 | error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6131 | ¤t_ext, gotp, cur, &logflags, |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 6132 | direction, dfops); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6133 | if (error) |
| 6134 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6135 | /* |
| 6136 | * If there was an extent merge during the shift, the extent |
| 6137 | * count can change. Update the total and grade the next record. |
| 6138 | */ |
| 6139 | if (direction == SHIFT_LEFT) { |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 6140 | total_extents = xfs_iext_count(ifp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6141 | stop_extent = total_extents; |
| 6142 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6143 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6144 | if (current_ext == stop_extent) { |
| 6145 | *done = 1; |
| 6146 | *next_fsb = NULLFSBLOCK; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6147 | break; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6148 | } |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 6149 | gotp = xfs_iext_get_ext(ifp, current_ext); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6150 | } |
| 6151 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6152 | if (!*done) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6153 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6154 | *next_fsb = got.br_startoff; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 6155 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6156 | |
| 6157 | del_cursor: |
| 6158 | if (cur) |
| 6159 | xfs_btree_del_cursor(cur, |
| 6160 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6161 | |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 6162 | if (logflags) |
| 6163 | xfs_trans_log_inode(tp, ip, logflags); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 6164 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 6165 | return error; |
| 6166 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6167 | |
| 6168 | /* |
| 6169 | * Splits an extent into two extents at split_fsb block such that it is |
| 6170 | * the first block of the current_ext. @current_ext is a target extent |
| 6171 | * to be split. @split_fsb is a block where the extents is split. |
| 6172 | * If split_fsb lies in a hole or the first block of extents, just return 0. |
| 6173 | */ |
| 6174 | STATIC int |
| 6175 | xfs_bmap_split_extent_at( |
| 6176 | struct xfs_trans *tp, |
| 6177 | struct xfs_inode *ip, |
| 6178 | xfs_fileoff_t split_fsb, |
| 6179 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6180 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6181 | { |
| 6182 | int whichfork = XFS_DATA_FORK; |
| 6183 | struct xfs_btree_cur *cur = NULL; |
| 6184 | struct xfs_bmbt_rec_host *gotp; |
| 6185 | struct xfs_bmbt_irec got; |
| 6186 | struct xfs_bmbt_irec new; /* split extent */ |
| 6187 | struct xfs_mount *mp = ip->i_mount; |
| 6188 | struct xfs_ifork *ifp; |
| 6189 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
| 6190 | xfs_extnum_t current_ext; |
| 6191 | int error = 0; |
| 6192 | int logflags = 0; |
| 6193 | int i = 0; |
| 6194 | |
| 6195 | if (unlikely(XFS_TEST_ERROR( |
| 6196 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 6197 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 6198 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 6199 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 6200 | XFS_ERRLEVEL_LOW, mp); |
| 6201 | return -EFSCORRUPTED; |
| 6202 | } |
| 6203 | |
| 6204 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 6205 | return -EIO; |
| 6206 | |
| 6207 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6208 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 6209 | /* Read in all the extents */ |
| 6210 | error = xfs_iread_extents(tp, ip, whichfork); |
| 6211 | if (error) |
| 6212 | return error; |
| 6213 | } |
| 6214 | |
| 6215 | /* |
| 6216 | * gotp can be null in 2 cases: 1) if there are no extents |
| 6217 | * or 2) split_fsb lies in a hole beyond which there are |
| 6218 | * no extents. Either way, we are done. |
| 6219 | */ |
| 6220 | gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext); |
| 6221 | if (!gotp) |
| 6222 | return 0; |
| 6223 | |
| 6224 | xfs_bmbt_get_all(gotp, &got); |
| 6225 | |
| 6226 | /* |
| 6227 | * Check split_fsb lies in a hole or the start boundary offset |
| 6228 | * of the extent. |
| 6229 | */ |
| 6230 | if (got.br_startoff >= split_fsb) |
| 6231 | return 0; |
| 6232 | |
| 6233 | gotblkcnt = split_fsb - got.br_startoff; |
| 6234 | new.br_startoff = split_fsb; |
| 6235 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 6236 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 6237 | new.br_state = got.br_state; |
| 6238 | |
| 6239 | if (ifp->if_flags & XFS_IFBROOT) { |
| 6240 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 6241 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6242 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6243 | cur->bc_private.b.flags = 0; |
| 6244 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 6245 | got.br_startblock, |
| 6246 | got.br_blockcount, |
| 6247 | &i); |
| 6248 | if (error) |
| 6249 | goto del_cursor; |
| 6250 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6251 | } |
| 6252 | |
| 6253 | xfs_bmbt_set_blockcount(gotp, gotblkcnt); |
| 6254 | got.br_blockcount = gotblkcnt; |
| 6255 | |
| 6256 | logflags = XFS_ILOG_CORE; |
| 6257 | if (cur) { |
| 6258 | error = xfs_bmbt_update(cur, got.br_startoff, |
| 6259 | got.br_startblock, |
| 6260 | got.br_blockcount, |
| 6261 | got.br_state); |
| 6262 | if (error) |
| 6263 | goto del_cursor; |
| 6264 | } else |
| 6265 | logflags |= XFS_ILOG_DEXT; |
| 6266 | |
| 6267 | /* Add new extent */ |
| 6268 | current_ext++; |
| 6269 | xfs_iext_insert(ip, current_ext, 1, &new, 0); |
| 6270 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 6271 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 6272 | |
| 6273 | if (cur) { |
| 6274 | error = xfs_bmbt_lookup_eq(cur, new.br_startoff, |
| 6275 | new.br_startblock, new.br_blockcount, |
| 6276 | &i); |
| 6277 | if (error) |
| 6278 | goto del_cursor; |
| 6279 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
| 6280 | cur->bc_rec.b.br_state = new.br_state; |
| 6281 | |
| 6282 | error = xfs_btree_insert(cur, &i); |
| 6283 | if (error) |
| 6284 | goto del_cursor; |
| 6285 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 6286 | } |
| 6287 | |
| 6288 | /* |
| 6289 | * Convert to a btree if necessary. |
| 6290 | */ |
| 6291 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 6292 | int tmp_logflags; /* partial log flag return val */ |
| 6293 | |
| 6294 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6295 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6296 | &cur, 0, &tmp_logflags, whichfork); |
| 6297 | logflags |= tmp_logflags; |
| 6298 | } |
| 6299 | |
| 6300 | del_cursor: |
| 6301 | if (cur) { |
| 6302 | cur->bc_private.b.allocated = 0; |
| 6303 | xfs_btree_del_cursor(cur, |
| 6304 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6305 | } |
| 6306 | |
| 6307 | if (logflags) |
| 6308 | xfs_trans_log_inode(tp, ip, logflags); |
| 6309 | return error; |
| 6310 | } |
| 6311 | |
| 6312 | int |
| 6313 | xfs_bmap_split_extent( |
| 6314 | struct xfs_inode *ip, |
| 6315 | xfs_fileoff_t split_fsb) |
| 6316 | { |
| 6317 | struct xfs_mount *mp = ip->i_mount; |
| 6318 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6319 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6320 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6321 | int error; |
| 6322 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6323 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6324 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6325 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6326 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6327 | |
| 6328 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6329 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6330 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6331 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6332 | |
| 6333 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6334 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6335 | if (error) |
| 6336 | goto out; |
| 6337 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6338 | error = xfs_defer_finish(&tp, &dfops, NULL); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6339 | if (error) |
| 6340 | goto out; |
| 6341 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6342 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6343 | |
| 6344 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6345 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6346 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6347 | return error; |
| 6348 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6349 | |
| 6350 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6351 | static bool |
| 6352 | xfs_bmap_is_update_needed( |
| 6353 | struct xfs_bmbt_irec *bmap) |
| 6354 | { |
| 6355 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6356 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6357 | } |
| 6358 | |
| 6359 | /* Record a bmap intent. */ |
| 6360 | static int |
| 6361 | __xfs_bmap_add( |
| 6362 | struct xfs_mount *mp, |
| 6363 | struct xfs_defer_ops *dfops, |
| 6364 | enum xfs_bmap_intent_type type, |
| 6365 | struct xfs_inode *ip, |
| 6366 | int whichfork, |
| 6367 | struct xfs_bmbt_irec *bmap) |
| 6368 | { |
| 6369 | int error; |
| 6370 | struct xfs_bmap_intent *bi; |
| 6371 | |
| 6372 | trace_xfs_bmap_defer(mp, |
| 6373 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6374 | type, |
| 6375 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6376 | ip->i_ino, whichfork, |
| 6377 | bmap->br_startoff, |
| 6378 | bmap->br_blockcount, |
| 6379 | bmap->br_state); |
| 6380 | |
| 6381 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6382 | INIT_LIST_HEAD(&bi->bi_list); |
| 6383 | bi->bi_type = type; |
| 6384 | bi->bi_owner = ip; |
| 6385 | bi->bi_whichfork = whichfork; |
| 6386 | bi->bi_bmap = *bmap; |
| 6387 | |
| 6388 | error = xfs_defer_join(dfops, bi->bi_owner); |
| 6389 | if (error) { |
| 6390 | kmem_free(bi); |
| 6391 | return error; |
| 6392 | } |
| 6393 | |
| 6394 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6395 | return 0; |
| 6396 | } |
| 6397 | |
| 6398 | /* Map an extent into a file. */ |
| 6399 | int |
| 6400 | xfs_bmap_map_extent( |
| 6401 | struct xfs_mount *mp, |
| 6402 | struct xfs_defer_ops *dfops, |
| 6403 | struct xfs_inode *ip, |
| 6404 | struct xfs_bmbt_irec *PREV) |
| 6405 | { |
| 6406 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6407 | return 0; |
| 6408 | |
| 6409 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6410 | XFS_DATA_FORK, PREV); |
| 6411 | } |
| 6412 | |
| 6413 | /* Unmap an extent out of a file. */ |
| 6414 | int |
| 6415 | xfs_bmap_unmap_extent( |
| 6416 | struct xfs_mount *mp, |
| 6417 | struct xfs_defer_ops *dfops, |
| 6418 | struct xfs_inode *ip, |
| 6419 | struct xfs_bmbt_irec *PREV) |
| 6420 | { |
| 6421 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6422 | return 0; |
| 6423 | |
| 6424 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6425 | XFS_DATA_FORK, PREV); |
| 6426 | } |
| 6427 | |
| 6428 | /* |
| 6429 | * Process one of the deferred bmap operations. We pass back the |
| 6430 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6431 | */ |
| 6432 | int |
| 6433 | xfs_bmap_finish_one( |
| 6434 | struct xfs_trans *tp, |
| 6435 | struct xfs_defer_ops *dfops, |
| 6436 | struct xfs_inode *ip, |
| 6437 | enum xfs_bmap_intent_type type, |
| 6438 | int whichfork, |
| 6439 | xfs_fileoff_t startoff, |
| 6440 | xfs_fsblock_t startblock, |
| 6441 | xfs_filblks_t blockcount, |
| 6442 | xfs_exntst_t state) |
| 6443 | { |
| 6444 | struct xfs_bmbt_irec bmap; |
| 6445 | int nimaps = 1; |
| 6446 | xfs_fsblock_t firstfsb; |
| 6447 | int flags = XFS_BMAPI_REMAP; |
| 6448 | int done; |
| 6449 | int error = 0; |
| 6450 | |
| 6451 | bmap.br_startblock = startblock; |
| 6452 | bmap.br_startoff = startoff; |
| 6453 | bmap.br_blockcount = blockcount; |
| 6454 | bmap.br_state = state; |
| 6455 | |
| 6456 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6457 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6458 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
| 6459 | ip->i_ino, whichfork, startoff, blockcount, state); |
| 6460 | |
| 6461 | if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) |
| 6462 | return -EFSCORRUPTED; |
| 6463 | if (whichfork == XFS_ATTR_FORK) |
| 6464 | flags |= XFS_BMAPI_ATTRFORK; |
| 6465 | |
| 6466 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
| 6467 | XFS_ERRTAG_BMAP_FINISH_ONE, |
| 6468 | XFS_RANDOM_BMAP_FINISH_ONE)) |
| 6469 | return -EIO; |
| 6470 | |
| 6471 | switch (type) { |
| 6472 | case XFS_BMAP_MAP: |
| 6473 | firstfsb = bmap.br_startblock; |
| 6474 | error = xfs_bmapi_write(tp, ip, bmap.br_startoff, |
| 6475 | bmap.br_blockcount, flags, &firstfsb, |
| 6476 | bmap.br_blockcount, &bmap, &nimaps, |
| 6477 | dfops); |
| 6478 | break; |
| 6479 | case XFS_BMAP_UNMAP: |
| 6480 | error = xfs_bunmapi(tp, ip, bmap.br_startoff, |
| 6481 | bmap.br_blockcount, flags, 1, &firstfsb, |
| 6482 | dfops, &done); |
| 6483 | ASSERT(done); |
| 6484 | break; |
| 6485 | default: |
| 6486 | ASSERT(0); |
| 6487 | error = -EFSCORRUPTED; |
| 6488 | } |
| 6489 | |
| 6490 | return error; |
| 6491 | } |